diff --git a/html/index.php b/html/index.php index 654c4a6..228cf4e 100644 --- a/html/index.php +++ b/html/index.php @@ -7,7 +7,7 @@ use Game\Game; $game = Game::fromStartPosition(); $content = function() use ($game) { - require '../src/View/fragments/board.php'; + require '../src/View/fragments/game.php'; }; require '../src/View/base.php'; diff --git a/html/static/styles.css b/html/static/styles.css index df00f08..89c79a8 100644 --- a/html/static/styles.css +++ b/html/static/styles.css @@ -14,6 +14,7 @@ .square { width: 12.5%; height: 12.5%; + position: relative; } .square.black { @@ -24,11 +25,24 @@ background-color: lightgoldenrodyellow; } -.square.black.hasMoves { +.square.movePossible:after { + content: ""; + display: block; + position: absolute; + top: 50%; + left: 50%; + background: green; + width: 25%; + height: 25%; + border-radius: 50%; + transform: translate(-50%, -50%); +} + +.board:not(.moveSelection) .square.black.hasMoves, .square.black.source { background-color: green; } -.square.white.hasMoves { +.board:not(.moveSelection) .square.white.hasMoves, .square.white.source { background-color: greenyellow; } diff --git a/src/Game/Move.php b/src/Game/Move.php index 2de89ae..3b99717 100644 --- a/src/Game/Move.php +++ b/src/Game/Move.php @@ -161,6 +161,16 @@ class Move { return $result; } + public function toJS() { + return join(",", [ + $this->piece->toJS(), + $this->target, + $this->captures ?? "", + $this->promoteTo?->getShort() ?? "", + $this->castleWith ?? "" + ]); + } + public function __toString(): string { return $this->getLong(); } diff --git a/src/Game/Piece.php b/src/Game/Piece.php index 6713d68..95dcd96 100644 --- a/src/Game/Piece.php +++ b/src/Game/Piece.php @@ -47,6 +47,14 @@ abstract class Piece { return $this->getType()->getShort() . $this->getPosition(); } + public function toJS() { + return join("-", [ + $this->getSide()->getShort(), + $this->getType()->getShort(), + $this->getPosition() + ]); + } + private static function getClassForType(PieceType $type): string { switch ($type) { case PieceType::PAWN: diff --git a/src/Game/Side.php b/src/Game/Side.php index 7b7afd7..3b85036 100644 --- a/src/Game/Side.php +++ b/src/Game/Side.php @@ -13,4 +13,12 @@ enum Side { return Side::WHITE; } } + + public function getShort(): string { + if ($this == Side::WHITE) { + return "w"; + } else { + return "b"; + } + } } \ No newline at end of file diff --git a/src/View/fragments/board.php b/src/View/fragments/board.php index ccc7c31..844e496 100644 --- a/src/View/fragments/board.php +++ b/src/View/fragments/board.php @@ -4,11 +4,9 @@ use Game\Game; use Game\Piece; use Game\Position;use Game\Side; -$game ??= new Game([], \Game\Side::WHITE); +$game ??= new Game([], Side::WHITE); -$current = Side::WHITE; - -if (($current ?? Side::WHITE) == Side::WHITE) { +if (($viewSide ?? Side::WHITE) == Side::WHITE) { $start = 7; $end = -1; $dir = -1; @@ -18,27 +16,36 @@ if (($current ?? Side::WHITE) == Side::WHITE) { $dir = 1; } +$interactive ??= false; + +global $boardId; +$boardId = ($boardId ?? 0) + 1; + function getImageForPice(Piece $piece): string { return "/static/pieces/" . - ($piece->getSide() == Side::WHITE ? "w" : "b") . + $piece->getSide()->getShort() . $piece->getType()->getShort() . ".svg"; } ?> -