#! /usr/bin/perl use strict ; # Need to change these defintions and use statements # These are the names of the classes for the two players # If the name has a ::, as in OddWins::Random # The Perl modules chould be stored in OddWins/Random # That is, replace the ::'s, with /'s use OddWins::Random ; use OddWins::Winner ; my $ClassNamePlayer1 = 'OddWins::Random' ; my $ClassNamePlayer2 = 'OddWins::Winner' ; # Use these to set the number of games played my $maxGames = 100 ; my $boardSize = 15 ; my $maxSize = 3 ; # Play a game # Return 1, if firstPlayer wins # Return 2, if secondPlayer wins # Return -1, if an illegal move is made sub playGame { my ($ClassNameFirst, $ClassNameSecond, $boardSize, $maxMoves) = @_ ; my $playerFirst = $ClassNameFirst->new($boardSize, $maxMoves) ; my $playerSecond = $ClassNameSecond->new($boardSize, $maxMoves) ; my $tokensFirst = 0 ; my $tokensSecond = 0 ; my $tokensLeft = $boardSize ; while (1) { # Let the first player move my $moveFirst = $playerFirst->myTakes() ; printf " %-20s: %2d\n", $ClassNameFirst, $moveFirst ; if ($moveFirst<=0 || $moveFirst>$maxMoves || $moveFirst>$tokensLeft) { return -1 ; } my $ackSecond = $playerSecond->opponentTakes($moveFirst) ; if ($ackSecond != 1) { printf " %-20s: objects\n", $ClassNameSecond ; return -1 ; } $tokensFirst += $moveFirst ; $tokensLeft -= $moveFirst ; if ($tokensLeft == 0) { return ($tokensFirst % 2) ? 1 : 2 ; } # Now let the second player move my $moveSecond = $playerSecond->myTakes() ; printf " %-20s: %2d\n", $ClassNameSecond, $moveSecond ; if ($moveSecond<=0||$moveSecond>$maxMoves||$moveSecond>$tokensLeft) { return -1 ; } my $ackFirst = $playerFirst->opponentTakes($moveSecond) ; if ($ackFirst != 1) { printf " %-20s: objects\n", $ClassNameFirst ; return -1 ; } $tokensSecond += $moveSecond ; $tokensLeft -= $moveSecond ; if ($tokensLeft == 0) { return ($tokensFirst % 2) ? 1 : 2 ; } } } print "\n\n${ClassNamePlayer1} vs. ${ClassNamePlayer2}:" ; print " ${maxGames} matches\n" ; print " Size = (${boardSize}, ${maxSize})\n" ; my $Player1Wins = 0 ; my $Player2Wins = 0 ; for (my $i = 1 ; $i<=$maxGames; $i+=2) { printf "\nGame %3d:\n", $i ; my $winner = &playGame($ClassNamePlayer1,$ClassNamePlayer2,$boardSize,$maxSize) ; if ($winner == 1) { ++$Player1Wins ; } elsif ($winner == 2) { ++$Player2Wins ; } else { print STDERR "Bad Game!\n" ; } if ($i==$maxGames) { last ; } printf "\nGame %3d:\n", $i+1 ; $winner = &playGame($ClassNamePlayer2,$ClassNamePlayer1,$boardSize,$maxSize) ; if ($winner == 1) { ++$Player2Wins ; } elsif ($winner == 2) { ++$Player1Wins ; } else { print STDERR "Bad Game!\n" ; } } printf "\n\nFinal Score\n" ; printf " %-20s: %3d\n", $ClassNamePlayer1, $Player1Wins ; printf " %-20s: %3d\n", $ClassNamePlayer2, $Player2Wins ;