Next 5.2 Automatic play, example script Prev

Automatic play, example script

use Games::Checkers::Constants;
use Games::Checkers::Board;
use Games::Checkers::BoardTree;

my $board = new Games::Checkers::Board;
my $color = White;
my $numMoves = 0;
print $board->dump;

while ($board->canColorMove($color)) {
        sleep(2);
        exit if $numMoves++ == 200;  # allow 100 moves for each player
        my $boardTree = new Games::Checkers::BoardTree
                ($board, $color, 2);  # think 2 steps ahead
        my $move = $boardTree->chooseBestMove;  # or: chooseRandomMove

        $board->transform($move);
        print $move->dump, "\n", $board->dump;
        $color = ($color == White)? Black: White;
}

print "\n", ["Black", "White"]->[$color == White? 0: 1], " won.\n";

Next Artificial Intelligence and Perl Prev