Example #1
0
        /// <summary>
        /// Sets the piece on a board square. Don't call this function to update bord when moving a piece,
        /// only call it to place one piece directly on board.
        /// </summary>
        /// <param name="square">The square to place a piece.</param>
        /// <param name="piece">The piece to place. To remove a piece use "Piece.None".</param>
        public void PlacePiece(Square square, Piece piece)
        {
            //Hash out current piece on square
            if (m_squares[(int)square] != Piece.None)
            {
                m_boardHash.HashFlipPieceSquare(m_squares[(int)square], square);
            }

            //Hash in new piece
            if (piece != Piece.None)
            {
                m_boardHash.HashFlipPieceSquare(piece, square);
            }

            //Update quick indexes
            switch (GetPieceColor(piece))
            {
            case PieceColor.White:
                m_whiteLocationList.PlacePiece(square);
                m_blackLocationList.RemovePiece(square);
                break;

            case PieceColor.Black:
                m_blackLocationList.PlacePiece(square);
                m_whiteLocationList.RemovePiece(square);
                break;

            case PieceColor.None:
                m_whiteLocationList.RemovePiece(square);
                m_blackLocationList.RemovePiece(square);
                break;
            }

            //Place piece on board
            m_squares[(int)square] = piece;

            //and update king location
            if (piece == Piece.WhiteKing)
            {
                m_whiteKingLocation = square;
            }

            if (piece == Piece.BlackKing)
            {
                m_blackKingLocation = square;
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of Board ready to be played on
        /// and with pieces put on their initial locations.
        /// </summary>
        public Board()
        {
            //we make board twice as big as we use the 0x88 scheme for representing the board
              m_squares = new Piece[NOF_SQUARS];
              for (int i = 0; i < m_squares.Length; ++i)
            m_squares[i] = Piece.None;

              m_squares[(int)Square.A1] = Piece.WhiteRook;
              m_squares[(int)Square.B1] = Piece.WhiteKnight;
              m_squares[(int)Square.C1] = Piece.WhiteBishop;
              m_squares[(int)Square.D1] = Piece.WhiteQueen;
              m_squares[(int)Square.E1] = Piece.WhiteKing;
              m_squares[(int)Square.F1] = Piece.WhiteBishop;
              m_squares[(int)Square.G1] = Piece.WhiteKnight;
              m_squares[(int)Square.H1] = Piece.WhiteRook;
              m_squares[(int)Square.A2] = Piece.WhitePawn;
              m_squares[(int)Square.B2] = Piece.WhitePawn;
              m_squares[(int)Square.C2] = Piece.WhitePawn;
              m_squares[(int)Square.D2] = Piece.WhitePawn;
              m_squares[(int)Square.E2] = Piece.WhitePawn;
              m_squares[(int)Square.F2] = Piece.WhitePawn;
              m_squares[(int)Square.G2] = Piece.WhitePawn;
              m_squares[(int)Square.H2] = Piece.WhitePawn;
              m_squares[(int)Square.A7] = Piece.BlackPawn;
              m_squares[(int)Square.B7] = Piece.BlackPawn;
              m_squares[(int)Square.C7] = Piece.BlackPawn;
              m_squares[(int)Square.D7] = Piece.BlackPawn;
              m_squares[(int)Square.E7] = Piece.BlackPawn;
              m_squares[(int)Square.F7] = Piece.BlackPawn;
              m_squares[(int)Square.G7] = Piece.BlackPawn;
              m_squares[(int)Square.H7] = Piece.BlackPawn;
              m_squares[(int)Square.A8] = Piece.BlackRook;
              m_squares[(int)Square.B8] = Piece.BlackKnight;
              m_squares[(int)Square.C8] = Piece.BlackBishop;
              m_squares[(int)Square.D8] = Piece.BlackQueen;
              m_squares[(int)Square.E8] = Piece.BlackKing;
              m_squares[(int)Square.F8] = Piece.BlackBishop;
              m_squares[(int)Square.G8] = Piece.BlackKnight;
              m_squares[(int)Square.H8] = Piece.BlackRook;

              m_state.ColorToPlay = PieceColor.White;
              m_state.WhiteCanCastleShort = true;
              m_state.WhiteCanCastleLong = true;
              m_state.BlackCanCastleShort = true;
              m_state.BlackCanCastleLong = true;
              m_state.EnPassantTarget = Square.None;
              m_state.NonHitAndPawnMovesPlayed = 0;

              m_history = new HashHistory();
              m_pieceFactory = FlyweightPieceFactory.Instance();

              m_whiteLocationList = new PieceLocationManager();
              m_blackLocationList = new PieceLocationManager();
              foreach (Square square in this)
              {
            if (GetPieceColor(square) == PieceColor.White)
              m_whiteLocationList.PlacePiece(square);

            if (GetPieceColor(square) == PieceColor.Black)
              m_blackLocationList.PlacePiece(square);
              }
              m_whiteKingLocation = Square.E1;
              m_blackKingLocation = Square.E8;

              m_boardHash = new ZobristHash(this);
              AddToHistory();
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of Board ready to be played on
        /// and with pieces put on their initial locations.
        /// </summary>
        public Board()
        {
            //we make board twice as big as we use the 0x88 scheme for representing the board
            m_squares = new Piece[NOF_SQUARS];
            for (int i = 0; i < m_squares.Length; ++i)
            {
                m_squares[i] = Piece.None;
            }

            m_squares[(int)Square.A1] = Piece.WhiteRook;
            m_squares[(int)Square.B1] = Piece.WhiteKnight;
            m_squares[(int)Square.C1] = Piece.WhiteBishop;
            m_squares[(int)Square.D1] = Piece.WhiteQueen;
            m_squares[(int)Square.E1] = Piece.WhiteKing;
            m_squares[(int)Square.F1] = Piece.WhiteBishop;
            m_squares[(int)Square.G1] = Piece.WhiteKnight;
            m_squares[(int)Square.H1] = Piece.WhiteRook;
            m_squares[(int)Square.A2] = Piece.WhitePawn;
            m_squares[(int)Square.B2] = Piece.WhitePawn;
            m_squares[(int)Square.C2] = Piece.WhitePawn;
            m_squares[(int)Square.D2] = Piece.WhitePawn;
            m_squares[(int)Square.E2] = Piece.WhitePawn;
            m_squares[(int)Square.F2] = Piece.WhitePawn;
            m_squares[(int)Square.G2] = Piece.WhitePawn;
            m_squares[(int)Square.H2] = Piece.WhitePawn;
            m_squares[(int)Square.A7] = Piece.BlackPawn;
            m_squares[(int)Square.B7] = Piece.BlackPawn;
            m_squares[(int)Square.C7] = Piece.BlackPawn;
            m_squares[(int)Square.D7] = Piece.BlackPawn;
            m_squares[(int)Square.E7] = Piece.BlackPawn;
            m_squares[(int)Square.F7] = Piece.BlackPawn;
            m_squares[(int)Square.G7] = Piece.BlackPawn;
            m_squares[(int)Square.H7] = Piece.BlackPawn;
            m_squares[(int)Square.A8] = Piece.BlackRook;
            m_squares[(int)Square.B8] = Piece.BlackKnight;
            m_squares[(int)Square.C8] = Piece.BlackBishop;
            m_squares[(int)Square.D8] = Piece.BlackQueen;
            m_squares[(int)Square.E8] = Piece.BlackKing;
            m_squares[(int)Square.F8] = Piece.BlackBishop;
            m_squares[(int)Square.G8] = Piece.BlackKnight;
            m_squares[(int)Square.H8] = Piece.BlackRook;

            m_state.ColorToPlay              = PieceColor.White;
            m_state.WhiteCanCastleShort      = true;
            m_state.WhiteCanCastleLong       = true;
            m_state.BlackCanCastleShort      = true;
            m_state.BlackCanCastleLong       = true;
            m_state.EnPassantTarget          = Square.None;
            m_state.NonHitAndPawnMovesPlayed = 0;

            m_history      = new HashHistory();
            m_pieceFactory = FlyweightPieceFactory.Instance();

            m_whiteLocationList = new PieceLocationManager();
            m_blackLocationList = new PieceLocationManager();
            foreach (Square square in this)
            {
                if (GetPieceColor(square) == PieceColor.White)
                {
                    m_whiteLocationList.PlacePiece(square);
                }

                if (GetPieceColor(square) == PieceColor.Black)
                {
                    m_blackLocationList.PlacePiece(square);
                }
            }
            m_whiteKingLocation = Square.E1;
            m_blackKingLocation = Square.E8;

            m_boardHash = new ZobristHash(this);
            AddToHistory();
        }