/// <summary> /// Checks if a player is mated (no legal moves) /// </summary> /// <param name="color">Color of player to check</param> /// <param name="positionIsStaleMate">Set to true on stalemate</param> /// <returns>True if there are no legal moves for the player color</returns> private bool PlayerMated(PieceColor color, out bool positionIsStaleMate) { positionIsStaleMate = false; bool result = true; List <ChessPiece> pieces = (color == PieceColor.White) ? board.WhitePieces : board.BlackPieces; foreach (ChessPiece piece in pieces) { if (!piece.Captured && LegalChessMovesGenerator.GetLegalMoves(piece, board).Count() > 0) { result = false; break; } } if (result) { // Verify the King is in check, if not, stalemate ChessPiece king = board.GetKing(color); if (!LegalChessMovesGenerator.IsSquareInCheck(board, king.File, king.Rank, color)) { positionIsStaleMate = true; } } return(result); }
/// <summary> /// Should only be called after a game over event, no draws right now in normal /// play so find the mated player, or possibly the stalemate. /// </summary> /// <returns>GameResult value</returns> public GameResult GetWinner() { bool whiteStalemate = false; bool whiteMated = PlayerMated(PieceColor.White, out whiteStalemate); bool blackStalemate = false; bool blackMated = PlayerMated(PieceColor.Black, out blackStalemate); bool activeMated = (ActivePlayer == PieceColor.White) ? whiteMated : blackMated; bool opponentMated = (ActivePlayer == PieceColor.White) ? blackMated : whiteMated; bool activeStalemate = (ActivePlayer == PieceColor.White) ? whiteStalemate : blackStalemate; ChessPiece playerKing = board.GetKing(PieceColor.White); if (LegalChessMovesGenerator.IsSquareInCheck(board, playerKing.File, playerKing.Rank, playerKing.Color)) { ++checkWhite; } playerKing = board.GetKing(PieceColor.Black); if (LegalChessMovesGenerator.IsSquareInCheck(board, playerKing.File, playerKing.Rank, playerKing.Color)) { ++checkBlack; } if (checkWhite == 3 && typeofGame == TypeGame.ThreeCheck) { return(GameResult.BlackWins); } if (checkBlack == 3 && typeofGame == TypeGame.ThreeCheck) { return(GameResult.WhiteWins); } //Drawn - neither player has legal moves and neither is in check if (!opponentMated && activeMated && activeStalemate) { return(GameResult.Draw); } // Stalemate is stalemate at this point if (whiteStalemate) { return(GameResult.Stalemate); } // Still going....needed to check agains engines that behave questionably // in drawn and or mated positions if (!opponentMated && !activeMated) { return(GameResult.Contested); } // we must have an actual winner return(whiteMated ? GameResult.BlackWins : GameResult.WhiteWins); }
/// <summary> /// Process the input when waiting for the current player to select a piece /// </summary> /// <param name="x">x coordinate in form</param> /// <param name="y">y coordinate in form</param> private void OnWaitingForPieceSelection(int x, int y) { ChessPiece foundPiece = ((IChessBoardView)view).GetPiece(x, y); if ((foundPiece != null) && (foundPiece.Color == PlayerColor)) { if (foundPiece.Color == PlayerColor) { legalMoves = LegalChessMovesGenerator.GetLegalMoves(foundPiece, board); ((IChessBoardView)view).HighlightSquares(ref legalMoves); ((IChessBoardView)view).HighlightSquare(foundPiece.File, foundPiece.Rank); selectedPiece = foundPiece; } } }