#!/usr/bin/perl -wT # licence: GPL # author: Ian Hickson # initialise system use strict; # be anal about stuff use diagnostics; # enable fuller diagnostics of warnings use CGI; # Initialize Parameters my $query = new CGI; my $size = ($query->param('size') or 23); my $maxPerTurn = ($query->param('maxPerTurn') or 3); my $remaining = ($query->param('remaining') or $size); my $wantLast = (defined($query->param('wantLast')) ? $query->param('wantLast') : 0); my $move = $query->param('move'); # print page print < NIM

NIM

EOF &play($size, $remaining, $move); print <

Start a new game.

Let me start a new game.

Create a new custom game:

Number of Matchsticks: EOF &printOptions($maxPerTurn, 1..64); print < Variant:

EOF # end sub play { my($size, $remaining, $yourMove, $myMove) = @_; if (defined($yourMove)) { $remaining -= $yourMove; if ($remaining) { $myMove = &doMyMove($remaining); $remaining -= $myMove; } else { $myMove = 0; } my $yourS = $yourMove == 1 ? '' : 's'; my $myS = $myMove == 1 ? '' : 's'; print "

\n You removed $yourMove matchstick$yourS, I removed $myMove matchstick$myS.\n"; print "

\n"; } else { print "

\n Welcome to NIM!\n"; print "

\n"; } &paint($size, $remaining, $myMove, $yourMove); } sub paint { my($size, $remaining, $myMove, $yourMove) = @_; print "
\n"; foreach ($remaining+1..$size) { print " <\n"; } foreach (1..$remaining) { print " |\n"; } print "
\n"; print "
\n"; if ($remaining) { print "
\n"; print "
\n"; print "

\n"; print " \n"; print " \n"; print " \n"; print " \n"; print " Number of matchsticks to remove:\n"; foreach (1..($remaining < $maxPerTurn ? $remaining : $maxPerTurn)) { print " \n"; } print "

\n
\n
\n"; } else { if ($myMove xor $wantLast) { print "

You win.

\n"; } else { print "

I win! Wohay!

\n"; } } } sub doMyMove { my($remaining) = @_; return ((($remaining-(!$wantLast)) % ($maxPerTurn+1)) or 1); } sub printOptions { my($value, @values) = @_; foreach (@values) { print ' $_\n"; } } sub printWantLastOptions { my $a; if ($wantLast) { $a = ' selected="selected"'; } else { $a = ''; } print " Winner takes last matchstick\n"; if (not $wantLast) { $a = ' selected="selected"'; } else { $a = ''; } print " Loser takes last matchstick\n"; }