air-petr/tic-tac-toe-ai

PHP AI for Tic-Tac-Toe playing.

v1.0.3 2022-02-13 08:20 UTC

This package is auto-updated.

Last update: 2024-05-13 12:57:03 UTC


README

PHP AI for a tic-tac-toe game.

Installation

composer require air-petr/tic-tac-toe-ai

How To Use

Player and Board are main classes of this package. In general, you create a board, give it to an AI player and receive it back with a new mark. placeMark is a main function of Player instance.

use AirPetr\TicTacToeAi\Player;
use AirPetr\TicTacToeAi\Board;

$board = new Board();
$player = new Player();

echo $board->toString(); // "_________"

$boardWithMove = $player->placeMark('X', $board);

echo $boardWithMove->toString(); // "__X______"

There are three difficulties of AI players: easy, normal and hard.

  • Easy player does random moves.
  • Hard player is based on a minimax algorithm. He is unbeatable.
  • Normal player is between hard and easy player. He doesn't give you an easy victory, but can be caught by fork. Just like an average human Tic-tac-toe player.
$easyPlayer = Player::easy();
$normalPlayer = Player::normal();
$hardPlayer = Player::hard();

Board instance can be created from various data sources. Use X and O symbols for initialization:

$board1 = Board::createByString('__X___O__');
echo $board1->toString(); // "__X___O__"

$board2 = Board::createByPlainArray(['_', '_', 'X', '_', '_', '_', 'O', '_', '_']);
echo $board2->toString(); // "__X___O__"

$board3 = Board::createByArrayTable([
    ['_', '_', 'X'],
    ['_', '_', '_'],
    ['O', '_', '_']
]);
echo $board3->toString(); // "__X___O__"

Board instance also can be converted to various representations:

$board = Board::createByString('__X___O__');

$board->toString();
// "__X___O__"

$board->toPlainArray();
// ['_', '_', 'X', '_', '_', '_', 'O', '_', '_']

$board->toArrayTable();
// [['_', '_', 'X'], ['_', '_', '_'], ['O', '_', '_']]

Demo

You can play with code in demo folder. Here's how you can run interactive game:

php demo/interactive_game.php

Testing

There are some unit tests in test folder. Run tests by:

composer test