/// <summary> /// Returns a list of positions the pawn can move. /// list contain strings => "xPosition yPosition" /// </summary> public List <string> RuleMove(Vector3 globalPosition, GameObject pieceObject, GameObject[,] boardState) { PieceInformation piece = pieceObject.GetComponent <PieceInformation>(); colour = (int)piece.colour; currentZPosition = piece.CurrentZPosition; currentXPosition = piece.CurrentXPosition; board = boardState; // Initialise new list validPositions = new List <string>(); // Check if king is compromised if moving top-right and bottom-left // Or top-left and bottom-right bool forwards = rules.DiagonalCheckBackward(globalPosition, colour); bool backwards = rules.DiagonalCheckForward(globalPosition, colour); // King will be left compromised if piece moves in any valid direction // return empty list if (rules.ColumnCheck(globalPosition, colour) || rules.RowCheck(globalPosition, colour)) { return(validPositions); } // Moving up-right or down-left will not leave the king compromised to a chec if (!forwards) { // Possible moves up-right for (int right = currentXPosition + 1, upwards = currentZPosition + 1; upwards <= 7 && right <= 7; upwards++, right++) { if (!StorePosition(right, upwards)) { break; } } // Possible moves bottom-left for (int left = currentXPosition - 1, downwards = currentZPosition - 1; downwards >= 0 && left >= 0; downwards--, left--) { if (!StorePosition(left, downwards)) { break; } } } // Moving up-left or down-right will not leave the king compromised to a check if (!backwards) { // Possible moves up-left for (int left = currentXPosition - 1, upwards = currentZPosition + 1; upwards <= 7 && left >= 0; upwards++, left--) { if (!StorePosition(left, upwards)) { break; } } // Possible moves bottom-right for (int right = currentXPosition + 1, downwards = currentZPosition - 1; downwards >= 0 && right <= 7; downwards--, right++) { if (!StorePosition(right, downwards)) { break; } } } return(validPositions); }
/// <summary> /// Returns a list of positions the pawn can move. /// list contain strings => "xPosition yPosition" /// </summary> public List <string> RuleMove(Vector3 globalPosition, GameObject pieceObject, GameObject[,] boardState) { PieceInformation piece = pieceObject.GetComponent <PieceInformation>(); colour = (int)piece.colour; currentZPosition = piece.CurrentZPosition; currentXPosition = piece.CurrentXPosition; board = boardState; // Initialise new list validPositions = new List <string>(); // Check if king is compromised if pawn is moved up/down // Check if king is compromised if pawn moves top-left/ top-right // Check if king is compromised if pawn moves, from top-left or top-right // (substitute top with bottom if black) bool columnMovement = rules.RowCheck(globalPosition, colour); bool rowMovement = rules.ColumnCheck(globalPosition, colour); bool topLeftMovement = rules.DiagonalCheckForward(globalPosition, colour); bool topRightMovement = rules.DiagonalCheckBackward(globalPosition, colour); // King will be left compromised if pawn moves in the upwards direction // return empty list if (columnMovement) { return(validPositions); } // Z position pawn can move to. // If black, move down, -1 // If white, move up, +1 int change = 1; if (colour == 1) { change = -1; } int displacement = currentZPosition + change; // Check if no piece ahead of the pawn. // Check if moving the piece will not leave the king compromised to a check diagonally // Move allowed if true if (board[displacement, currentXPosition] == null && !(topRightMovement || topLeftMovement)) { StorePosition(currentXPosition, displacement); // If pawn hasn't been moved, check if it can move two positions if (!piece.HasMoved() && board[displacement + change, currentXPosition] == null) { StorePosition(currentXPosition, displacement + change); } } // King will be left compromised if pawn moves diagonally // return list if (rowMovement) { return(validPositions); } // Taking a piece left or right will not leave the king compromised to a check if (!topRightMovement) { // Check if piece in top-right position && top-left position is not outside the bound int right = currentXPosition + 1; if (right <= 7 && board[displacement, right] != null) { StorePosition(right, displacement); } // Check if en passant condition is met // Add position to allowed moves if true if (right <= 7 && rules.EnPassant(globalPosition, new Vector3(1, 0, 0), colour)) { StorePosition(right, displacement); } } if (!topLeftMovement) { // Check if piece in top-left position && top-left position is not outside the bound int left = currentXPosition - 1; if (left >= 0 && board[displacement, left] != null) { StorePosition(left, displacement); } // Check if en passant condition is met // Add position to allowed moves if true if (left >= 0 && rules.EnPassant(globalPosition, new Vector3(-1, 0, 0), colour)) { StorePosition(left, displacement); } } return(validPositions); }
/// <summary> /// Returns a list of positions the pawn can move. /// list contain strings => "xPosition yPosition" /// </summary> public List <string> RuleMove(Vector3 globalPosition, GameObject pieceObject, GameObject[,] boardState) { PieceInformation piece = pieceObject.GetComponent <PieceInformation>(); colour = (int)piece.colour; currentZPosition = piece.CurrentZPosition; currentXPosition = piece.CurrentXPosition; board = boardState; // Initialise new list validPositions = new List <string>(); // Check if king is compromised if moving top-right and bottom-left // Or top-left and bottom-right bool forwards = rules.DiagonalCheckBackward(globalPosition, colour); bool backwards = rules.DiagonalCheckForward(globalPosition, colour); // Check if king is compromised if moving up and down // Or left and right bool rowMovement = rules.ColumnCheck(globalPosition, colour); bool columnMovement = rules.RowCheck(globalPosition, colour); // If compromised diagonally, cannot move up and down, or left and right if (!forwards && !backwards) { // Moving up or down will not leave the king compromised to a check if (!columnMovement) { // Possible moves upwards for (int upwards = currentZPosition + 1; upwards <= 7; upwards++) { if (!StorePosition(currentXPosition, upwards)) { break; } } // Possible moves downwards for (int downwards = currentZPosition - 1; downwards >= 0; downwards--) { if (!StorePosition(currentXPosition, downwards)) { break; } } } // Moving left or right will not leave the king compromised to a check if (!rowMovement) { // Possible moves left side for (int left = currentXPosition - 1; left >= 0; left--) { if (!StorePosition(left, currentZPosition)) { break; } } // Possible moves right side for (int right = currentXPosition + 1; right <= 7; right++) { if (!StorePosition(right, currentZPosition)) { break; } } } } // If compromised up/down or left/right, cannot move diagonally if (!rowMovement && !columnMovement) { // Moving up or down will not leave the king compromised to a check if (!forwards) { // Possible moves up-right for (int right = currentXPosition + 1, upwards = currentZPosition + 1; upwards <= 7 && right <= 7; upwards++, right++) { if (!StorePosition(right, upwards)) { break; } } // Possible moves bottom-left for (int left = currentXPosition - 1, downwards = currentZPosition - 1; downwards >= 0 && left >= 0; downwards--, left--) { if (!StorePosition(left, downwards)) { break; } } } // Moving up or down will not leave the king compromised to a check if (!backwards) { // Possible moves up-left for (int left = currentXPosition - 1, upwards = currentZPosition + 1; upwards <= 7 && left >= 0; upwards++, left--) { if (!StorePosition(left, upwards)) { break; } } // Possible moves bottom-right for (int right = currentXPosition + 1, downwards = currentZPosition - 1; downwards >= 0 && right <= 7; downwards--, right++) { if (!StorePosition(right, downwards)) { break; } } } } return(validPositions); }
/// <summary> /// Returns a list of positions the pawn can move. /// list contain strings => "xPosition yPosition" /// </summary> public List <string> RuleMove(Vector3 globalPosition, GameObject pieceObject, GameObject[,] boardState) { PieceInformation piece = pieceObject.GetComponent <PieceInformation>(); colour = (int)piece.colour; currentZPosition = piece.CurrentZPosition; currentXPosition = piece.CurrentXPosition; board = boardState; // Initialise new list validPositions = new List <string>(); // Check if king is compromised if moving up and down // Or left and right bool rowMovement = rules.ColumnCheck(globalPosition, colour); bool columnMovement = rules.RowCheck(globalPosition, colour); // King will be left compromised if piece moves in any direction // return empty list if (rules.DiagonalCheckForward(globalPosition, colour) || rules.DiagonalCheckBackward(globalPosition, colour)) { return(validPositions); } // Moving up or down will not leave the king compromised to a check if (!columnMovement) { // Possible moves upwards for (int upwards = currentZPosition + 1; upwards <= 7; upwards++) { if (!StorePosition(currentXPosition, upwards)) { break; } } // Possible moves downwards for (int downwards = currentZPosition - 1; downwards >= 0; downwards--) { if (!StorePosition(currentXPosition, downwards)) { break; } } } // Moving left or right will not leave the king compromised to a check if (!rowMovement) { // Possible moves left side for (int left = currentXPosition - 1; left >= 0; left--) { if (!StorePosition(left, currentZPosition)) { break; } } // Possible moves right side for (int right = currentXPosition + 1; right <= 7; right++) { if (!StorePosition(right, currentZPosition)) { break; } } } // All possible moves for the rook added to the list return(validPositions); }