Inspiration

It was a challenge from BroadbandTV and I though that developing even a simple game, is challenging.

What it does

Player plays TicTacToe against the computer. All responses are in JSON format.

How I built it

It was built using Laravel Framework, and as it was to be small and run as easily as possible, no database was used. All session/cache files are stored in files locally.

As there was no indication if this project was to be Player X Computer or Player X Player, I ended up coding Player X Computer, as it is more challenging.

Challenges I ran into

Calculate the computer's next move.

What's next for TicTacToe

  • Add support for multiplayer;
  • Add support for Ultimate TicTacToe;
  • Add PHP Unit tests.

Running TicTacToe

unzip tictactoe.zip
cd tictactoe
php artisan serve --port=8000

If using source from GitHub:

composer install
php artisan serve --port=8000

Playing using curl

1) Start a new session:

curl http://localhost:8000/start

If you want to choose a side (X or O, X is the default side), post data:

curl --data "side=O" http://localhost:8000/start

The response will return the sessionId for that game instance.

2) Choose a field and play:

curl --data "sessionId={SESSIONID}&field={FIELD}" http://localhost:8000/play

Example:

curl --data "sessionId=8e49a5350b188822ee466bb537696df4&field=b3" http://localhost:8000/play

3) You can forfeit:

curl --data "sessionId={SESSIONID}" http://localhost:8000/forfeit

Example:

curl --data "sessionId=8e49a5350b188822ee466bb537696df4" http://localhost:8000/forfeit

4) To check game status:

curl --data "sessionId={SESSIONID}" http://localhost:8000/status

Example:

curl --data "sessionId=8e49a5350b188822ee466bb537696df4" http://localhost:8000/status

5) Show help

curl http://localhost:8000/

Game information

The board was designed this way and accepts the following field names:

a1 | a2 | a3
------------
b1 | b2 | b3
------------
c1 | c2 | c3

Relevant files

  • app/Http/Controllers/SessionControler.php
  • app/Http/Controllers/TicTacToeControler.php
  • app/Http/Middleware/SessionValidator.php
  • app/Http/routes.php
  • app/Model/TicTacToeBoard.php
  • app/Utils/TicTacToeUtils.php

Tools used

  • PHP 5.5.34
  • Eclipse Mars 4.5.2
  • curl 7.43.0
  • composer 1.2-dev
  • Mac OS X El Capitan 10.11.5

Built With

Share this project:

Updates