Skip to content

Commit

Permalink
Updated to PHP Chess 1.4.19
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Apr 29, 2024
1 parent a33c839 commit 8cd807b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Command/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Game/AbstractMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
33 changes: 20 additions & 13 deletions src/Game/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +15,7 @@
/**
* Game
*
* @author Jordi Bassagañas
* @author Jordi Bassagaña
* @license GPL
*/
class Game
Expand Down Expand Up @@ -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();
Expand All @@ -81,7 +88,7 @@ public function __construct(
}

/**
* Returns the Chess\Board object.
* Returns the chess board object.
*
* @return \Chess\Variant\Classical\Board
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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,
];
}

Expand Down
16 changes: 8 additions & 8 deletions src/Socket/ChesslaBlabSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ChessServer\Socket;

use Chess\Grandmaster;
use Chess\Computer\GrandmasterComputer;
use ChessServer\Command\CommandParser;
use ChessServer\Game\GameModeStorage;

Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 8cd807b

Please sign in to comment.