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); } } }