From 9a0a705607567a0ea0e2aba25343e4a334238619 Mon Sep 17 00:00:00 2001 From: sigmasternchen Date: Thu, 31 Oct 2024 22:31:19 +0100 Subject: [PATCH] fix: Show last move --- html/index.php | 2 ++ html/static/styles.css | 14 +++++++++++--- src/Game/Game.php | 6 +++++- src/Game/GameHistory.php | 15 ++++++++++++++- src/View/fragments/board.php | 8 ++++---- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/html/index.php b/html/index.php index 18bac88..3d59c12 100644 --- a/html/index.php +++ b/html/index.php @@ -3,6 +3,7 @@ require_once '../src/core.php'; use Engine\GameOutcome; +use Engine\MinimaxAStar; use Engine\MinimaxDF; use Engine\PeSTO; use Engine\PieceValues; @@ -17,6 +18,7 @@ if (isset($_SESSION["game"])) { $engine = $_SESSION["engine"]; } else { $game = Game::fromStartPosition(); + //$engine = new MinimaxAStar(1.0, new WeightedHeuristics([ $engine = new MinimaxDF(1, new WeightedHeuristics([ [new GameOutcome(), 1.0], [new PieceValues(), 1.0], diff --git a/html/static/styles.css b/html/static/styles.css index e1c9200..d064391 100644 --- a/html/static/styles.css +++ b/html/static/styles.css @@ -38,12 +38,20 @@ 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; } -.board.interactive:not(.moveSelection) .square.white.hasMoves, .square.white.source { - background-color: greenyellow; +.board.interactive:not(.moveSelection) .square.white.hasMoves .doesnotexist, .square.white.source { + background-color: mediumseagreen; } .piece { diff --git a/src/Game/Game.php b/src/Game/Game.php index 66cb382..bbb3c17 100644 --- a/src/Game/Game.php +++ b/src/Game/Game.php @@ -321,7 +321,7 @@ class Game { $this->current = $this->current->getNext(); $this->moveCache = null; - $this->history->add($this); + $this->history->add($this, $move); } private function isDeadPosition(): bool { @@ -468,6 +468,10 @@ class Game { return $result; } + public function getLastMove(): Move|null { + return $this->history->getLastMove(); + } + public static function fromStartPosition(): Game { return new Game([ new Pawn(new Position(0, 1), Side::WHITE), diff --git a/src/Game/GameHistory.php b/src/Game/GameHistory.php index bd7b564..0d761cf 100644 --- a/src/Game/GameHistory.php +++ b/src/Game/GameHistory.php @@ -8,6 +8,7 @@ if (gettype(2147483648) == "double") { class GameHistory { private array $counts = []; + private array $moves = []; private function hasCastlingRights(array &$rooks, King $king): bool { 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); if (array_key_exists($hash, $this->counts)) { @@ -111,4 +116,12 @@ class GameHistory { $this->counts[$hash] = 1; } } + + public function getLastMove(): Move|null { + if (count($this->moves) > 0) { + return $this->moves[count($this->moves) - 1]; + } else { + return null; + } + } } \ No newline at end of file diff --git a/src/View/fragments/board.php b/src/View/fragments/board.php index 52a99a7..00bfa28 100644 --- a/src/View/fragments/board.php +++ b/src/View/fragments/board.php @@ -28,9 +28,6 @@ function getImageForPice(Piece $piece): string { $piece->getType()->getShort() . ".svg"; } - -echo $interactive; - ?>
" id="board" data-board="" @@ -48,15 +45,18 @@ echo $interactive; > getLastMove(); + for($rank = $start; $rank != $end; $rank += $dir) { for($file = 7-$start; $file != 7-$end; $file -= $dir) { $position = new Position($file, $rank); $piece = $game->getPiece($position); $moves = $piece ? $game->getMovesForPiece($piece) : []; $hasMoves = count($moves) > 0; + $isLastMove = $lastMove && ($lastMove->piece->getPosition()->equals($position) || $lastMove->target->equals($position)); ?>
" + class="square getSquareColor()->name) ?> " data-square="" >