# $Id: Player.pm,v 1.1.1.1 2008/10/12 04:05:34 alamos Exp $ # The state is in an array package TicTacToe::Player; use strict; use TicTacToe::Board; use TicTacToe::MinMax; use TicTacToe::Position qw(:all); use base qw(Exporter); use vars qw(@ISA $VERSION @EXPORT); $VERSION='1.0'; @EXPORT = qw(&play); sub play { my $game = &init_game; &display_game($game); my @squares = &unoccupied_squares($game->{position}); &prompt(@squares); while(1) { last unless defined(my $square=<>); chomp($square); if(!grep /^$square$/, @squares) { &syntax($square); next; } print "You chose $square.\n"; my($move) = &make_move($square); $game = &apply_move($game, $move); &display_game($game); if(defined($game->{result})) { if($game->{result} eq 'won') { print "You won! Congratulations.\n"; } elsif($game->{result} eq 'stalemate') { print "It's a stalemate, mate!\n"; } last; } $move = &alpha_beta($game, -1000, 1000); #$move = &min_max($game); $game = &apply_move($game, $move); &display_game($game); if(defined($game->{result})) { if($game->{result} eq 'won') { print "I won. Perhaps the next time you will have more luck.\n"; } elsif($game->{result} eq 'stalemate') { print "It's a stalemate, mate!"; } last; } @squares = &unoccupied_squares($game->{position}); &prompt(@squares); } } sub prompt { print "Enter square (legal are ", join(',', @_), "):\n"; } sub syntax($) { print "Illegal move: $_[0]\n"; } 1;