示例#1
0
        /// <summary>
        /// Creates a key that can be used to return an actual "Move" from the "MoveOrganizer"
        /// This method is also responsible for raising the Pawn promotion events.
        /// </summary>
        /// <param name="from">Where to move from.</param>
        /// <param name="to">Where to move to.</param>
        /// <returns>Key matching the requested move.</returns>
        private MoveIdentifier CreateMoveIdentifier(Square from, Square to)
        {
            MoveIdentifier result;

            result.From           = from;
            result.To             = to;
            result.PromotionPiece = Piece.None;

            if (Board.Rank(to) == 7 && m_currentGame.Board[from] == Piece.WhitePawn)
            {
                result.PromotionPiece = Piece.WhiteQueen;
                if (m_currentGame.Moves.Find(result) == null)
                {
                    result.PromotionPiece = Piece.None;
                }
                else if (WhitePawnPromoted != null)
                {
                    PromotionEventArgs promotionArgs = new PromotionEventArgs(PromotionPiece.Queen);
                    WhitePawnPromoted(this, promotionArgs);
                    switch (promotionArgs.PromotionPiece)
                    {
                    case PromotionPiece.Bishop:
                        result.PromotionPiece = Piece.WhiteBishop;
                        break;

                    case PromotionPiece.Knight:
                        result.PromotionPiece = Piece.WhiteKnight;
                        break;

                    case PromotionPiece.Rook:
                        result.PromotionPiece = Piece.WhiteRook;
                        break;
                    }
                }
            }

            if (Board.Rank(to) == 0 && m_currentGame.Board[from] == Piece.BlackPawn)
            {
                result.PromotionPiece = Piece.BlackQueen;
                if (m_currentGame.Moves.Find(result) == null)
                {
                    result.PromotionPiece = Piece.None;
                }
                else if (BlackPawnPromoted != null)
                {
                    PromotionEventArgs promotionArgs = new PromotionEventArgs(PromotionPiece.Queen);
                    BlackPawnPromoted(this, promotionArgs);
                    switch (promotionArgs.PromotionPiece)
                    {
                    case PromotionPiece.Bishop:
                        result.PromotionPiece = Piece.BlackBishop;
                        break;

                    case PromotionPiece.Knight:
                        result.PromotionPiece = Piece.BlackKnight;
                        break;

                    case PromotionPiece.Rook:
                        result.PromotionPiece = Piece.BlackRook;
                        break;
                    }
                }
            }

            return(result);
        }
示例#2
0
        public bool ValidatePosition()
        {
            Board      board = m_editBoard;
            BoardState state = board.State;

            //----Validating piece count----
            byte K = 0; //white kings
            byte k = 0; //black kings
            byte Q = 0; //white queens
            byte q = 0; //black queens
            byte R = 0; //white rooks
            byte r = 0; //black rooks
            byte B = 0; //white bishops
            byte b = 0; //black bishops
            byte N = 0; //white knights
            byte n = 0; //black knights
            byte P = 0; //white pawns
            byte p = 0; //black pawns

            foreach (Square square in board)
            {
                if (board[square] == Piece.WhiteKing)
                {
                    ++K;
                }

                if (board[square] == Piece.BlackKing)
                {
                    ++k;
                }

                if (board[square] == Piece.WhiteQueen)
                {
                    ++Q;
                }

                if (board[square] == Piece.BlackQueen)
                {
                    ++q;
                }

                if (board[square] == Piece.WhiteRook)
                {
                    ++R;
                }

                if (board[square] == Piece.BlackRook)
                {
                    ++r;
                }

                if (board[square] == Piece.WhiteBishop)
                {
                    ++B;
                }

                if (board[square] == Piece.BlackBishop)
                {
                    ++b;
                }

                if (board[square] == Piece.WhiteKnight)
                {
                    ++N;
                }

                if (board[square] == Piece.BlackKnight)
                {
                    ++n;
                }

                if (board[square] == Piece.WhitePawn)
                {
                    ++P;
                }

                if (board[square] == Piece.BlackPawn)
                {
                    ++p;
                }
            }

            if ((K + Q + R + B + N + P) > 16 || (k + q + r + b + n + p) > 16) //total
            {
                return(false);
            }

            if (K != 1 || k != 1) //kings
            {
                return(false);
            }

            if (P > 8 || p > 8) //pawns
            {
                return(false);
            }

            if ((Q + R + B + N - 7) > (8 - P) || (q + r + b + n - 7) > (8 - p)) //ekstra due to promotion
            {
                return(false);
            }


            //----Validate color to play----
            if (!(state.ColorToPlay == PieceColor.White || state.ColorToPlay == PieceColor.Black))
            {
                return(false);
            }


            //----Validate pawn locations----
            for (Square square = Square.A1; square <= Square.H1; ++square)
            {
                Piece piece = board[square];
                if (piece == Piece.WhitePawn || piece == Piece.BlackPawn)
                {
                    return(false);
                }
            }

            for (Square square = Square.A8; square <= Square.H8; ++square)
            {
                Piece piece = board[square];
                if (piece == Piece.WhitePawn || piece == Piece.BlackPawn)
                {
                    return(false);
                }
            }


            //----Validating if a king can be captured----
            if (state.ColorToPlay == PieceColor.White)
            {
                if (board.IsCheck(PieceColor.Black))
                {
                    return(false);
                }
            }

            if (state.ColorToPlay == PieceColor.Black)
            {
                if (board.IsCheck(PieceColor.White))
                {
                    return(false);
                }
            }


            //----Validating En Passant----
            if (state.EnPassantTarget != Square.None)
            {
                if (!(Board.Rank(state.EnPassantTarget) == 3 || Board.Rank(state.EnPassantTarget) == 4))
                {
                    return(false);
                }

                if (state.ColorToPlay == PieceColor.White && board[state.EnPassantTarget] != Piece.BlackPawn)
                {
                    return(false);
                }

                if (state.ColorToPlay == PieceColor.Black && board[state.EnPassantTarget] != Piece.WhitePawn)
                {
                    return(false);
                }
            }


            //----Validating castling----
            if (state.WhiteCanCastleShort)
            {
                if (board[Square.E1] != Piece.WhiteKing || board[Square.H1] != Piece.WhiteRook)
                {
                    return(false);
                }
            }

            if (state.WhiteCanCastleLong)
            {
                if (board[Square.E1] != Piece.WhiteKing || board[Square.A1] != Piece.WhiteRook)
                {
                    return(false);
                }
            }

            if (state.BlackCanCastleShort)
            {
                if (board[Square.E8] != Piece.BlackKing || board[Square.H8] != Piece.BlackRook)
                {
                    return(false);
                }
            }

            if (state.BlackCanCastleLong)
            {
                if (board[Square.E8] != Piece.BlackKing || board[Square.A8] != Piece.BlackRook)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#3
0
 /// <summary>
 /// Returns the rank (or row) the square is located on.
 /// </summary>
 /// <param name="square">Valid arguments are all squares except Square.NoSquare.</param>
 /// <returns>The rank [0..7], viewed from white player, where square is located on the board.</returns>
 public static int Rank(Square square)
 {
     return(Board.Rank(square));
 }