Exemple #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 internal Move(BoardStatus before, int from, int to)
 {
     this.before = before;
     this.after = before;// the after board status is initialized with the before status
     this.from = from;
     this.to = to;
 }
Exemple #2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 internal Move(BoardStatus before, int from, int to)
 {
     this.before = before;
     this.after  = before;// the after board status is initialized with the before status
     this.from   = from;
     this.to     = to;
 }
        /// <summary>
        /// Gets a board in the starting position.
        /// </summary>
        /// <returns></returns>
        public static Board GetStartingBoard()
        {
            Piece[]     squares = new Piece[SquareNo];
            BoardStatus status  = new BoardStatus();

            squares[A8] = new BlackRook();
            squares[B8] = new BlackKnight();
            squares[C8] = new BlackBishop();
            squares[D8] = new BlackQueen();
            squares[E8] = new BlackKing();
            squares[F8] = new BlackBishop();
            squares[G8] = new BlackKnight();
            squares[H8] = new BlackRook();

            for (int sqIndex = SideSquareNo; sqIndex < SideSquareNo * 2; sqIndex++)
            {
                squares[sqIndex] = new BlackPawn();
            }

            for (int sqIndex = SideSquareNo * (SideSquareNo - 2); sqIndex < SideSquareNo * (SideSquareNo - 1); sqIndex++)
            {
                squares[sqIndex] = new WhitePawn();
            }

            squares[A1] = new WhiteRook();
            squares[B1] = new WhiteKnight();
            squares[C1] = new WhiteBishop();
            squares[D1] = new WhiteQueen();
            squares[E1] = new WhiteKing();
            squares[F1] = new WhiteBishop();
            squares[G1] = new WhiteKnight();
            squares[H1] = new WhiteRook();

            status.WhiteTurn            = true;
            status.WhiteCouldCastleLong = status.WhiteCouldCastleShort = status.BlackCouldCastleLong = status.BlackCouldCastleShort = true;
            status.EnPassantTarget      = null;
            status.Ply   = 0;
            status.Moves = 1;

            return(new Board(squares, status));
        }
Exemple #4
0
        /// <summary>
        /// Gets a board from its FEN (Forsyth-Edwards notation).
        /// Throws ArgumentException if it's not a valid position.
        /// </summary>
        /// <param name="fen">The FEN string</param>
        /// <returns></returns>
        public static Board GetFENBoard(string fen)
        {
            try
            {
                // the FEN string must not be null
                if (fen == null)
                {
                    throw new ArgumentNullException("fen", Resources.NullFENMsg);
                }

                Piece[]     squares = new Piece[Board.SquareNo];
                BoardStatus status  = new BoardStatus();

                Piece piece;
                int   index = 0, pos = 0;

                while (fen[index] != ' ')
                {
                    // get the piece from its char representation
                    piece = GetPieceFromChar(fen[index]);

                    // if there is a piece
                    if (piece != null)
                    {
                        squares[pos++] = piece;// put the piece on board
                    }
                    // if there is a number
                    else if (fen[index] >= '1' && fen[index] <= '8')
                    {
                        pos += fen[index] - '0';// skip empty squares
                    }
                    // if there another char than '/' throw exception
                    else if (fen[index] != '/')
                    {
                        throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
                    }

                    index++;
                }

                index++;

                // set side to move
                if (fen[index] == 'w')
                {
                    status.WhiteTurn = true;
                }
                else if (fen[index] == 'b')
                {
                    status.BlackTurn = true;
                }
                else
                {
                    throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
                }

                index += 2;

                // set castling availability
                if (fen[index] == '-')
                {
                    index++;
                }
                else
                {
                    do
                    {
                        switch (fen[index])
                        {
                        case 'K':
                            status.WhiteCouldCastleShort = true;
                            break;

                        case 'Q':
                            status.WhiteCouldCastleLong = true;
                            break;

                        case 'k':
                            status.BlackCouldCastleShort = true;
                            break;

                        case 'q':
                            status.BlackCouldCastleLong = true;
                            break;

                        default:
                            throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
                        }
                    }while (fen[++index] != ' ');
                }

                index++;

                // set en passant target
                if (fen[index] == '-')
                {
                    status.EnPassantTarget = null;
                    index++;
                }
                else
                {
                    // checks if the square notation is correct
                    if (fen[index] < 'a' || fen[index] > 'h' || fen[index + 1] < '1' || fen[index + 1] > '8')
                    {
                        throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
                    }

                    status.EnPassantTarget = GetPosition(fen[index], fen[index + 1]);

                    index += 2;
                }

                index++;

                // set ply
                try
                {
                    status.Ply = Int32.Parse(fen.Substring(index, fen.IndexOf(' ', index) - index), CultureInfo.InvariantCulture);
                }
                catch
                {
                    throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
                }

                index = fen.IndexOf(' ', index) + 1;

                // set move number
                try
                {
                    status.Moves = Int32.Parse(fen.Substring(index), CultureInfo.InvariantCulture);
                }
                catch
                {
                    throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
                }

                Board board = new Board(squares, status);

                // verfies if the board is in valid state, throws ArgumentException if it's not
                VerifyState(board);

                return(board);
            }
            // if the FEN string was too short
            catch (IndexOutOfRangeException)
            {
                throw new ArgumentException(Resources.IllegalFENFormatMsg, "fen");
            }
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="squares"></param>
 /// <param name="status"></param>
 public Board(Piece[] squares, BoardStatus status)
 {
     this.squares = squares;
     this.status  = status;
 }
Exemple #6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 internal PromotionMove(BoardStatus before, int from, int to)
     : base(before, from, to)
 {
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 internal PromotionMove(BoardStatus before, int from, int to)
     : base(before, from, to)
 {
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 /// <param name="rookMove">The rook move</param>
 internal CastlingMove(BoardStatus before, int from, int to, Move rookMove)
     : base(before, from, to)
 {
     this.rookMove = rookMove;
 }
Exemple #9
0
        /// <summary>
        /// Gets a board from its FEN (Forsyth-Edwards notation).
        /// Throws ArgumentException if it's not a valid position.
        /// </summary>
        /// <param name="fen">The FEN string</param>
        /// <returns></returns>
        public static Board GetFENBoard(string fen)
        {
            try
            {
                // the FEN string must not be null
                if (fen == null) { throw new ArgumentNullException("fen", "Resources.NullFENMsg"); }

                Piece[] squares = new Piece[Board.SquareNo];
                BoardStatus status = new BoardStatus();

                Piece piece;
                int index = 0, pos = 0;

                while (fen[index] != ' ')
                {
                    // get the piece from its char representation
                    piece = GetPieceFromChar(fen[index]);

                    // if there is a piece
                    if (piece != null)
                    {
                        squares[pos++] = piece;// put the piece on board
                    }
                    // if there is a number
                    else if (fen[index] >= '1' && fen[index] <= '8')
                    {
                        pos += fen[index] - '0';// skip empty squares
                    }
                    // if there another char than '/' throw exception
                    else if (fen[index] != '/')
                    {
                        throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
                    }

                    index++;
                }

                index++;

                // set side to move
                if (fen[index] == 'w')
                {
                    status.WhiteTurn = true;
                }
                else if (fen[index] == 'b')
                {
                    status.BlackTurn = true;
                }
                else
                {
                    throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
                }

                index += 2;

                // set castling availability
                if (fen[index] == '-')
                {
                    index++;
                }
                else
                {
                    do
                    {
                        switch (fen[index])
                        {
                            case 'K':
                                status.WhiteCouldCastleShort = true;
                                break;
                            case 'Q':
                                status.WhiteCouldCastleLong = true;
                                break;
                            case 'k':
                                status.BlackCouldCastleShort = true;
                                break;
                            case 'q':
                                status.BlackCouldCastleLong = true;
                                break;
                            default:
                                throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
                        }
                    }
                    while (fen[++index] != ' ');
                }

                index++;

                // set en passant target
                if (fen[index] == '-')
                {
                    status.EnPassantTarget = null;
                    index++;
                }
                else
                {
                    // checks if the square notation is correct
                    if (fen[index] < 'a' || fen[index] > 'h' || fen[index + 1] < '1' || fen[index + 1] > '8')
                    {
                        throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
                    }

                    status.EnPassantTarget = GetPosition(fen[index], fen[index + 1]);

                    index += 2;
                }

                index++;

                // set ply
                try
                {
                    status.Ply = Int32.Parse(fen.Substring(index, fen.IndexOf(' ', index) - index), CultureInfo.InvariantCulture);
                }
                catch
                {
                    throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
                }

                index = fen.IndexOf(' ', index) + 1;

                // set move number
                try
                {
                    status.Moves = Int32.Parse(fen.Substring(index), CultureInfo.InvariantCulture);
                }
                catch
                {
                    throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
                }

                Board board = new Board(squares, status);

                // verfies if the board is in valid state, throws ArgumentException if it's not
                VerifyState(board);

                return board;
            }
            // if the FEN string was too short
            catch (IndexOutOfRangeException)
            {
                throw new ArgumentException("Resources.IllegalFENFormatMsg", "fen");
            }
        }
Exemple #10
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="squares"></param>
 /// <param name="status"></param>
 public Board(Piece[] squares, BoardStatus status)
 {
     this.squares = squares;
     this.status = status;
 }
Exemple #11
0
        /// <summary>
        /// Gets a board in the starting position.
        /// </summary>
        /// <returns></returns>
        public static Board GetStartingBoard()
        {
            Piece[] squares = new Piece[SquareNo];
            BoardStatus status = new BoardStatus();

            squares[A8] = new BlackRook();
            squares[B8] = new BlackKnight();
            squares[C8] = new BlackBishop();
            squares[D8] = new BlackQueen();
            squares[E8] = new BlackKing();
            squares[F8] = new BlackBishop();
            squares[G8] = new BlackKnight();
            squares[H8] = new BlackRook();

            for (int sqIndex = SideSquareNo; sqIndex < SideSquareNo * 2; sqIndex++)
            {
                squares[sqIndex] = new BlackPawn();
            }

            for (int sqIndex = SideSquareNo * (SideSquareNo - 2); sqIndex < SideSquareNo * (SideSquareNo - 1); sqIndex++)
            {
                squares[sqIndex] = new WhitePawn();
            }

            squares[A1] = new WhiteRook();
            squares[B1] = new WhiteKnight();
            squares[C1] = new WhiteBishop();
            squares[D1] = new WhiteQueen();
            squares[E1] = new WhiteKing();
            squares[F1] = new WhiteBishop();
            squares[G1] = new WhiteKnight();
            squares[H1] = new WhiteRook();

            status.WhiteTurn = true;
            status.WhiteCouldCastleLong = status.WhiteCouldCastleShort = status.BlackCouldCastleLong = status.BlackCouldCastleShort = true;
            status.EnPassantTarget = null;
            status.Ply = 0;
            status.Moves = 1;

            return new Board(squares, status);
        }
Exemple #12
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 internal EnPassantCaptureMove(BoardStatus before, int from, int to)
     : base(before, from, to)
 {
 }
Exemple #13
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 internal EnPassantCaptureMove(BoardStatus before, int from, int to)
     : base(before, from, to)
 {
 }
Exemple #14
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="before">The before status</param>
 /// <param name="from">The starting square</param>
 /// <param name="to">The ending square</param>
 /// <param name="rookMove">The rook move</param>
 internal CastlingMove(BoardStatus before, int from, int to, Move rookMove)
     : base(before, from, to)
 {
     this.rookMove = rookMove;
 }