Exemple #1
0
        /// <summary>
        /// Generate moves a FlyweightPiece can make.
        /// This method does not varifies if a move puts its own king in check.
        /// This is done in MoveOrganizer when adding a move.
        /// </summary>
        /// <param name="board">Board to generate moves for.</param>
        /// <param name="location">Location of the piece.</param>
        /// <param name="moves">Container class to which all generated moves are added.</param>
        public void GenerateMoves(Board board, Square location, MoveOrganizer moves)
        {
            if (m_color == board.State.ColorToPlay)
            {
                for (int i = 0; i < m_directions.Length; i++)
                {
                    m_iterator.Reset(board, location, m_directions[i]);
                    if (m_iterator.Next())
                    {
                        if (m_iterator.CurrentPiece() == Piece.None &&
                            (m_iterator.CurrentDirection() == Direction.Up || m_iterator.CurrentDirection() == Direction.Down))
                        {
                            //Non hitting moves
                            switch (Board.Rank(m_iterator.CurrentSquare()))
                            {
                            case 0:
                                //Black non hitting promotion move
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackBishop));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackKnight));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackQueen));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackRook));
                                break;

                            case 7:
                                //White non hitting promotion move
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteBishop));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteKnight));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteQueen));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteRook));
                                break;

                            default:
                                //Basic non hitting move
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare()));
                                break;
                            }

                            if ((Board.Rank(m_iterator.CurrentSquare()) == 2 && m_iterator.CurrentDirection() == Direction.Up) ||
                                (Board.Rank(m_iterator.CurrentSquare()) == 5 && m_iterator.CurrentDirection() == Direction.Down))
                            {   //Two squares forward opening move
                                m_iterator.Next();
                                if (m_iterator.CurrentPiece() == Piece.None)
                                {
                                    moves.Add(new Move(board, location, m_iterator.CurrentSquare()));
                                }
                            }
                        }
                        else if (m_iterator.CurrentPiece() != Piece.None && m_iterator.CurrentPieceColor() != m_color &&
                                 m_iterator.CurrentDirection() != Direction.Up && m_iterator.CurrentDirection() != Direction.Down)
                        {
                            //Hitting moves
                            switch (Board.Rank(m_iterator.CurrentSquare()))
                            {
                            case 0:
                                //Black hitting promotion move
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackBishop));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackKnight));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackQueen));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.BlackRook));
                                break;

                            case 7:
                                //White hitting promotion move
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteBishop));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteKnight));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteQueen));
                                moves.Add(new PawnPromotionMove(board, location, m_iterator.CurrentSquare(), Piece.WhiteRook));
                                break;

                            default:
                                //Basic hitting move
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare()));
                                break;
                            }
                        }
                    }
                }

                //EnPassant moves
                if (board.State.EnPassantTarget != Square.None &&
                    Math.Abs(board.State.EnPassantTarget - location) == 1 &&
                    Board.Rank(board.State.EnPassantTarget) == Board.Rank(location))
                {
                    switch (m_color)
                    {
                    case PieceColor.White:
                        moves.Add(new EnPassantCaptureMove(board, location, Board.Position(Board.File(board.State.EnPassantTarget), Board.Rank(location) + 1)));
                        break;

                    case PieceColor.Black:
                        moves.Add(new EnPassantCaptureMove(board, location, Board.Position(Board.File(board.State.EnPassantTarget), Board.Rank(location) - 1)));
                        break;
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Generate moves a FlyweightPiece can make.
        /// This method does not varifies if a move puts its own king in check.
        /// This is done by a move when its carried out.
        /// </summary>
        /// <param name="board">Board to generate moves for.</param>
        /// <param name="location">Location of the piece.</param>
        /// <param name="moves">Container class to which all generated moves are added.</param>
        public void GenerateMoves(Board board, Square location, MoveOrganizer moves)
        {
            if (m_color == board.State.ColorToPlay)
            {
                //Non hitting moves
                m_iterator.Reset(board, location, m_directions[0]);
                if (m_iterator.Next())
                {
                    if (m_iterator.CurrentPiece() == Piece.None)
                    {
                        switch (Board.Rank(m_iterator.CurrentSquare()))
                        {
                        case 0:
                            //Black non hitting promotion move
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackBishop));
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackKnight));
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackQueen));
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackRook));
                            break;

                        case 7:
                            //White non hitting promotion move
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteBishop));
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteKnight));
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteQueen));
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteRook));
                            break;

                        default:
                            //Basic non hitting move
                            moves.Add(new Move(board, location, m_iterator.CurrentSquare()));
                            break;
                        }

                        //Two squares forward opening move
                        if (Board.Rank(m_iterator.CurrentSquare()) == moveAgainRow)
                        {
                            m_iterator.Next();
                            if (m_iterator.CurrentPiece() == Piece.None)
                            {
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare()));
                            }
                        }
                    }
                }

                //Hitting moves to the left and right
                for (int i = 1; i < m_directions.Length; ++i)
                {
                    m_iterator.Reset(board, location, m_directions[i]);
                    if (m_iterator.Next())
                    {
                        if (m_iterator.CurrentPieceColor() == m_opponentColor)
                        {
                            switch (Board.Rank(m_iterator.CurrentSquare()))
                            {
                            case 0:
                                //Black hitting promotion move
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackBishop));
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackKnight));
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackQueen));
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.BlackRook));
                                break;

                            case 7:
                                //White hitting promotion move
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteBishop));
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteKnight));
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteQueen));
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare(), Piece.WhiteRook));
                                break;

                            default:
                                //Basic hitting move
                                moves.Add(new Move(board, location, m_iterator.CurrentSquare()));
                                break;
                            }
                        }
                    }
                }

                //EnPassant moves
                if (Math.Abs(board.State.EnPassantTarget - location) == 1)
                {
                    switch (m_color)
                    {
                    case PieceColor.White:
                        moves.Add(new Move(board, location, Board.Position(Board.File(board.State.EnPassantTarget), Board.Rank(location) + 1), board.State.EnPassantTarget));
                        break;

                    case PieceColor.Black:
                        moves.Add(new Move(board, location, Board.Position(Board.File(board.State.EnPassantTarget), Board.Rank(location) - 1), board.State.EnPassantTarget));
                        break;
                    }
                }
            }
        }