protected void CheckMate() { if (KingInCheck.IsWhiteInCheck()) { foreach (Piece whitePiece in GetPieces(board)) { if (whitePiece.GetTeam() == Teams.WHITE_TEAM && whitePiece.CheckLegalMoves(board, whitePiece.LegalMoves(board)).Capacity > 0) { return; } } } if (KingInCheck.IsBlackInCheck()) { foreach (Piece blackPiece in GetPieces(board)) { if (blackPiece.GetTeam() == Teams.BLACK_TEAM && blackPiece.CheckLegalMoves(board, blackPiece.LegalMoves(board)).Capacity > 0) { return; } } } endGameMenu.SetActive(true); print("No legal moves were found, the game is over"); return; }
public List <Vector2> CheckLegalMoves(Board boardClone, List <Vector2> currentLegalMoves) { List <Vector2> newLegalMoves = new List <Vector2>(); Board boardCopy = DeepCopy.Copy(boardClone); bool wasBlackinCheck = KingInCheck.IsBlackInCheck(); bool wasWhiteinCheck = KingInCheck.IsWhiteInCheck(); int selectedPiece = boardClone.GetActivePieces().IndexOf(this); Piece selectedCopy = boardCopy.GetActivePieces()[selectedPiece]; Debug.Log("Current legal moves are: "); foreach (Vector2 move in currentLegalMoves) { //When checkMove is false, the current teams king is not in check and the move is valid if (selectedCopy.CheckMove(boardCopy, move) == false) { newLegalMoves.Add(move); } } KingInCheck.SetBlackCheck(wasBlackinCheck); KingInCheck.SetWhiteCheck(wasWhiteinCheck); return(newLegalMoves); }
protected void MovePieceModel(Vector3 from, Vector3 to) { if (currentlySelectedPiece == null) { return; } moveModel.MovePiece(from, to); TookPiece(); board.Mark(to, currentlySelectedPiece); board.UnMark(from); TogglePromoteMenu(); currentlySelectedPiece.SetPosition(to); movePieceTo = movePieceFrom = Vector3.down; moveTime = Time.time; isPieceMove = true; DrawPiece.ClearHighlight(); drawBoard.ClearHighlights(); board.UpdateBoardThreat(null, Vector2.down); if (KingInCheck.IsBlackInCheck() == true || KingInCheck.IsWhiteInCheck() == true) { CheckMate(); } }
//*****Helper***** public bool CheckMove(Board cloneChessBoard, Vector2 testMove) { cloneChessBoard.UpdateBoardThreat(this, testMove); if (GetTeam() == Teams.WHITE_TEAM) { return(KingInCheck.IsWhiteInCheck()); } else { return(KingInCheck.IsBlackInCheck()); } }
protected void CheckingForCheck() { if (currentlySelectedPiece.GetTeam() == Teams.BLACK_TEAM) { if (KingInCheck.IsBlackInCheck() == true || board.GetSquare(currentlySelectedPiece.GetPiecePosition()).getWhiteThreat() == true || currentlySelectedPiece.GetType() == typeof(King)) { print("Black King in check, or Square is thretened"); legalMovesForAPiece = currentlySelectedPiece.CheckLegalMoves(board, legalMovesForAPiece); } } else { if (KingInCheck.IsWhiteInCheck() == true || board.GetSquare(currentlySelectedPiece.GetPiecePosition()).getBlackThreat() == true || currentlySelectedPiece.GetType() == typeof(King)) { print("White King is in check, or square is threatened"); legalMovesForAPiece = currentlySelectedPiece.CheckLegalMoves(board, legalMovesForAPiece); } } }
public void UpdateBoardThreat(Piece testPiece, Vector2 testMove) { List <Piece> activePieces = new List <Piece>(); activePieces.AddRange(GetActivePieces()); Vector2 originalPosition = Vector2.down; Piece tempRemovedPiece = new Piece(); if (testPiece != null) { originalPosition = testPiece.GetPiecePosition(); if (IsOccupied(testMove)) { tempRemovedPiece = GetPieceAt(testMove); activePieces.Remove(tempRemovedPiece); } testPiece.SetPosition(testMove); Mark(testMove, testPiece); UnMark(originalPosition); } for (int row = BoardConstants.BOARD_MINIMUM; row <= BoardConstants.BOARD_MAXIMUM; row++) { for (int column = BoardConstants.BOARD_MINIMUM; column <= BoardConstants.BOARD_MAXIMUM; column++) { GetSquare(row, column).setBlackThreat(false); GetSquare(row, column).setWhiteThreat(false); } } KingInCheck.SetBlackCheck(false); KingInCheck.SetWhiteCheck(false); foreach (Piece inPlay in activePieces) { foreach (Vector2 moves in inPlay.LegalMoves(this)) { if (inPlay.GetTeam() == Teams.BLACK_TEAM) { squares[(int)moves.x, (int)moves.y].setBlackThreat(true); //GetSquare(moves).setBlackThreat(true); if (GetSquare(moves).isSquareOccupied()) { if (GetSquare(moves).GetPiece().GetType() == typeof(King) && (GetSquare(moves).getBlackThreat() == true)) { KingInCheck.SetWhiteCheck(true); } } } else { squares[(int)moves.x, (int)moves.y].setWhiteThreat(true); //GetSquare(moves).setWhiteThreat(true); if (GetSquare(moves).isSquareOccupied()) { if (GetSquare(moves).GetPiece().GetType() == typeof(King) && (GetSquare(moves).getWhiteThreat() == true)) { KingInCheck.SetBlackCheck(true); } } } } } if (testPiece != null) { testPiece.SetPosition(originalPosition); UnMark(testMove); Mark(originalPosition, testPiece); if (IsOccupied((int)testMove.x, (int)testMove.y)) { activePieces.Add(tempRemovedPiece); } } }