/// <summary> /// Localize and return poosition of king on board /// </summary> /// <param name="board"></param> /// <param name="kingColor"></param> /// <returns>position of king</returns> private static Position FindKing(ChessBoard board, ChessEnum.Color kingColor) { Position pos; for (int i = 0; i <= 7; i++) { for (int j = 0; j <= 7; j++) { pos = new Position(i, j); if (!board.IsSquareEmpty(pos) && board.GetPieceFromPosition(pos).figure == Figure.King && board.GetPieceFromPosition(pos).color == kingColor) { return(pos); } } } return(null); }
/// <summary> /// searches for a tie on the basis of non-existing check and number of available moves /// </summary> /// <returns>true, when there is no check and one side has no possible moves, /// false in every other situation </returns> private bool DoesTieExist() { //converting actual chessboard to chessboard type: #region conversion Debug.Log("Searching for CheckMate"); ChessPiece[,] board = new ChessPiece[8, 8]; var sq = unityBoard.gameObject.GetComponent <UnityChessBoard>().squares; var currentPiece = GetComponent <UnityChessPiece>(); var currentSquare = currentPiece.square.name; didMove = currentPiece.didmove; var currentPos = new Position(currentSquare[0] - 'A', currentSquare[1] - '1'); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { var localSquare = sq[i, j].GetComponent <ChessSquare>(); var localFigure = localSquare.figure; if (localFigure != null) { var color = ChessEnum.Color.Black; ////Debug.Log($"name is {localFigure.name}"); if (localFigure.name.Contains("White")) { color = ChessEnum.Color.White; } board[i, j] = new ChessPiece(localFigure.figure, color, localFigure.didmove, localSquare.enPassantPossible); // //Debug.Log($"({i},{j})={board[i, j].figure},{color}"); } else { board[i, j] = null; ////Debug.Log($"({i},{j})=null"); } } } ChessBoard chessBoard = new ChessBoard(board); //arranging array of pieces on board and corresponding positions GameObject[] pieces = GameObject.FindGameObjectsWithTag("Chess Piece"); List <Position> positionsOfPieces = new List <Position>(); UnityChessPiece[] figures = new UnityChessPiece[pieces.Length]; for (int i = 0; i < pieces.Length; i++) { figures[i] = pieces[i].GetComponent <UnityChessPiece>(); currentSquare = figures[i].square.name; positionsOfPieces.Add(new Position(currentSquare[0] - 'A', currentSquare[1] - '1')); } Position[] positions = positionsOfPieces.ToArray(); List <Position> defMoves = new List <Position>(); #endregion turn = GameObject.FindGameObjectWithTag("Turn Order").GetComponent <Turns>().turn; Debug.Log(turn); if (!DoesCheckExist(chessBoard, FindKing(chessBoard, ChessEnum.Color.White)) && turn == ChessEnum.Color.White) { for (int i = 0; i < figures.Length; i++) { UnityChessPiece piece = figures[i].GetComponent <UnityChessPiece>(); if (piece.color == ChessEnum.Color.White) { switch (figures[i].figure) { case Figure.Pawn: defMoves = PossibleMovesPawn(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Knight: defMoves = PossibleMovesKnight(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Bishop: defMoves = PossibleMovesBishop(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Rook: defMoves = PossibleMovesRook(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Queen: defMoves = PossibleMovesQueen(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.King: defMoves = PossibleMovesKing(chessBoard, positions[i], didMove).ToList <Position>(); break; } if (defMoves.Count > 0) { return(false); } defMoves.Clear(); } } Debug.Log("white cannot move"); return(true); //a tie, no check and white cannot move } else if (!DoesCheckExist(chessBoard, FindKing(chessBoard, ChessEnum.Color.Black)) && turn == ChessEnum.Color.Black) { for (int i = 0; i < figures.Length; i++) { if (figures[i].color == ChessEnum.Color.Black) { switch (figures[i].figure) { case Figure.Pawn: defMoves = PossibleMovesPawn(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Knight: defMoves = PossibleMovesKnight(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Bishop: defMoves = PossibleMovesBishop(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Rook: defMoves = PossibleMovesRook(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.Queen: defMoves = PossibleMovesQueen(chessBoard, positions[i], didMove).ToList <Position>(); break; case Figure.King: defMoves = PossibleMovesKing(chessBoard, positions[i], didMove).ToList <Position>(); break; } if (defMoves.Count > 0) { return(false); } defMoves.Clear(); } } Debug.Log("black cannot move"); return(true); //a tie, (no check and black cant move) } else { return(false); //will never be reached } }
private void Start() { turn = ChessEnum.Color.Black; }