mirror of
https://github.com/sigmasternchen/php-chess
synced 2025-03-15 07:58:54 +00:00
feat: Random Engine
This commit is contained in:
parent
4aace35222
commit
ff8f74fb15
3 changed files with 30 additions and 0 deletions
|
@ -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
10
src/Engine/Engine.php
Normal 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
13
src/Engine/Random.php
Normal 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)];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue