package Apache::Toe; # # for reasons beyond my comprehension, the package breaks when # named Apache::TicTacToe, probably a local config issue. # use strict; use vars qw($VERSION); $VERSION = '0.2'; use Apache::Constants qw(:common); use Apache::Request; use Aw; require Aw::Client; require Aw::Event; my $eventTypeName; my ($c,$te,$ter); my $message = "ChildInit Error!"; my @board = (); my $tttEvent = "PerlDevKit::TicTacToe"; my $tttEventRequest = "PerlDevKit::TicTacToeRequest"; my %Images =( 'e' => "src=\"/images/TicTacToe/empty.gif\" alt=\"[ ]\"", 'O' => "src=\"/images/TicTacToe/not.gif\" alt=\"[O]\"", 'X' => "src=\"/images/TicTacToe/cross.gif\" alt=\"[X]\"" ); sub childinit { $message="Hello"; $c = new Aw::Client ( "PerlDemoClient", "Apache.$$" ); unless ( $c->canPublish ( $tttEvent ) ) { $message="Can't publish: $tttEvent"; # printf STDERR "Cannot publish to %s: %s\n", $tttEvent, $c->errmsg; # exit ( 0 ); } unless ( $c->canPublish ( $tttEventRequest ) ) { $message="Can't publish: $tttEventRequest"; # printf STDERR "Cannot publish to %s: %s\n", $tttEvent, $c->errmsg; # exit ( 0 ); } $c->newSubscriptions ( $tttEvent, 0 ); $ter = new Aw::Event ($c, $tttEventRequest); unless ( ref ($ter) ) { $message="TER HAS NO REF"; } $te = new Aw::Event ( $c, $tttEvent ); unless ( ref ($te) ) { $message="TER HA NO REF"; } 1; } my $moves = 0; my $WIN = 1; my $LOSE = 2; my $DRAW = 3; my $OK = 0; sub checkWin { my @check_win =( 0, 1, 2, # row 1 3, 4, 5, # row 2 6, 7, 8, # row 3 0, 3, 6, # col 1 1, 4, 7, # col 2 2, 5, 8, # col 3 0, 4, 8, 2, 4, 6 ); while (@check_win) { my $tic = shift ( @check_win ); return unless ( defined($tic) ); my $tac = shift ( @check_win ); my $toe = shift ( @check_win ); next if ( ($board[$tic] eq 'e') || ($board[$tac] eq 'e') || ($board[$toe] eq 'e') ); if ( ($board[$tic] eq $board[$tac]) && ($board[$tac] eq $board[$toe]) ) { return ( $board[$tic] eq 'X' ) ? $WIN : $LOSE; } } if ( $moves == 9 ) { return $DRAW; } $OK; } sub printForm { my $r = shift; # # Do a checkWin here and print winner # my $status = ( $moves > 4 ) ? checkWin : $OK; my $title = "Your Move!"; if ( $status ) { if ( $status == $WIN ) { $title = "You Win!"; } elsif ( $status == $LOSE ) { $title = "You Lose!"; } else { $title = "Stalemate! Noone Wins!"; } } $r->print(<
| print ("X,");
}
else {
$r->print ("$board[$k],");
}
}
$r->print ( ")\"> | " );
}
else {
$r->print ( "