Esempio n. 1
0
        // This utility function will move a piece from the selected square to the target square.
        // It will return any chess piece that was captured from the target square.
        public AbstractChessPiece MoveChessPiece(ChessSquare selectedSquare, ChessSquare clickedSquare)
        {
            // save any piece that is on the target square
            AbstractChessPiece capturedPiece = clickedSquare.ChessPiece;

            // move the piece from the original square to the target square
            clickedSquare.SetChessPiece(selectedSquare.ChessPiece);

            // mark the chess piece as having been moved, in case the piece needs to know (e.g. Pawn)
            selectedSquare.ChessPiece.HasMoved = true;

            // remove the chess piece from the original square
            selectedSquare.RemoveChessPiece();

            // Unselect the currently selected square
            selectedSquare.Unselect();

            // return the captured piece, if any (or null if no piece was captured)
            return(capturedPiece);
        }
Esempio n. 2
0
        // This utility function will look at a proposed move from the selectedSquare to
        // the clickedSquare and determine if the specified player would be in check after
        // the move.  The actual gameboard itself is left unchanged.
        public bool TestMoveForCheck(PlayerType currentPlayer, ChessSquare selectedSquare, ChessSquare clickedSquare)
        {
            // save the piece that may be on the target square
            AbstractChessPiece capturedPiece = clickedSquare.ChessPiece;

            // temporarily put the original piece on the target square
            clickedSquare.SetChessPiece(selectedSquare.ChessPiece);
            // temporarily remove the original piece from the original square
            selectedSquare.RemoveChessPiece();

            // see if the specified player is in check
            bool isInCheck = IsInCheck(currentPlayer);

            // restore the moved piece to the original square
            selectedSquare.SetChessPiece(clickedSquare.ChessPiece);

            // restore the original contents of the target square
            clickedSquare.SetChessPiece(capturedPiece);

            // return true if the current player would still be in check, or false otherwise
            return(isInCheck);
        }