feat: Random Engine

This commit is contained in:
sigmasternchen 2024-10-31 16:59:19 +01:00
parent 4aace35222
commit ff8f74fb15
3 changed files with 30 additions and 0 deletions

View file

@ -9,9 +9,13 @@ session_start();
if (isset($_SESSION["game"])) {
$game = $_SESSION["game"];
$engine = $_SESSION["engine"];
} else {
$game = Game::fromStartPosition();
$engine = new \Engine\Random();
$_SESSION["game"] = $game;
$_SESSION["engine"] = $engine;
}
$content = function() use ($game) {
@ -22,6 +26,9 @@ if (isset($_GET["move"])) {
$move = Move::fromJS($_REQUEST["move"]);
$game->applyInPlace($move);
$opponentMove = $engine->nextMove($game);
$game->applyInPlace($opponentMove);
$content();
} else {
require '../src/View/base.php';

10
src/Engine/Engine.php Normal file
View file

@ -0,0 +1,10 @@
<?php
namespace Engine;
use Game\Game;
use Game\Move;
interface Engine {
public function nextMove(Game $game): Move;
}

13
src/Engine/Random.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Engine;
use Game\Game;
use Game\Move;
class Random implements Engine {
public function nextMove(Game $game): Move {
$legalMoves = $game->getLegalMoves();
return $legalMoves[array_rand($legalMoves, 1)];
}
}