http://www.perlmonks.org/index.pl?node_id=200787

Description: Fellow monk husoft made this nice tic-tac-toe game.
tic-tac-toe I thought it needed to be colorized to make it easier to play. So here it is.....tic-tac-toe-color
#!/usr/bin/perl
#game by husoft, color by zentara

print "+---------------------------------------+\n";
print "| Names :                               |\n";
print "+---------------------------------------+\n";
print "| Player #1 : ";chomp($pl1 = <STDIN>);
print "| Player #2 : ";chomp($pl2 = <STDIN>);
print "\n\n";

@. = ("0","1","2","3","4","5","6","7","8","9");

#$,=0;$_=$pl1;@s=("O","X");$S="O";$count= 0; 
$,=0;$_=$pl1;@s=("\e[1;31mO\e[0m","\e[1;34mX\e[0m");$S="\e[1;31mO\e[0m
+";$count= 0;

GAME: until ($, == 1)
{
  verify();
  &display();
  $count++;
  print "$_($S) It's your turn! : ";chomp($q=<STDIN>);

  if ($q eq "bye") { print "See you!\n"; last GAME; }
  elsif ($q >= 10) { $count--; print "$_ You lose your turn cause $q i
+s too high!\n"; }
  elsif ($q <= 0) { $count--; print "$_ You lose your turn cause $q is
+ too low!\n"; }
  else
  {
    if ($.[$q] eq $s[0]) { print "There is already an $s[0] in that pl
+ace!\n"; }
    elsif ($.[$q] eq $s[1]) { print "There is already a $s[1] in that 
+place!\n"; }
    else { $.[$q] = $S; print "$_ you made it putting a $S in the plac
+e $q\n"; }
  }
verify();
if ($_ eq $pl1) { $_ = $pl2; $S = $s[1];}
elsif ($_ eq $pl2) { $_ = $pl1; $S = $s[0];}
else { print "Something Went Wrong"; last GAME; }
}

sub verify {
if ($.[1] eq $S && $.[2] eq $S && $.[3] eq $S){print "$_ You won!\nmov
+e[1,2,3]\n";&display();last GAME;}
elsif ($.[1] eq $S && $.[4] eq $S && $.[7] eq $S){print "$_ You won!\n
+move[1,4,7]\n";&display();last GAME;}
elsif ($.[3] eq $S && $.[6] eq $S && $.[9] eq $S){print "$_ You won!\n
+move[3,6,9]\n";&display();last GAME;}
elsif ($.[1] eq $S && $.[5] eq $S && $.[9] eq $S){print "$_ You won!\n
+move[1,5,9]\n";&display();last GAME;}
elsif ($.[2] eq $S && $.[5] eq $S && $.[8] eq $S){print "$_ You won!\n
+move[2,5,8]\n";&display();last GAME;}
elsif ($.[3] eq $S && $.[5] eq $S && $.[7] eq $S){print "$_ You won!\n
+move[3,5,7]\n";&display();last GAME;}
elsif ($.[4] eq $S && $.[5] eq $S && $.[6] eq $S){print "$_ You won!\n
+move[4,5,6]\n";&display();last GAME;}
elsif ($.[7] eq $S && $.[8] eq $S && $.[9] eq $S){print "$_ You won!\n
+move[7,8,9]\n";&display();last GAME;}

if ($count == 9){print "Good game, but, neither of you won!\n";&displa
+y();last GAME;}
}


sub display{
print<<EOT;
+---+---+---+
| $.[1] | $.[2] | $.[3] |
+---+---+---+
| $.[4] | $.[5] | $.[6] |
+---+---+---+
| $.[7] | $.[8] | $.[9] |
+---+---+---+
EOT
}

comment on tic-tac-toe-color
Download Code

Back to Snippets Section