Skip to content

Commit

Permalink
Updated to PHP Chess 1.4.38
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Jun 15, 2024
1 parent 0fdd2a6 commit dfbcb6f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/Command/RandomizerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ public function run(ChesslaBlabSocket $socket, array $argv, int $id)
$color = array_key_first($items);
$pieceIds = str_split(current($items));
if ($pieceIds === ['B', 'B']) {
$board = (new TwoBishopsRandomizer($argv[1]))->getBoard();
$board = (new TwoBishopsRandomizer($argv[1]))->board;
} elseif ($pieceIds === ['P']) {
$board = (new PawnEndgameRandomizer($argv[1]))->getBoard();
$board = (new PawnEndgameRandomizer($argv[1]))->board;
} else {
$board = (new Randomizer($argv[1], [$color => $pieceIds]))->getBoard();
$board = (new Randomizer($argv[1], [$color => $pieceIds]))->board;
}
} else {
$wIds = str_split($items[Color::W]);
$bIds = str_split($items[Color::B]);
$board = (new Randomizer($argv[1], [
Color::W => $wIds,
Color::B => $bIds,
]))->getBoard();
]))->board;
}
return $socket->getClientStorage()->sendToOne($id, [
$this->name => [
'turn' => $board->getTurn(),
'turn' => $board->turn,
'fen' => $board->toFen(),
],
]);
Expand Down
7 changes: 3 additions & 4 deletions src/Command/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,17 @@ public function run(ChesslaBlabSocket $socket, array $argv, int $id)
$sanPlay = new SanPlay($settings->movetext, $board);
}
$sanPlay->validate();
$board = $sanPlay->getBoard();
$sanMode = new SanMode(new Game($argv[1], $argv[2]), [$id]);
$game = $sanMode->getGame()->setBoard($board);
$game = $sanMode->getGame()->setBoard($sanPlay->board);
$sanMode->setGame($game);
$socket->getGameModeStorage()->set($sanMode);
return $socket->getClientStorage()->sendToOne($id, [
$this->name => [
'variant' => $argv[1],
'mode' => $argv[2],
'turn' => $game->state()->turn,
'movetext' => $sanPlay->getSanMovetext()->validate(),
'fen' => $sanPlay->getFen(),
'movetext' => $sanPlay->sanMovetext->validate(),
'fen' => $sanPlay->fen,
...($argv[1] === Game::VARIANT_960
? ['startPos' => $settings->startPos]
: []
Expand Down
4 changes: 2 additions & 2 deletions src/Game/AbstractMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function res($argv, $cmd)
$options = json_decode(stripslashes($argv[1]), true);
$params = json_decode(stripslashes($argv[2]), true);
$computer = $this->game->computer($options, $params);
if ($computer->pgn) {
$this->game->play($this->game->state()->turn, $computer->pgn);
if ($computer['pgn']) {
$this->game->play($this->game->state()->turn, $computer['pgn']);
}
}
return [
Expand Down
29 changes: 15 additions & 14 deletions src/Game/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ public function setBoard(ClassicalBoard $board): Game
*/
public function state(): object
{
$history = $this->board->getHistory();
$history = $this->board->history;
$end = end($history);

return (object) [
'turn' => $this->board->getTurn(),
'pgn' => $end ? $end->move->pgn : null,
'castlingAbility' => $this->board->getCastlingAbility(),
'movetext' => $this->board->getMovetext(),
'turn' => $this->board->turn,
'pgn' => $end ? $end['move']['pgn'] : null,
'castlingAbility' => $this->board->castlingAbility,
'movetext' => $this->board->movetext(),
'fen' => $this->board->toFen(),
'isCapture' => $end ? $end->move->isCapture : false,
'isCapture' => $end ? $end['move']['isCapture'] : false,
'isCheck' => $this->board->isCheck(),
'isMate' => $this->board->isMate(),
'isStalemate' => $this->board->isStalemate(),
Expand All @@ -152,27 +152,28 @@ public function state(): object
*
* @param array $options
* @param array $params
* @return object|null
* @return array|null
*/
public function computer(array $options = [], array $params = []): ?object
public function computer(array $options = [], array $params = []): ?array
{
if ($this->gmMove) {
if ($move = $this->gmMove->move($this->board)) {
return $move;
}
}

$limit = (new Limit())->setDepth($params['depth']);
$limit = new Limit();
$limit->depth = $params['depth'];
$stockfish = (new UciEngine('/usr/games/stockfish'))->setOption('Skill Level', $options['Skill Level']);
$analysis = $stockfish->analysis($this->board, $limit);

$clone = unserialize(serialize($this->board));
$clone->playLan($this->board->getTurn(), $analysis['bestmove']);
$history = $clone->getHistory();
$clone = $this->board->clone();
$clone->playLan($this->board->turn, $analysis['bestmove']);
$history = $clone->history;
$end = end($history);

return (object) [
'pgn' => $end->move->pgn,
return [
'pgn' => $end['move']['pgn'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Game/PlayMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function updateTimer(string $color)
{
$now = time();
$diff = $now - $this->updatedAt;
if ($this->game->getBoard()->getTurn() === Color::B) {
if ($this->game->getBoard()->turn === Color::B) {
$this->timer[Color::W] -= $diff;
$this->timer[Color::W] += $this->getJwtDecoded()->increment;
} else {
Expand Down

0 comments on commit dfbcb6f

Please sign in to comment.