/// <summary> /// Called when a move is made /// </summary> /// <param name="pieceEliminated"> True if the move eliminate's the opponent's piece </param> /// <param name="eliminatedObject"> Opponent's piece that has been eliminated, otherwise null </param> /// <param name="piece"> Piece that was moved </param> /// <param name="originalPos"> Original Position of the piece ("x z")</param> /// <param name="newPos"> New Position of the Piece ("x z")</param> public void Move(bool pieceEliminated, GameObject eliminatedObject, GameObject piece, string originalPos, string newPos) { Eliminated.Add(pieceEliminated); if (pieceEliminated) { EliminatedObjects.Add(eliminatedObject); } else { EliminatedObjects.Add(null); } PieceMoved.Add(piece); PreviousXPosition.Add((int)char.GetNumericValue(originalPos[0])); PreviousZPosition.Add((int)char.GetNumericValue(originalPos[2])); CurrentXPosition.Add((int)char.GetNumericValue(newPos[0])); CurrentZPosition.Add((int)char.GetNumericValue(newPos[2])); // by default, pawnPromoted = false PawnPromoted.Add(false); // by default, check = false Check.Add(false); Count++; }
public ArrayList Undo() { ArrayList undo = new ArrayList(); undo.Add(Eliminated[Index]); undo.Add(EliminatedObjects[Index]); undo.Add(PieceMoved[Index]); undo.Add(PreviousXPosition[Index]); undo.Add(PreviousZPosition[Index]); undo.Add(CurrentXPosition[Index]); undo.Add(CurrentZPosition[Index]); undo.Add(Check[Index]); undo.Add(PawnPromoted[Index]); Eliminated.RemoveAt(Index); EliminatedObjects.RemoveAt(Index); PieceMoved.RemoveAt(Index); PreviousXPosition.RemoveAt(Index); PreviousZPosition.RemoveAt(Index); CurrentXPosition.RemoveAt(Index); CurrentZPosition.RemoveAt(Index); Check.RemoveAt(Index); PawnPromoted.RemoveAt(Index); Count--; return(undo); }
void CheckValid() { // Sandbox mode - Game has ended if (boardInfo.GameEnded) { return; } // Check if king placed on the forfeit tile if (WinRules.CheckForfeit((int)type, (int)colour, gameObject, boardInfo)) { KingForfeit(); return; } // If piece dropped off the board if (transform.localPosition.y <= -0.1f) { FixPosition(); return; } // Declare the new position of the piece int newXPosition = -1; int newZPosition = -1; RaycastHit hit; /// <summary> /// Raycast down to find the tile the player is currently on /// </summary> if (Physics.Raycast(transform.position, -transform.up, out hit, 1f, chessboardLayer)) { GameObject pieceCollided = hit.collider.gameObject; newXPosition = (int)pieceCollided.name[0] - 65; newZPosition = (int)Char.GetNumericValue(pieceCollided.transform.parent.gameObject.name[0]) - 1; } // Since the peice can go through the board // It can miss the raycast (backup) else { newXPosition = (int)Math.Round(transform.localPosition.x); newZPosition = (int)Math.Round(transform.localPosition.z); } string newPosition = newXPosition.ToString() + " " + newZPosition.ToString(); // Check if piece can be moved to this position if (possibleMoves.Contains(newPosition)) { // If player was under check, check = false boardInfo.Check = false; string originalPosition = CurrentXPosition.ToString() + " " + CurrentZPosition.ToString(); // Check if new position has opponent's piece if (board[newZPosition, newXPosition] != null) { // Destroy opponent's piece at new position GameObject opponentPiece = board[newZPosition, newXPosition]; pieceAction.Eliminate(opponentPiece); MoveHistory.Instance.Move(true, opponentPiece, gameObject, originalPosition, newPosition); boardInfo.RemoveFromBoard(opponentPiece); // Reset fifty move to 0 WinRules.FiftyMoves = 0; } // Check if en passant else if (type == Type.Pawn && CurrentXPosition != newXPosition) { // Destroy opponent's pawn GameObject eliminatedPiece = board[CurrentZPosition, newXPosition]; pieceAction.Eliminate(eliminatedPiece); MoveHistory.Instance.Move(true, eliminatedPiece, gameObject, originalPosition, newPosition); board[CurrentZPosition, newXPosition] = null; // reset fifty move to 0 WinRules.FiftyMoves = 0; } else { // Reset fifty move to 0 if pawn moved // Else, +1 if (type == Type.Pawn) { WinRules.FiftyMoves = 0; } else { WinRules.FiftyMoves += 1; } MoveHistory.Instance.Move(false, null, gameObject, originalPosition, newPosition); // Check if king and castling // xDisplacement (- if moving right, + if moving left) int xDisplacement = CurrentXPosition - newXPosition; if (type == Type.King && Math.Abs(xDisplacement) == 2) { Vector3 endPosition; GameObject rook; int initialRookX; int newRookX; // If moved towards right, move rook to the left if (xDisplacement < 0) { initialRookX = 7; newRookX = 5; rook = board[newZPosition, initialRookX]; endPosition = new Vector3(newZPosition, 0, newRookX); } // If moved towards left, move rook to the right else { initialRookX = 0; newRookX = 3; rook = board[newZPosition, initialRookX]; endPosition = new Vector3(newZPosition, 0, newRookX); } // Move the rook to new position pieceAction.ChangePosition(rook, endPosition, (int)colour); boardInfo.UpdateBoard(initialRookX, newZPosition, newRookX, newZPosition); } } boardInfo.UpdateBoard(CurrentXPosition, CurrentZPosition, newXPosition, newZPosition); PieceMoves += 1; if (boardInfo.ghostActive) { StartCoroutine(AnimateMovement(transform.localPosition)); } } // Not a valid move, still the player's turn else { FixPosition(); return; } if (type == Type.Pawn) { // Check if in final tile for pawn promotion if ((colour == Colour.White && newZPosition == 7) || (colour == Colour.Black && newZPosition == 0)) { StartCoroutine(boardInfo.PromotePawn(GetComponent <PieceInformation>())); BeenPromoted = true; } else { ContinueProcess(); } } else { ContinueProcess(); } }