#!/usr/local/bin/perl
#
# game of tic-tac-toe for mosiac 2.0//http 1.0.5
#
# Jason Heirtzler
# 2/10/94
#
$host = "http://lobster.bu.edu";
$href = "$host:5999";
#
$xgif = "$host/TTT/x.gif";
$ogif = "$host/TTT/o.gif";
$bgif = "$host/TTT/b.gif";
#
$newgame = "
New game?";
#
# the winning final positions
@win = ("-x--x--x-", "--x--x--x", "x--x--x--",
"xxx------", "---xxx---", "------xxx",
"x---x---x", "--x-x-x--" );
#
require 'getopts.pl';
#
#
&Getopts( 'd' );
#
$done = 0;
$move = <>;
chop($move);
#
# initial board position is ---------
# called as follows:
#
# http://lobster.bu.edu:5999/tictactoe
#
# for ex: GET /ooooooooo HTTP/1.0
$save = $move;
$move =~ s!GET /(\S+).*!$1! unless ($opt_d);
#
# Print banner!
#
if (!$opt_d) {
print "\n
TIC TAC TOE\n";
print "TIC-TAC-TOE
\n";
print "\n";
}
if ($move eq "tictactoe") {
$move = "---------";
&shownext( $move );
print "Okay, you start..\n
\n";
exit(0);
}
if (length($move) != 9) {
print "!$move! - illegal move !$save!", length($move), "
\n";
exit(1);
}
sub iswinner
{
local( $pos, $man ) = @_;
local( @m, $i );
# print "iswinner( $pos, $man)\n";
@m = split( //, $pos );
foreach $w ( @win ) {
@x = split(//, $w);
#
# --x--x--x
# xoxoox-ox
#
win: {
for ( $i = 0; $i < 9; $i++ ) {
if (@x[$i] eq "-") { next; }
if (@m[$i] ne $man) { last win; }
}
return( 1 );
}
}
return( 0 );
}
# xxoooxxxo
sub printref
{
local( $board, $x ) = @_;
#
if ($opt_d) {
print "[", $x, "]";
print "($board) ";
}
else {
# when the game is over, the board should
# no longer respond to any input
#
print "" unless ($done);
print "\n";
}
}
sub nextmove
{
local( $board, $x ) = @_;
local( @t, $crud );
@t = split( //, $board );
if ( @t[$x] eq "-" ) {
@t[$x] = "x";
$crud = join( '', @t );
&printref( $crud, "-" );
}
else {
&printref( $board, @t[$x] );
}
}
sub shownext
{
local( $move ) = @_;
local( $j );
#
for ( $j = 0; $j<3; $j++ ) {
&nextmove( $move, $j*3+0 );
&nextmove( $move, $j*3+1 );
&nextmove( $move, $j*3+2 );
#
if ($opt_d) { print "\n"; }
else { print "
\n"; }
}
}
sub showboard
{
local( $move ) = @_;
local( $j, @m );
#
@m = split( //, $move );
for ( $j = 0; $j<3; $j++ ) {
&printref( $move, @m[$j*3+0] );
&printref( $move, @m[$j*3+1] );
&printref( $move, @m[$j*3+2] );
#
if ($opt_d) { print "\n"; }
else { print "
\n"; }
}
}
#
# returns 1 if we have to move to block
# him from winning. modifies @m
#
sub block
{
local( $k, @j );
for ( $k=0; $k<9; $k++ ) {
if (@m[$k] eq "-") {
@j = @m;
@j[$k] = "x";
if (&iswinner(join('', @j ), "x")) {
@m[$k] = "o";
print "blocked ya\n" if ($opt_d);
return(1);
}
}
}
return(0);
}
# did he win?
if (&iswinner($move, "x")) {
$done = 1;
&showboard( $move );
print "\nYOU WON!!\n
\n";
print $newgame;
exit(0);
}
# try each blank and see if that would
# be a win for us
@m = split( //, $move );
for ( $k=0; $k<9; $k++ ) {
if (@m[$k] eq "-") {
@j = @m;
@j[$k] = "o";
if (&iswinner(join('', @j ), "o")) {
$move = join( '', @j );
$done = 1;
&showboard( $move );
print "\nSorry, you lost\n
\n";
print $newgame;
exit(0);
}
}
}
# how many blank spaces?
$n = 0;
foreach (@m) {
if ($_ eq "-") { $n++; }
}
# zero moves is stalemate
# if there's one move left, we can't win with
# that either, otherwise we would have detected
# it above
if ($n <= 1) {
$done = 1;
for ( $i=0; $i<9; $i++ ) {
if (@m[$i] eq "-") { @m[$i]="o"; }
}
$move = join( '', @m );
&showboard( $move );
print "We'll call it a draw\n
\n";
print $newgame;
exit(0);
}
#
# okay, time for us to make our move!
# first, check if he is about to win. if so,
# we'll need to block him.
if (!&block) {
# pick a random square {0..$n-1}
srand;
$y = int(rand($n));
# and place a piece on that square
#print "y is <$y>\n";
#print "move is <$move>\n";
$j = 0; $k = 0;
for ( $j=0; $j<9; $j++ ) {
if ( @m[$j] eq "-") {
if ( $k == $y ) { @m[$j] = "o"; }
$k++;
}
}
}
$move = join( '', @m );
print "after computer move: $move\n" if ($opt_d);
&shownext( $move );
print "Your move..\n
\n";
exit(0);
#
#TIC TAC TOE
#
#TIC TAC TOE
#
#
#
#
#