fix: can make moves after undo

This commit is contained in:
Cozma Rares
2023-08-17 22:30:47 +03:00
parent e51bbcd36c
commit 9d87a344c3
7 changed files with 71 additions and 30 deletions
+3 -2
View File
@@ -13,15 +13,16 @@ const ChessUI: React.FC<InferProps<[typeof ChessBoard, typeof History]>> = (
const [showModal, setShowModal] = useState(true);
const navigate = useNavigate();
const chess = props.chess;
const currentChess = chess.getCurrentBoardPosition();
return (
<>
<div className="w-screen h-screen flex items-center justify-center">
<div className="m-4 flex flex-row gap-4">
<ChessBoard
key={`${props.blackPerspective}` + chess.getFEN()}
key={`${props.blackPerspective}` + currentChess.getFEN()}
{...props}
chess={chess.getCurrentBoardPosition()}
chess={currentChess}
disabled={(props.disabled ?? false) || chess.isGameOver()}
/>
<History {...props} />
+19 -10
View File
@@ -57,9 +57,18 @@ const ChessBoard: React.FC<{
isAttacked: false,
isPromotion: false,
isActive: false,
EndGameIcon: undefined,
endGameIcon: undefined,
}));
const lastMove = chess.getHistory().history.at(-1)?.move;
console.log(chess.getHistory().history.at(-1));
if (lastMove) {
console.log(lastMove);
tileProps[squareIndex(lastMove.from)].isActive = true;
tileProps[squareIndex(lastMove.to)].isActive = true;
}
if (activeTile != -1) {
tileProps[activeTile].isActive = true;
chess.getMovesForSquare(activeTile).forEach(({ to, flags }) => {
@@ -72,13 +81,13 @@ const ChessBoard: React.FC<{
if (chess.isGameOver()) {
const kings = chess.getKings();
if (chess.isDraw()) {
tileProps[kings[COLOR.BLACK]].EndGameIcon = <HalfStar />;
tileProps[kings[COLOR.WHITE]].EndGameIcon = <HalfStar />;
tileProps[kings[COLOR.BLACK]].endGameIcon = <HalfStar />;
tileProps[kings[COLOR.WHITE]].endGameIcon = <HalfStar />;
} else {
const loser = chess.getTurn(),
winner = swapColor(loser);
tileProps[kings[winner]].EndGameIcon = <Crown />;
tileProps[kings[loser]].EndGameIcon = <Hashtag />;
tileProps[kings[winner]].endGameIcon = <Crown />;
tileProps[kings[loser]].endGameIcon = <Hashtag />;
}
}
@@ -93,7 +102,7 @@ const ChessBoard: React.FC<{
if (isNaN(tile)) return;
if (!chess.didUndo() && activeTile != -1 && tileProps[tile].isAttacked) {
if (activeTile != -1 && tileProps[tile].isAttacked) {
const moveObj = {
from: algebraic(activeTile),
to: algebraic(tile),
@@ -180,14 +189,14 @@ const Tile: React.FC<{
isActive: boolean;
isAttacked: boolean;
blackPerspective?: boolean;
EndGameIcon?: ReactNode;
endGameIcon?: ReactNode;
}> = ({
tileNumber,
piece,
isActive,
isAttacked,
blackPerspective,
EndGameIcon,
endGameIcon,
}) => {
const color = squareColor(tileNumber);
@@ -216,9 +225,9 @@ const Tile: React.FC<{
className="max-w-full max-h-full aspect-square"
/>
)}
<Show when={EndGameIcon != undefined}>
<Show when={endGameIcon != undefined}>
<div className="absolute top-0 left-full -translate-x-1/2 -translate-y-1/2 bg-white z-10 rounded-full p-1 shadow-lg shadow-black aspect-square flex justify-center items-center">
{EndGameIcon}
{endGameIcon}
</div>
</Show>
<Show when={isAttacked}>
+1 -1
View File
@@ -20,7 +20,7 @@ const LocalGame = () => {
const rerender = () => aux((prev) => !prev);
const makeMove = (move: Move) => {
if (chess.didUndo()) return;
if (chess.didUndo()) chess.undoMoves();
chess.makeMove(move);
rerender();
+1 -2
View File
@@ -7,7 +7,6 @@ import { socket } from "../utils/socket";
import { CaretLeft, CaretRight, CopyIcon } from "../components/icons";
import Show from "../utils/Show";
import ErrorNorification from "../components/ErrorNotification";
import Modal, { ModalButton } from "../components/Modal";
import ChessUI from "../components/ChessUI";
import { removeLocationState } from "../utils/utils";
import InferProps from "../utils/InferProps";
@@ -106,7 +105,7 @@ const Game = () => {
chess={chess}
makeMove={makeMove}
blackPerspective={color === COLOR.BLACK}
disabled={color !== chess.getTurn()}
disabled={color !== chess.getTurn() || chess.didUndo()}
buttons={buttons}
/>
</Show>