1: #!/usr/bin/perl
   2: 
   3: # This is the first game I write in Perl!.
   4: # The classic Tic-Tac-Toe
   5: # What you think about it?
   6: 
   7: print "+---------------------------------------+\n";
   8: print "| Names :                               |\n";
   9: print "+---------------------------------------+\n";
  10: print "| Player #1 : ";chomp($pl1 = <STDIN>);
  11: print "| Player #2 : ";chomp($pl2 = <STDIN>);
  12: print "\n\n";
  13: 
  14: @. = ("0","1","2","3","4","5","6","7","8","9");
  15: $,=0;$_=$pl1;@s=("O","X");$S="O";$count= 0;
  16: 
  17: GAME: until ($, == 1)
  18:         {
  19:         verify();
  20:         write;
  21:         $count++;
  22:         print "$_($S) It's your turn! : ";chomp($q=<STDIN>);
  23: 
  24:         if ($q eq "bye") { print "See you!\n"; last GAME; }        
  25:         elsif ($q >= 10) { $count--; print "$_ You lose your turn cause $q is too high!\n"; }
  26:         elsif ($q <= 0) { $count--; print "$_ You lose your turn cause $q is too low!\n"; }
  27:         else 
  28:                 {
  29:                 if ($.[$q] eq $s[0]) { print "There is already an $s[0] in that place!\n"; }
  30:                 elsif ($.[$q] eq $s[1]) { print "There is already a $s[1] in that place!\n"; }
  31:                 else { $.[$q] = $S; print "$_ you made it putting a $S in the place $q\n"; }
  32:                 }
  33: 
  34:         verify();
  35:         if ($_ eq $pl1) { $_ = $pl2; $S = $s[1] }
  36:         elsif ($_ eq $pl2) { $_ = $pl1; $S = $s[0] }
  37:         else { print "Something Went Wrong"; last GAME; }       
  38:         }
  39: 
  40: sub verify {
  41:         if ($.[1] eq $S && $.[2] eq $S && $.[3] eq $S){print "$_ You won!\nmove[1,2,3]";last GAME;}
  42:         elsif ($.[1] eq $S && $.[4] eq $S && $.[7] eq $S){print "$_ You won!\nmove[1,4,7]";last GAME;}
  43:         elsif ($.[3] eq $S && $.[6] eq $S && $.[9] eq $S){print "$_ You won!\nmove[3,6,9]";last GAME;}
  44:         elsif ($.[1] eq $S && $.[5] eq $S && $.[9] eq $S){print "$_ You won!\nmove[1,5,9]";last GAME;}
  45:         elsif ($.[2] eq $S && $.[5] eq $S && $.[8] eq $S){print "$_ You won!\nmove[2,5,8]";last GAME;}
  46:         elsif ($.[3] eq $S && $.[5] eq $S && $.[7] eq $S){print "$_ You won!\nmove[3,5,7]";last GAME;}
  47:         elsif ($.[4] eq $S && $.[5] eq $S && $.[6] eq $S){print "$_ You won!\nmove[4,5,6]";last GAME;}
  48:         elsif ($.[7] eq $S && $.[8] eq $S && $.[9] eq $S){print "$_ You won!\nmove[7,8,9]";last GAME;}
  49:                 
  50:         if ($count == 9){print "Good game, but, neither of you won!";last GAME;}
  51: }
  52: 
  53: format STDOUT =
  54:                         +---+---+---+
  55:                         | @<| @<| @<|
  56:                         $.[1],$.[2],$.[3]
  57:                         +---+---+---+
  58:                         | @<| @<| @<|
  59:                         $.[4],$.[5],$.[6]
  60:                         +---+---+---+
  61:                         | @<| @<| @<|
  62:                         $.[7],$.[8],$.[9]
  63:                         +---+---+---+
  64: .

Replies are listed 'Best First'.
Re: My first game
by zentara (Cardinal) on Sep 24, 2002 at 15:47 UTC
    #!/user/bin/perl should be #!/usr/bin/perl. It would be nice if you could add some color to the players and board, it's hard to follow in black and white. Maybe use some ansi escape sequences.