I am programming a 2-player nim game on PERL. The rule is that the computer randomly selects 20-30 stones and each player takes one or two stones each turn. The person who takes the last stones wins the game. I am stuck and here is what I got so far...
if ( $input eq "" ) {
print "<center>Player 1: <input type='text' name='p1'></center><br>";
print "<center>Player 2: <input type='text' name='p2'></center><br>";
print "<center><input type='submit' name='do' value='Play!'></center>";
exit }
if ( $input =~ /p1=(.*)&p2=(.*)&do=Play%21/ ) {
$p1 = $1;
$p2 = $2;
$stones = 20 + int ( rand (11) );
print "<h3><center>$p1, it's your turn. There are $stones stones left.</center></h3><br>\n";
print "<center>What would you like to do?</center><br>";
print "<center><input type='submit' name='do' value='Take 1'></center>";
print "<center><input type='submit' name='do' value='Take 2'></center>";
print "<center><input type='submit' name='do' value='I give up. $p2 wins.'></center><br>";
print "<center><input type='hidden' name='old' value='$p1'></center><br>";
print "<center><input type='hidden' name='new' value='$p2'></center><br>";
print "<center><input type='hidden' name='stones' value='$stones'></center><br>";
exit }
if ( $input =~ /do=Take\+(.)&old=(.*)&new=(.*)&stones=(.*)/) {
$take = $1;
$old = $2;
$new = $3;
$stones = $4;
print "<center><h3>$old just took $take stones.</h3></center><br>";
$left = $stones - $take;
print "<center><h3>$new, it's your turn. There are $left stones left. </h3></center><br>";
print "<center>What would you like to do?</center><br>";
print "<center><input type='submit' name='do' value='Take 1'></center>";
if ( $left > 1) {
print "<center><input type='submit' name='do' value='Take 2'></center>" }
print "<center><input type='submit' name='do' value='I give up. $old wins.'></center>";
print "<center><input type='hidden' name='old' value='$new'></center><br>";
print "<center><input type='hidden' name='new' value='$old'></center><br>";
print "<center><input type='hidden' name='stones' value='$left'></center><br>";
exit }
So now when a player takes the last stone, I need it to say congrats and the winner's name and maybe go back to the beginning. Also, I can't get the "give up" part to work. Please help me to get this game to work!
I didn't know how to align them all at the same time for a cgi file so I had to align each of them. If you know a better way, please teach me!
The winner is supposed to be the person who takes the last stone. And if there is only one stone left, the "take 2" button should not appear.
if ( $input eq "" ) {
print "<center>Player 1: <input type='text' name='p1'></center><br>";
print "<center>Player 2: <input type='text' name='p2'></center><br>";
print "<center><input type='submit' name='do' value='Play!'></center>";
exit }
if ( $input =~ /p1=(.*)&p2=(.*)&do=Play%21/ ) {
$p1 = $1;
$p2 = $2;
$stones = 20 + int ( rand (11) );
print "<h3><center>$p1, it's your turn. There are $stones stones left.</center></h3><br>\n";
print "<center>What would you like to do?</center><br>";
print "<center><input type='submit' name='do' value='Take 1'></center>";
print "<center><input type='submit' name='do' value='Take 2'></center>";
print "<center><input type='submit' name='do' value='I give up. $p2 wins.'></center><br>";
print "<center><input type='hidden' name='old' value='$p1'></center><br>";
print "<center><input type='hidden' name='new' value='$p2'></center><br>";
print "<center><input type='hidden' name='stones' value='$stones'></center><br>";
exit }
if ( $input =~ /do=Take\+(.)&old=(.*)&new=(.*)&stones=(.*)/) {
$take = $1;
$old = $2;
$new = $3;
$stones = $4;
print "<center><h3>$old just took $take stones.</h3></center><br>";
$left = $stones - $take;
print "<center><h3>$new, it's your turn. There are $left stones left. </h3></center><br>";
print "<center>What would you like to do?</center><br>";
print "<center><input type='submit' name='do' value='Take 1'></center>";
if ( $left > 1) {
print "<center><input type='submit' name='do' value='Take 2'></center>" }
print "<center><input type='submit' name='do' value='I give up. $old wins.'></center>";
print "<center><input type='hidden' name='old' value='$new'></center><br>";
print "<center><input type='hidden' name='new' value='$old'></center><br>";
print "<center><input type='hidden' name='stones' value='$left'></center><br>";
exit }
So now when a player takes the last stone, I need it to say congrats and the winner's name and maybe go back to the beginning. Also, I can't get the "give up" part to work. Please help me to get this game to work!
I didn't know how to align them all at the same time for a cgi file so I had to align each of them. If you know a better way, please teach me!
The winner is supposed to be the person who takes the last stone. And if there is only one stone left, the "take 2" button should not appear.