mirror of
https://github.com/sigmasternchen/php-chess
synced 2025-03-14 23:58:53 +00:00
fix: Show last move
This commit is contained in:
parent
3b3d6904c2
commit
9a0a705607
5 changed files with 36 additions and 9 deletions
|
@ -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],
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,9 +28,6 @@ function getImageForPice(Piece $piece): string {
|
|||
$piece->getType()->getShort() .
|
||||
".svg";
|
||||
}
|
||||
|
||||
echo $interactive;
|
||||
|
||||
?>
|
||||
<div class="board <?= $interactive ? "interactive" : "" ?>" id="board<?= $boardId ?>"
|
||||
data-board="<?= $boardId ?>"
|
||||
|
@ -48,15 +45,18 @@ echo $interactive;
|
|||
<?php } ?>
|
||||
>
|
||||
<?php
|
||||
$lastMove = $game->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));
|
||||
?>
|
||||
<div
|
||||
class="square <?= strtolower($position->getSquareColor()->name) ?> <?= $hasMoves ? "hasMoves" : "" ?> <?= $position ?>"
|
||||
class="square <?= strtolower($position->getSquareColor()->name) ?> <?= $hasMoves ? "hasMoves" : "" ?> <?= $isLastMove ? "lastMove" : "" ?> <?= $position ?>"
|
||||
data-square="<?= $position ?>"
|
||||
>
|
||||
<?php
|
||||
|
|
Loading…
Reference in a new issue