#!/usr/bin/perl # This is the first game I write in Perl!. # The classic Tic-Tac-Toe # What you think about it? print "+---------------------------------------+\n"; print "| Names : |\n"; print "+---------------------------------------+\n"; print "| Player #1 : ";chomp($pl1 = ); print "| Player #2 : ";chomp($pl2 = ); print "\n\n"; @. = ("0","1","2","3","4","5","6","7","8","9"); $,=0;$_=$pl1;@s=("O","X");$S="O";$count= 0; GAME: until ($, == 1) { verify(); write; $count++; print "$_($S) It's your turn! : ";chomp($q=); if ($q eq "bye") { print "See you!\n"; last GAME; } elsif ($q >= 10) { $count--; print "$_ You lose your turn cause $q is 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 place!\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 place $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!\nmove[1,2,3]";last GAME;} elsif ($.[1] eq $S && $.[4] eq $S && $.[7] eq $S){print "$_ You won!\nmove[1,4,7]";last GAME;} elsif ($.[3] eq $S && $.[6] eq $S && $.[9] eq $S){print "$_ You won!\nmove[3,6,9]";last GAME;} elsif ($.[1] eq $S && $.[5] eq $S && $.[9] eq $S){print "$_ You won!\nmove[1,5,9]";last GAME;} elsif ($.[2] eq $S && $.[5] eq $S && $.[8] eq $S){print "$_ You won!\nmove[2,5,8]";last GAME;} elsif ($.[3] eq $S && $.[5] eq $S && $.[7] eq $S){print "$_ You won!\nmove[3,5,7]";last GAME;} elsif ($.[4] eq $S && $.[5] eq $S && $.[6] eq $S){print "$_ You won!\nmove[4,5,6]";last GAME;} elsif ($.[7] eq $S && $.[8] eq $S && $.[9] eq $S){print "$_ You won!\nmove[7,8,9]";last GAME;} if ($count == 9){print "Good game, but, neither of you won!";last GAME;} } format STDOUT = +---+---+---+ | @<| @<| @<| $.[1],$.[2],$.[3] +---+---+---+ | @<| @<| @<| $.[4],$.[5],$.[6] +---+---+---+ | @<| @<| @<| $.[7],$.[8],$.[9] +---+---+---+ .