Skip to content

Instantly share code, notes, and snippets.

@Caaz
Last active February 10, 2017 01:59
Show Gist options
  • Save Caaz/7783fa070a2382be6180b1f1dc31f838 to your computer and use it in GitHub Desktop.
Save Caaz/7783fa070a2382be6180b1f1dc31f838 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
my $board = 12;
sub board($) {
print "There are $board board pieces left.\n";
print "\n".$_[0]." Wins!\n\n" if($board <= 0);
}
sub take($$) {
my ($p,$t) = @_;
return 0 if(($t > 3) || ($t <= 0) || ($t > $board)) && (print "Invalid input!\n");
print "$p takes $t pieces\n"; $board -= $t; board($p); 1;
}
sub getInput($) { print($_[0].': '); chomp(my $result = <STDIN>); int($result); }
START: {
$board = getInput('Starting pieces') or redo;
redo if($board < 4) && (print "Too few pieces!\n");
}
while($board > 0) {
PLAYER: { take('Player', (getInput('How many pieces do you want to take (1-3)') or redo)) or redo; }
take('NIM',$board % 4 || 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment