Skip to content

Commit 3455848

Browse files
author
kbrannen
committed
added 15-boxing for perl
1 parent 89a01d3 commit 3455848

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

15_Boxing/perl/boxing.pl

+252
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
#!/usr/bin/perl
2+
3+
# Boxing program in Perl
4+
# Required extensive restructuring to remove all of the GOTO's.
5+
# Translated by Kevin Brannen (kbrannen)
6+
7+
use strict;
8+
use warnings;
9+
10+
# globals
11+
my $Opp_won = 0; # num rounds opponent has won
12+
my $You_won = 0; # num rounds you have won
13+
my $Opp_name = ""; # opponent name
14+
my $Your_name = ""; # your name
15+
my $Your_best = 0; # your best punch
16+
my $Your_worst = 0; # your worst punch
17+
my $Opp_best; # opponent best punch
18+
my $Opp_worst; # opponent worst punch
19+
my $Opp_damage; # opponent damage ?
20+
my $Your_damage; # your damage ?
21+
22+
sub get_punch
23+
{
24+
my $prompt = shift;
25+
my $p;
26+
while (1)
27+
{
28+
print "$prompt: ";
29+
chomp($p = int(<>));
30+
last if ($p >= 1 && $p <= 4);
31+
print "DIFFERENT PUNCHES ARE: (1) FULL SWING; (2) HOOK; (3) UPPERCUT; (4) JAB.\n";
32+
}
33+
return $p;
34+
}
35+
36+
print "\n";
37+
print " " x 33, "BOXING\n";
38+
print " " x 15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n";
39+
40+
print "BOXING OLYMPIC STYLE (3 ROUNDS -- 2 OUT OF 3 WINS)\n\n";
41+
print "WHAT IS YOUR OPPONENT'S NAME: ";
42+
chomp($Opp_name = <>);
43+
print "INPUT YOUR MAN'S NAME: ";
44+
chomp($Your_name = <>);
45+
print "DIFFERENT PUNCHES ARE: (1) FULL SWING; (2) HOOK; (3) UPPERCUT; (4) JAB.\n";
46+
$Your_best = get_punch("WHAT IS YOUR MANS BEST");
47+
$Your_worst = get_punch("WHAT IS HIS VULNERABILITY");
48+
49+
do {
50+
$Opp_best = int(4*rand(1)+1);
51+
$Opp_worst = int(4*rand(1)+1);
52+
} while ($Opp_best == $Opp_worst);
53+
print "$Opp_name\'S ADVANTAGE IS $Opp_best AND VULNERABILITY IS SECRET.\n\n";
54+
55+
for my $R (1 .. 3) # rounds
56+
{
57+
last if ($Opp_won >= 2 || $You_won >= 2);
58+
$Opp_damage = 0;
59+
$Your_damage = 0;
60+
print "ROUND $R BEGINS...\n";
61+
for my $R1 (1 .. 7) # 7 events per round?
62+
{
63+
if (int(10*rand(1)+1) <= 5)
64+
{
65+
my $your_punch = get_punch("$Your_name\'S PUNCH");
66+
$Opp_damage += 2 if ($your_punch == $Your_best);
67+
68+
if ($your_punch == 1) { punch1(); }
69+
elsif ($your_punch == 2) { punch2(); }
70+
elsif ($your_punch == 3) { punch3(); }
71+
else { punch4(); }
72+
next;
73+
}
74+
75+
my $Opp_punch = int(4*rand(1)+1);
76+
$Your_damage += 2 if ($Opp_punch == $Opp_best);
77+
78+
if ($Opp_punch == 1) { opp1(); }
79+
elsif ($Opp_punch == 2) { opp2(); }
80+
elsif ($Opp_punch == 3) { opp3(); }
81+
else { opp4(); }
82+
}
83+
84+
if ($Opp_damage > $Your_damage)
85+
{
86+
print "\n$Your_name WINS ROUND $R\n\n";
87+
$You_won++;
88+
}
89+
else
90+
{
91+
print "\n$Opp_name WINS ROUND $R\n\n";
92+
$Opp_won++;
93+
}
94+
}
95+
96+
if ($Opp_won >= 2)
97+
{
98+
done("$Opp_name WINS (NICE GOING, $Opp_name).");
99+
}
100+
101+
#else # if ($You_won >= 2)
102+
done("$Your_name AMAZINGLY WINS!!");
103+
104+
###################################################
105+
106+
sub done
107+
{
108+
my $msg = shift;
109+
print $msg;
110+
print "\n\nAND NOW GOODBYE FROM THE OLYMPIC ARENA.\n\n";
111+
exit(0);
112+
}
113+
114+
sub punch1
115+
{
116+
# $your_punch == 1, full swing
117+
print "$Your_name SWINGS AND ";
118+
if ($Opp_worst == 4 || int(30*rand(1)+1) < 10)
119+
{
120+
print "HE CONNECTS!\n";
121+
if ($Opp_damage > 35)
122+
{
123+
done("$Opp_name IS KNOCKED COLD AND $Your_name IS THE WINNER AND CHAMP! ");
124+
}
125+
$Opp_damage += 15;
126+
}
127+
else
128+
{
129+
print "HE MISSES\n";
130+
print "\n\n" if ($Opp_damage != 1);
131+
}
132+
}
133+
134+
sub punch2
135+
{
136+
# $your_punch == 2, hook
137+
print "$Your_name GIVES THE HOOK... ";
138+
if ($Opp_worst == 2)
139+
{
140+
$Opp_damage += 7;
141+
return;
142+
}
143+
if (int(2*rand(1)+1) == 1)
144+
{
145+
print "BUT IT'S BLOCKED!!!!!!!!!!!!!\n";
146+
}
147+
else
148+
{
149+
print "CONNECTS...\n";
150+
$Opp_damage += 7;
151+
}
152+
}
153+
154+
sub punch3
155+
{
156+
# $your_punch == 3, uppercut
157+
print "$Your_name TRIES AN UPPERCUT ";
158+
if ($Opp_worst == 3 || int(100*rand(1)+1) < 51)
159+
{
160+
print "AND HE CONNECTS!\n";
161+
$Opp_damage += 4;
162+
}
163+
else
164+
{
165+
print "AND IT'S BLOCKED (LUCKY BLOCK!)\n";
166+
}
167+
}
168+
169+
sub punch4
170+
{
171+
# $your_punch == 4, jab
172+
print "$Your_name JABS AT $Opp_name\'S HEAD ";
173+
if ($Opp_worst == 4 || (int(8*rand(1)+1)) >= 4)
174+
{
175+
$Opp_damage += 3;
176+
print "\n";
177+
}
178+
else
179+
{
180+
print "IT'S BLOCKED.\n";
181+
}
182+
}
183+
184+
sub opp1
185+
{
186+
# opp_punch == 1
187+
print "$Opp_name TAKES A FULL SWING AND ";
188+
if ($Your_worst == 1 || int(60*rand(1)+1) < 30)
189+
{
190+
print " POW!!!!! HE HITS HIM RIGHT IN THE FACE!\n";
191+
if ($Your_damage > 35)
192+
{
193+
done("$Your_name IS KNOCKED COLD AND $Opp_name IS THE WINNER AND CHAMP!");
194+
}
195+
$Your_damage += 15;
196+
}
197+
else
198+
{
199+
print " IT'S BLOCKED!\n";
200+
}
201+
}
202+
203+
sub opp2
204+
{
205+
# opp_punch == 2
206+
print "$Opp_name GETS $Your_name IN THE JAW (OUCH!)\n";
207+
$Your_damage += 7;
208+
print "....AND AGAIN!\n";
209+
$Your_damage += 5;
210+
if ($Your_damage > 35)
211+
{
212+
done("$Your_name IS KNOCKED COLD AND $Opp_name IS THE WINNER AND CHAMP!");
213+
}
214+
print "\n";
215+
# 2 continues into opp_punch == 3
216+
opp3();
217+
}
218+
219+
sub opp3()
220+
{
221+
# opp_punch == 3
222+
print "$Your_name IS ATTACKED BY AN UPPERCUT (OH,OH)...\n";
223+
if ($Your_worst != 3 && int(200*rand(1)+1) > 75)
224+
{
225+
print " BLOCKS AND HITS $Opp_name WITH A HOOK.\n";
226+
$Opp_damage += 5;
227+
}
228+
else
229+
{
230+
print "AND $Opp_name CONNECTS...\n";
231+
$Your_damage += 8;
232+
}
233+
}
234+
235+
sub opp4
236+
{
237+
# opp_punch == 4
238+
print "$Opp_name JABS AND ";
239+
if ($Your_worst == 4)
240+
{
241+
$Your_damage += 5;
242+
}
243+
elsif (int(7*rand(1)+1) > 4)
244+
{
245+
print " BLOOD SPILLS !!!\n";
246+
$Your_damage += 5;
247+
}
248+
else
249+
{
250+
print "IT'S BLOCKED!\n";
251+
}
252+
}

0 commit comments

Comments
 (0)