/// <summary>
        /// Checks if at least one piece can block the path or eliminate the piece checking the king - if in check
        /// Or
        /// Checks to see if the opponent can play a move - for stalemate condition
        /// </summary>
        /// <returns> True if checkmate/stalemate </returns>
        static bool CheckmateStalemate(int colour, BoardInformation boardInfo)
        {
            foreach (GameObject pieceOnBoard in boardInfo.GetPieceAvailable())
            {
                PieceInformation pieceOnBoardInfo = pieceOnBoard.GetComponent <PieceInformation>();

                // skip if not opponent's piece
                if ((int)pieceOnBoardInfo.colour == colour)
                {
                    continue;
                }

                pieceOnBoardInfo.GetMoves();
                List <string> allowedPositions = pieceOnBoardInfo.GetPossibleMoves();

                // Break out early if at least one piece can be moved
                if (allowedPositions.Count != 0)
                {
                    string spots = "";
                    for (int i = 0; i < allowedPositions.Count; i++)
                    {
                        spots += allowedPositions[i] + " ";
                    }
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        void Awake()
        {
            GameObject manager = GameObject.Find("GameManager");

            boardInfo   = manager.GetComponent <BoardInformation>();
            pieceAction = manager.GetComponent <PieceAction>();
        }
Ejemplo n.º 3
0
        void Awake()
        {
            manager   = GameObject.Find("GameManager");
            boardInfo = manager.GetComponent <BoardInformation>();
            solvers   = game.GetComponent <BoardSolvers>();

            pause = false;

            currentlyOpenedMenu = homeMenu;
            originalSpot        = homeMenu.transform.localPosition;
        }
        static bool Impossibility(BoardInformation boardInfo)
        {
            // Two kings are left
            if (boardInfo.GetPieceAvailable().Count == 2)
            {
                return(true);
            }

            // Two kings and one bishop/knight
            if (boardInfo.GetPieceAvailable().Count == 3)
            {
                foreach (GameObject piece in boardInfo.GetPieceAvailable())
                {
                    PieceInformation pieceInfo = piece.GetComponent <PieceInformation>();
                    if ((int)pieceInfo.type == 1 || (int)pieceInfo.type == 2)
                    {
                        return(true);
                    }
                }
            }

            // King and Bishop vs King and Bishop
            if (boardInfo.GetPieceAvailable().Count == 4)
            {
                int bishopCount            = 0;
                PieceInformation[] bishops = new PieceInformation[2];
                foreach (GameObject piece in boardInfo.GetPieceAvailable())
                {
                    PieceInformation pieceInfo = piece.GetComponent <PieceInformation>();
                    if ((int)pieceInfo.type == 2)
                    {
                        bishops[bishopCount] = pieceInfo;
                        bishopCount++;
                    }
                }

                if (bishopCount == 2)
                {
                    // Cannot be the same colour
                    if (bishops[0].colour == bishops[1].colour)
                    {
                        return(false);
                    }

                    // Must be on the same colour tile for it to be a draw
                    if (bishops[0].GetOriginalX() != bishops[1].GetOriginalX())
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
        void Awake()
        {
            // Initialising the original and current positions of each piece
            originalXPosition = (int)transform.localPosition.x;
            originalZPosition = (int)transform.localPosition.z;

            CurrentXPosition = originalXPosition;
            CurrentZPosition = originalZPosition;
            currPos          = transform.position;

            manager         = GameObject.Find("GameManager");
            boardInfo       = manager.GetComponent <BoardInformation>();
            chessboard      = GameObject.Find("Chessboard");
            pieceAction     = manager.GetComponent <PieceAction>();
            ghostPickup     = GetComponent <GhostPickup>();
            chessboardLayer = boardInfo.GetChessboardLayer();

            pieceRigidBody = GetComponent <Rigidbody>();
        }
 /// <summary>
 /// Checks if the current board state satisfies the draw conditions
 /// </summary>
 /// <returns></returns>
 public static bool CheckDraw(int colour, BoardInformation boardInfo)
 {
     if (FiftyMoveRule())
     {
         boardInfo.DrawGame("Draw by fifty move rule.");
     }
     else if (Impossibility(boardInfo))
     {
         boardInfo.DrawGame("Draw by impossibility");
     }
     else if (CheckmateStalemate(colour, boardInfo))
     {
         boardInfo.DrawGame("Draw by stalemate");
     }
     else
     {
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// Checks if the player placed the piece on the forfeit tile
        /// </summary>
        /// <returns></returns>
        public static bool CheckForfeit(int type, int colour, GameObject piece, BoardInformation boardInfo)
        {
            // Piece not king
            if (type != 4)
            {
                return(false);
            }

            RaycastHit hit;

            if (Physics.Raycast(piece.transform.position, new Vector3(0, -1, 0), out hit, 1f))
            {
                GameObject pieceCollided = hit.collider.gameObject;
                if (string.Compare(pieceCollided.name, "forfeit tile") == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 8
0
 void Start()
 {
     boardInfo = GameObject.Find("GameManager").GetComponent <BoardInformation>();
     position  = transform.localPosition;
     rotation  = transform.rotation;
 }
Ejemplo n.º 9
0
 // Start is called before the first frame update
 void Start()
 {
     gameManager = GameObject.Find("GameManager");
     boardInfo   = gameManager.GetComponent <BoardInformation>();
     pieceAction = gameManager.GetComponent <PieceAction>();
 }
        /// <summary>
        /// Checks if the last move has checked the opponent's king
        /// </summary>
        /// <param name="pieceInfo"> Piece information for the piece checking the king </param>
        /// <param name="validMoves"> Positions piece can move from it's new location </param>
        /// <param name="colour"> Colour of the side that played the last move </param>
        public static bool CheckForCheck(List <string> validMoves, PieceInformation pieceInfo, BoardInformation boardInfo, string kingPos)
        {
            int type   = (int)pieceInfo.type;
            int colour = (int)pieceInfo.colour;

            if (validMoves.Contains(kingPos))
            {
                int historyIndex = MoveHistory.Instance.Index;
                MoveHistory.Instance.Check[historyIndex] = true;
                boardInfo.Check = true;
                StoreCheckPath(pieceInfo, kingPos);
                if (CheckmateStalemate(colour, boardInfo))
                {
                    boardInfo.Checkmate(colour);
                }
                else
                {
                    boardInfo.CheckDisplay();
                }
                return(true);
            }

            return(false);
        }