fix: Show last move

This commit is contained in:
sigmasternchen 2024-10-31 22:31:19 +01:00
parent 3b3d6904c2
commit 9a0a705607
5 changed files with 36 additions and 9 deletions

View file

@ -3,6 +3,7 @@
require_once '../src/core.php'; require_once '../src/core.php';
use Engine\GameOutcome; use Engine\GameOutcome;
use Engine\MinimaxAStar;
use Engine\MinimaxDF; use Engine\MinimaxDF;
use Engine\PeSTO; use Engine\PeSTO;
use Engine\PieceValues; use Engine\PieceValues;
@ -17,6 +18,7 @@ if (isset($_SESSION["game"])) {
$engine = $_SESSION["engine"]; $engine = $_SESSION["engine"];
} else { } else {
$game = Game::fromStartPosition(); $game = Game::fromStartPosition();
//$engine = new MinimaxAStar(1.0, new WeightedHeuristics([
$engine = new MinimaxDF(1, new WeightedHeuristics([ $engine = new MinimaxDF(1, new WeightedHeuristics([
[new GameOutcome(), 1.0], [new GameOutcome(), 1.0],
[new PieceValues(), 1.0], [new PieceValues(), 1.0],

View file

@ -38,12 +38,20 @@
transform: translate(-50%, -50%); transform: translate(-50%, -50%);
} }
.board.interactive:not(.moveSelection) .square.black.hasMoves, .square.black.source { .square.white.lastMove {
background-color: lightgreen;
}
.square.black.lastMove {
background-color: yellowgreen;
}
.board.interactive:not(.moveSelection) .square.black.hasMoves .doesnotexist, .square.black.source {
background-color: green; background-color: green;
} }
.board.interactive:not(.moveSelection) .square.white.hasMoves, .square.white.source { .board.interactive:not(.moveSelection) .square.white.hasMoves .doesnotexist, .square.white.source {
background-color: greenyellow; background-color: mediumseagreen;
} }
.piece { .piece {

View file

@ -321,7 +321,7 @@ class Game {
$this->current = $this->current->getNext(); $this->current = $this->current->getNext();
$this->moveCache = null; $this->moveCache = null;
$this->history->add($this); $this->history->add($this, $move);
} }
private function isDeadPosition(): bool { private function isDeadPosition(): bool {
@ -468,6 +468,10 @@ class Game {
return $result; return $result;
} }
public function getLastMove(): Move|null {
return $this->history->getLastMove();
}
public static function fromStartPosition(): Game { public static function fromStartPosition(): Game {
return new Game([ return new Game([
new Pawn(new Position(0, 1), Side::WHITE), new Pawn(new Position(0, 1), Side::WHITE),

View file

@ -8,6 +8,7 @@ if (gettype(2147483648) == "double") {
class GameHistory { class GameHistory {
private array $counts = []; private array $counts = [];
private array $moves = [];
private function hasCastlingRights(array &$rooks, King $king): bool { private function hasCastlingRights(array &$rooks, King $king): bool {
foreach ($rooks as $rook) { foreach ($rooks as $rook) {
@ -102,7 +103,11 @@ class GameHistory {
} }
} }
public function add(Game $game): void { public function add(Game $game, ?Move $move = null): void {
if ($move) {
$this->moves[] = $move;
}
$hash = $this->getHashForGame($game); $hash = $this->getHashForGame($game);
if (array_key_exists($hash, $this->counts)) { if (array_key_exists($hash, $this->counts)) {
@ -111,4 +116,12 @@ class GameHistory {
$this->counts[$hash] = 1; $this->counts[$hash] = 1;
} }
} }
public function getLastMove(): Move|null {
if (count($this->moves) > 0) {
return $this->moves[count($this->moves) - 1];
} else {
return null;
}
}
} }

View file

@ -28,9 +28,6 @@ function getImageForPice(Piece $piece): string {
$piece->getType()->getShort() . $piece->getType()->getShort() .
".svg"; ".svg";
} }
echo $interactive;
?> ?>
<div class="board <?= $interactive ? "interactive" : "" ?>" id="board<?= $boardId ?>" <div class="board <?= $interactive ? "interactive" : "" ?>" id="board<?= $boardId ?>"
data-board="<?= $boardId ?>" data-board="<?= $boardId ?>"
@ -48,15 +45,18 @@ echo $interactive;
<?php } ?> <?php } ?>
> >
<?php <?php
$lastMove = $game->getLastMove();
for($rank = $start; $rank != $end; $rank += $dir) { for($rank = $start; $rank != $end; $rank += $dir) {
for($file = 7-$start; $file != 7-$end; $file -= $dir) { for($file = 7-$start; $file != 7-$end; $file -= $dir) {
$position = new Position($file, $rank); $position = new Position($file, $rank);
$piece = $game->getPiece($position); $piece = $game->getPiece($position);
$moves = $piece ? $game->getMovesForPiece($piece) : []; $moves = $piece ? $game->getMovesForPiece($piece) : [];
$hasMoves = count($moves) > 0; $hasMoves = count($moves) > 0;
$isLastMove = $lastMove && ($lastMove->piece->getPosition()->equals($position) || $lastMove->target->equals($position));
?> ?>
<div <div
class="square <?= strtolower($position->getSquareColor()->name) ?> <?= $hasMoves ? "hasMoves" : "" ?> <?= $position ?>" class="square <?= strtolower($position->getSquareColor()->name) ?> <?= $hasMoves ? "hasMoves" : "" ?> <?= $isLastMove ? "lastMove" : "" ?> <?= $position ?>"
data-square="<?= $position ?>" data-square="<?= $position ?>"
> >
<?php <?php