# $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; use base qw(Exporter); use vars qw(@ISA $VERSION); $VERSION='1.0'; ## This sets up a superclass. We decide to inherit board methods @ISA = qw(TicTacToe::Board); sub new { return bless TicTacToe::Board->new(); } sub play { my $self = shift; $self->display(); my @squares = $self->position()->unoccupied_squares(); &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) = TicTacToe::Move->new($square); $self = $self->apply_move($move); $self->display(); if(defined($self->result())) { if($self->result() eq 'won') { print "You won! Congratulations.\n"; } elsif($self->result() eq 'stalemate') { print "It's a stalemate, mate!\n"; } last; } $move = &alpha_beta($self, -1000, 1000); #$move = &min_max($self); $self = $self->apply_move($move); $self->display(); if(defined($self->result())) { if($self->result() eq 'won') { print "I won. Perhaps the next time you will have more luck.\n"; } elsif($self->result() eq 'stalemate') { print "It's a stalemate, mate!"; } last; } @squares = $self->position()->unoccupied_squares(); &prompt(@squares); } } sub prompt { print "Enter square (legal are ", join(',', @_), "):\n"; } sub syntax($) { print "Illegal move: $_[0]\n"; } 1;