From 8cd807b7222e72263e30fff29f06e9def77d0044 Mon Sep 17 00:00:00 2001 From: programarivm Date: Mon, 29 Apr 2024 12:31:20 +0200 Subject: [PATCH] Updated to PHP Chess 1.4.19 --- src/Command/StartCommand.php | 2 +- src/Game/AbstractMode.php | 6 +++--- src/Game/Game.php | 33 +++++++++++++++++++------------- src/Socket/ChesslaBlabSocket.php | 16 ++++++++-------- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/Command/StartCommand.php b/src/Command/StartCommand.php index 386fdc1d..3b929ed3 100644 --- a/src/Command/StartCommand.php +++ b/src/Command/StartCommand.php @@ -312,7 +312,7 @@ public function run(ChesslaBlabSocket $socket, array $argv, int $id) $board = (new ClassicalFenStrToBoard($settings->fen))->create(); $game = (new Game($argv[1], $argv[2]))->setBoard($board); } else { - $game = new Game($argv[1], $argv[2], $socket->getGm()); + $game = new Game($argv[1], $argv[2], $socket->getGmComputer()); } $stockfishMode = new StockfishMode($game, [$id]); $socket->getGameModeStorage()->set($stockfishMode); diff --git a/src/Game/AbstractMode.php b/src/Game/AbstractMode.php index 9ab7dfa7..94ec9220 100644 --- a/src/Game/AbstractMode.php +++ b/src/Game/AbstractMode.php @@ -99,9 +99,9 @@ public function res($argv, $cmd) if (!$this->game->state()->isMate && !$this->game->state()->isStalemate) { $options = json_decode(stripslashes($argv[1]), true); $params = json_decode(stripslashes($argv[2]), true); - $ai = $this->game->ai($options, $params); - if ($ai->move) { - $this->game->play($this->game->state()->turn, $ai->move); + $computer = $this->game->computer($options, $params); + if ($computer->pgn) { + $this->game->play($this->game->state()->turn, $computer->pgn); } } return [ diff --git a/src/Game/Game.php b/src/Game/Game.php index 07ed8e15..6bf30597 100644 --- a/src/Game/Game.php +++ b/src/Game/Game.php @@ -2,7 +2,7 @@ namespace ChessServer\Game; -use Chess\Grandmaster; +use Chess\Computer\GrandmasterComputer; use Chess\UciEngine\UciEngine; use Chess\UciEngine\Details\Limit; use Chess\Variant\Capablanca\Board as CapablancaBoard; @@ -15,7 +15,7 @@ /** * Game * - * @author Jordi BassagaƱas + * @author Jordi BassagaƱa * @license GPL */ class Game @@ -52,20 +52,27 @@ class Game private string $mode; /** - * Grandmaster. + * Grandmaster computer. * - * @var Grandmaster + * @var \Chess\Computer\GrandmasterComputer */ - private null|Grandmaster $gm; + private null|GrandmasterComputer $gmComputer; + /** + * Constructor. + * + * @param string $variant + * @param string $mode + * @param GrandmasterComputer|null \Chess\Computer\GrandmasterComputer|null + */ public function __construct( string $variant, string $mode, - null|Grandmaster $gm = null + null|GrandmasterComputer $gmComputer = null ) { $this->variant = $variant; $this->mode = $mode; - $this->gm = $gm; + $this->gmComputer = $gmComputer; if ($this->variant === self::VARIANT_960) { $startPos = (new Chess960StartPosition())->create(); @@ -81,7 +88,7 @@ public function __construct( } /** - * Returns the Chess\Board object. + * Returns the chess board object. * * @return \Chess\Variant\Classical\Board */ @@ -111,7 +118,7 @@ public function getMode(): string } /** - * Sets the Chess\Board object. + * Sets the chess board object. * * @param \Chess\Variant\Classical\Board $board * @return \ChessServer\Game @@ -155,10 +162,10 @@ public function state(): object * @param array $params * @return object|null */ - public function ai(array $options = [], array $params = []): ?object + public function computer(array $options = [], array $params = []): ?object { - if ($this->gm) { - if ($move = $this->gm->move($this->board)) { + if ($this->gmComputer) { + if ($move = $this->gmComputer->move($this->board)) { return $move; } } @@ -173,7 +180,7 @@ public function ai(array $options = [], array $params = []): ?object $end = end($history); return (object) [ - 'move' => $end->move->pgn, + 'pgn' => $end->move->pgn, ]; } diff --git a/src/Socket/ChesslaBlabSocket.php b/src/Socket/ChesslaBlabSocket.php index f1c9517f..9d5d8f08 100644 --- a/src/Socket/ChesslaBlabSocket.php +++ b/src/Socket/ChesslaBlabSocket.php @@ -2,7 +2,7 @@ namespace ChessServer\Socket; -use Chess\Grandmaster; +use Chess\Computer\GrandmasterComputer; use ChessServer\Command\CommandParser; use ChessServer\Game\GameModeStorage; @@ -24,11 +24,11 @@ class ChesslaBlabSocket protected CommandParser $parser; /** - * Chess grandmaster. + * Grandmaster computer. * - * @var \Chess\Grandmaster + * @var \Chess\Computer\GrandmasterComputer */ - protected Grandmaster $gm; + protected GrandmasterComputer $gmComputer; /** * Game modes. @@ -50,7 +50,7 @@ class ChesslaBlabSocket public function __construct() { $this->parser = new CommandParser(); - $this->gm = new Grandmaster(self::DATA_FOLDER.'/players.json'); + $this->gmComputer = new GrandmasterComputer(self::DATA_FOLDER.'/players.json'); $this->gameModeStorage = new GameModeStorage(); echo "Welcome to PHP Chess Server" . PHP_EOL; @@ -67,13 +67,13 @@ public function init(ClientStorageInterface $clientStorage): ChesslaBlabSocket } /** - * Returns the chess grandmaster. + * Returns the grandmaster computer. * * @return string */ - public function getGm(): Grandmaster + public function getGmComputer(): GrandmasterComputer { - return $this->gm; + return $this->gmComputer; } /**