Merge pull request #494 from NezumiRonin/main

Perl changes.
This commit is contained in:
Jeff Atwood
2022-01-13 09:45:26 -08:00
committed by GitHub
2 changed files with 135 additions and 96 deletions

View File

@@ -1,96 +1,96 @@
#!/usr/bin/perl
use strict;
use warnings;
# global variables defined here
my(@MUGWUMP) = ();
# subroutines defined here
# init_mugwump: pick the random places for the Mugwumps
sub init_mugwump() {
@MUGWUMP = ();
for (1 .. 4) {
push @MUGWUMP, [ int(rand 10), int(rand 10) ];
}
}
# main code starts here
# print introductory text
print <<HERE;
Mugwump
Creative Computing Morristown, New Jersey
The object of this game is to find four Mugwumps
hidden on a 10 by 10 grid. Homebase is position 0,0.
Any guess you make must be two numbers with each
number between 0 and 9, inclusive. First number
is distance to right of homebase and second number
is distance above homebase.
You get 10 tries. After each try, I will tell
you how far you are from each Mugwump.
HERE
# PLAY block implements a complete game, and the
# continue block prints the "let's play again" msg
PLAY: while (1) {
init_mugwump();
TURN: for my $turn (1 .. 10) {
printf("\nTurn no %d -- what is your guess ? ", $turn);
# Note that parsing of input is rudimentary, in keeping with the
# spirit of the original BASIC program. Increased input checks
# would be a good place to start working on this program!
chomp(my $in = <STDIN>);
my($M,$N) = split(/,/,$in);
$M = int($M);
$N = int($N);
for my $i (0 .. $#MUGWUMP) {
# -1 indicates a Mugwump that was already found
next if $MUGWUMP[$i]->[0] == -1;
if ($MUGWUMP[$i]->[0] == $M && $MUGWUMP[$i]->[1] == $N) {
$MUGWUMP[$i]->[0] = -1;
printf("You have found Mugwump %d\n", $i+1);
} else {
my $d = sqrt(($MUGWUMP[$i]->[0] - $M) ** 2 + ($MUGWUMP[$i]->[1] - $N) ** 2);
printf("You are %.1f units away from Mugwump %d\n", $d, $i+1);
}
}
# If a Mugwump still has not been found,
# go to the next turn
for my $j (0 .. $#MUGWUMP) {
if ($MUGWUMP[$j]->[0] != -1) {
next TURN;
}
}
# You win!
printf("You got all of them in %d %s!\n\n", $turn, ($turn == 1 ? 'turn' : 'turns'));
# Pass execution down to the continue block
next PLAY;
} # end of TURN loop
print "\nSorry, that's 10 tries. Here's where they're hiding:\n";
for my $i (0 .. $#MUGWUMP) {
printf("Mugwump %d is at (%d, %d)\n", $i+1, $MUGWUMP[$i]->[0], $MUGWUMP[$i]->[1])
if $MUGWUMP[$i]->[0] != -1;
}
}
continue {
print "\nThat was fun! Let's play again.......\n";
print "Four more Mugwumps are now in hiding.\n\n";
}
#!/usr/bin/perl
use strict;
use warnings;
# global variables defined here
my(@MUGWUMP) = ();
# subroutines defined here
# init_mugwump: pick the random places for the Mugwumps
sub init_mugwump() {
@MUGWUMP = ();
for (1 .. 4) {
push @MUGWUMP, [ int(rand 10), int(rand 10) ];
}
}
# main code starts here
# print introductory text
print <<HERE;
Mugwump
Creative Computing Morristown, New Jersey
The object of this game is to find four Mugwumps
hidden on a 10 by 10 grid. Homebase is position 0,0.
Any guess you make must be two numbers with each
number between 0 and 9, inclusive. First number
is distance to right of homebase and second number
is distance above homebase.
You get 10 tries. After each try, I will tell
you how far you are from each Mugwump.
HERE
# PLAY block implements a complete game, and the
# continue block prints the "let's play again" msg
PLAY: while (1) {
init_mugwump();
TURN: for my $turn (1 .. 10) {
printf("\nTurn no %d -- what is your guess ? ", $turn);
# Note that parsing of input is rudimentary, in keeping with the
# spirit of the original BASIC program. Increased input checks
# would be a good place to start working on this program!
chomp(my $in = <STDIN>);
my($M,$N) = split(/,/,$in);
$M = int($M);
$N = int($N);
for my $i (0 .. $#MUGWUMP) {
# -1 indicates a Mugwump that was already found
next if $MUGWUMP[$i]->[0] == -1;
if ($MUGWUMP[$i]->[0] == $M && $MUGWUMP[$i]->[1] == $N) {
$MUGWUMP[$i]->[0] = -1;
printf("You have found Mugwump %d\n", $i+1);
} else {
my $d = sqrt(($MUGWUMP[$i]->[0] - $M) ** 2 + ($MUGWUMP[$i]->[1] - $N) ** 2);
printf("You are %.1f units away from Mugwump %d\n", $d, $i+1);
}
}
# If a Mugwump still has not been found,
# go to the next turn
for my $j (0 .. $#MUGWUMP) {
if ($MUGWUMP[$j]->[0] != -1) {
next TURN;
}
}
# You win!
printf("You got all of them in %d %s!\n\n", $turn, ($turn == 1 ? 'turn' : 'turns'));
# Pass execution down to the continue block
next PLAY;
} # end of TURN loop
print "\nSorry, that's 10 tries. Here's where they're hiding:\n";
for my $i (0 .. $#MUGWUMP) {
printf("Mugwump %d is at (%d, %d)\n", $i+1, $MUGWUMP[$i]->[0], $MUGWUMP[$i]->[1])
if $MUGWUMP[$i]->[0] != -1;
}
}
continue {
print "\nThat was fun! Let's play again.......\n";
print "Four more Mugwumps are now in hiding.\n\n";
}

View File

@@ -0,0 +1,39 @@
#!/usr/bin/perl
#use strict;
# Automatic converted by bas2perl.pl
print ' 'x28 . "RUSSIAN ROULETTE\n";
print ' 'x15 . "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n";
print "\n"; print "\n"; print "\n";
print "THIS IS A GAME OF >>>>>>>>>>RUSSIAN ROULETTE.\n";
Line10:
print "\n"; print "HERE IS A REVOLVER.\n";
Line20:
print "TYPE '1' TO SPIN CHAMBER AND PULL TRIGGER.\n";
print "TYPE '2' TO GIVE UP.\n";
print "GO";
$N=0;
Line30:
print "? "; chomp($I = <STDIN>);
if ($I ne 2) { goto Line35; }
print " CHICKEN!!!!!\n";
goto Line72;
Line35:
$N=$N+1;
if (rand(1)>.833333) { goto Line70; }
if ($N>10) { goto Line80; }
print "- CLICK -\n";
print "\n"; goto Line30;
Line70:
print " BANG!!!!! YOU'RE DEAD!\n";
print "CONDOLENCES WILL BE SENT TO YOUR RELATIVES.\n";
Line72:
print "\n"; print "\n"; print "\n";
print "...NEXT VICTIM...\n"; goto Line20;
Line80:
print "YOU WIN!!!!!\n";
print "LET SOMEONE ELSE BLOW HIS BRAINS OUT.\n";
goto Line10;
exit;