From 01128d6da7244a26f1e02b4bf6232d414a6ac507 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Tue, 5 Jul 2022 09:01:46 +0100 Subject: [PATCH] feat: Win detection --- src/components/Board.tsx | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/Board.tsx b/src/components/Board.tsx index d306fc7..7a9fc1c 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -1,5 +1,5 @@ import { FC, Fragment, useCallback, useEffect, useMemo, useState } from 'react'; -import { encodeFEN, eq, GameState } from '../utils/board'; +import { encodeFEN, eq, GameState, isChecked } from '../utils/board'; import { filterMoves, generateLegalMoves, @@ -91,10 +91,6 @@ const Board: FC = (props) => { const [promotionMove, setPromotionMove] = useState(null); - useEffect(() => { - console.log(currentState); - }, [currentState]); - const performMove = useCallback( (move: Move) => { const { board, enPassantTarget, sideToMove, castling } = makeMove( @@ -115,6 +111,14 @@ const Board: FC = (props) => { [currentState, setBoard, setSideToMove] ); + const gameResult = useMemo(() => { + if (validMoves.length === 0) { + const checked = isChecked(currentState) + return checked[sideToMove] ? 'checkmate' : 'stalemate'; + } + return false; + }, [validMoves, currentState, sideToMove]); + return ( <>