Exemple #1
0
        void CheckCondition()
        {
            // Check the board state for win conditions
            GetMoves();

            GameObject king;

            // Obtain the opponent's king
            if (colour == Colour.White)
            {
                king = boardInfo.GetBlackKing();
            }
            else
            {
                king = boardInfo.GetWhiteKing();
            }

            boardInfo.NextTurn();

            PieceInformation kingInfo     = king.GetComponent <PieceInformation>();
            String           kingPosition = kingInfo.CurrentXPosition + " " + kingInfo.CurrentZPosition;

            if (WinRules.CheckForCheck(possibleMoves, GetComponent <PieceInformation>(), boardInfo, kingPosition))
            {
                return;
            }

            // Check Draw Conditions
            if (WinRules.CheckDraw((int)colour, boardInfo))
            {
                return;
            }
        }
Exemple #2
0
        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();
            }
        }