The main goal of the game is to kill all enemy's ships before the enemy kills yours.
I didn't use any frameworks, libraries or anything else, just pure PHP.
I wrote 3 algorithms:
- RandomAI - just shooting randomly without trying to kill damaged ships.
- ImprovedRandomAI - shooting randomly, but when ship is damaged, AI is trying to kill it.
- SmartAI - it's working better than ImprovedRandomAI, but it has a little bit more complicated implementation.
// src/Game.php
public function startNewGame(): void
{
$this->enemyPlayer = new EnemyPlayer(new AI\SmartAI());
// $this->enemyPlayer = new EnemyPlayer(new AI\RandomAI());
// $this->enemyPlayer = new EnemyPlayer(new AI\ImprovedRandomAI());
}
// src/Board/AbstractBoard.php
public const WIDTH = 10;
public const HEIGHT = 10;
// src/Board/ShipBoard.php
public const SHIPS_TO_CREATE = [
["ship size" => 4, "number" => 1],
["ship size" => 3, "number" => 2],
["ship size" => 2, "number" => 3],
["ship size" => 1, "number" => 4],
];
git clone https://github.com/PavelGushchin/seabattle.git
cd seabattle
composer update
php -S localhost:8080 -t public
After that, open http://localhost:8080
in your browser.