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