Tic-Tac-Toe Game Programming with Perl Module Gamestictactoe

Tic Tac Toe game can be also programmed with the perl module from CPAN. Only a few line of code and the game is ready to play. How it can be done?

First assuming the perl is already installed and this is a Windows operating system type ppm from command prompt. This will open Perl Package Manager. Find Games-TicTacToe module and install it using PPM program.

Now you can create the perl script as following:

use strict;
use warnings;
use Games::TicTacToe;
my $tictactoe = Games::TicTacToe->new();
$tictactoe->addPlayer();
while (!$tictactoe->isGameOver())
{ $tictactoe->play(); }

This perl script can be used from command prompt. Here is the example of the screen:

C:\>perl ttt_cpan_console_game.cgi
Please select the player [H – Human, C – Computer]:H
Please select the symbol [X / O]: X
What is your next move [1,2,3,4,5,6,7,8,9]? 3
What is your next move [1,2,4,6,7,8,9]? 1
What is your next move [4,6,7,8,9]? 1
Please make a valid move [4,6,7,8,9]: 6
+———–+
| TicTacToe|
+—+—+—+
| X | O | X |
+—+—+—+
| 4 | O | X |
+—+—+—+
| 7 | O | 9 |
+—+—+—+
Congratulation, Computer won the game. Since it is not easy to convert numbers into 2-dimensional board here is the some modification where the board is showed after each move – so it is easy to choose the number.

use Games::TicTacToe;
my $tictactoe = Games::TicTacToe->new();
$tictactoe->addPlayer();
while (!$tictactoe->isGameOver())
{ $tictactoe->play();
print $tictactoe->{‘board’}->as_string();
}

You can also use print $tictactoe->getGameBoard(); to print the board.
Here is the example of screen:
C:\>perl ttt_cpan_console_game_simple2.cgi
Please select the player [H – Human, C – Computer]: H
Please select the symbol [X / O]: X
What is your next move [1,2,3,4,5,6,7,8,9]? 3
+———–+
| TicTacToe |
+—+—+—+
| 1 | 2 | X |
+—+—+—+
| 4 | 5 | 6 |
+—+—+—+
| 7 | 8 | 9 |
+—+—+—+
+———–+
| TicTacToe |
+—+—+—+
| 1 | 2 | X |
+—+—+—+
| 4 | O | 6 |
+—+—+—+
| 7 | 8 | 9 |
+—+—+—+
What is your next move [1,2,4,6,7,8,9]? 2
+———–+
| TicTacToe |
+—+—+—+
| 1 | X | X |
+—+—+—+
| 4 | O | 6 |
+—+—+—+
| 7 | 8 | 9 |
+—+—+—+
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| 4 | O | 6 |
+—+—+—+
| 7 | 8 | 9 |
+—+—+—+
What is your next move [4,6,7,8,9]? 9
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| 4 | O | 6 |
+—+—+—+
| 7 | 8 | X |
+—+—+—+
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| 4 | O | O |
+—+—+—+
| 7 | 8 | X |
+—+—+—+
What is your next move [4,7,8]? 4
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| X | O | O |
+—+—+—+
| 7 | 8 | X |
+—+—+—+
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| X | O | O |
+—+—+—+
| O | 8 | X |
+—+—+—+
What is your next move [8]? 8
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| X | O | O |
+—+—+—+
| O | X | X |
+—+—+—+
+———–+
| TicTacToe |
+—+—+—+
| O | X | X |
+—+—+—+
| X | O | O |
+—+—+—+
| O | X | X |
+—+—+—+
Game drawn !!!
So only a few lines are needed to create game with CPAN perl module Games::TicTacToe.
However this only 3 by 3 game. Also this is a console based game where the user is playing against computer by entering the moves through answering the questions. Any suggestions, comments and ideas how to make this game web based are welcome.



Leave a Comment