コード例 #1
0
        //Moves chess piece from old to new location on the board
        public void movePiece(int oldCol, int oldRow, int newCol, int newRow)
        {
            //Finds the type and colour of piece that we're moving
            chessPieces selectedType   = chessBoard[oldCol, oldRow].Type();
            chessColour selectedColour = chessBoard[oldCol, oldRow].Colour;

            //You can't move a piece if it doesn't exist
            if (selectedType != chessPieces.CLEAR)
            {
                //Variables to update display after enPassent move
                bool enPassant     = false;
                Pawn enPassantPawn = null;
                if (ChessPiece.DblMovePawn != null && chessBoard[oldCol, oldRow].Type() == chessPieces.PAWN)
                {
                    enPassant     = ChessPiece.isEnPassant(oldCol, oldRow, newCol, newRow, chessBoard);
                    enPassantPawn = (Pawn)(ChessPiece.DblMovePawn).Clone();
                }

                //Variables to update display after castling move
                bool castling = false;
                if (chessBoard[oldCol, oldRow].CanCastle && chessBoard[newCol, newRow].CanCastle)
                {
                    castling = ChessPiece.isCastling(chessBoard[oldCol, oldRow], chessBoard[newCol, newRow], chessBoard);
                }

                //move the piece
                ChessPiece.Move(oldCol, oldRow, newCol, newRow, chessBoard);
                selectedType = chessBoard[newCol, newRow].Type();               //type may have changed (pawn->queen), so update

                //Every time a valid move is made, switch turns
                currentGame.changeTurns();

                //DISPLAY - Special case: If we are castling, just update the whole row that the king is on
                if (castling)
                {
                    for (int i = 0; i < MAX_CHESS_SQUARES; i++)
                    {
                        displayPieceAt(i, newRow, chessBoard[i, newRow].Type(), chessBoard[i, newRow].Colour);
                        updateSelectionSquare();
                    }
                }
                else
                {
                    //DISPLAY - Regular Move: Update only the piece that just moved:
                    displayPieceAt(newCol, newRow, selectedType, selectedColour);

                    //DISPLAY - Special case: If the move was enPassent, update the possible pawn locations (+/- one from the pawn that just moved
                    if (enPassant)
                    {
                        displayPieceAt(enPassantPawn.PositionX,
                                       enPassantPawn.PositionY,
                                       chessBoard[enPassantPawn.PositionX, enPassantPawn.PositionY].Type(),
                                       chessBoard[enPassantPawn.PositionX, enPassantPawn.PositionY].Colour);
                    }
                }
            }
        }