Example #1
0
        public bool ProbeScore(ZobristHash hash, int alpha, int beta, int depth, ref int score)
        {
            int i = CalculateHashIndex(hash);

              if (m_hashTable[i].Flag != AlphaBetaFlag.NoScore && hash.Equals(m_hashTable[i].Hash))
              {
            if (m_hashTable[i].Depth >= depth)
            {
              if (m_hashTable[i].Flag == AlphaBetaFlag.ExactScore)
              {
            score = m_hashTable[i].Score;
            return true;
              }

              if (m_hashTable[i].Flag == AlphaBetaFlag.AlphaScore && m_hashTable[i].Score <= alpha)
              {
            score = alpha;
            return true;
              }

              if (m_hashTable[i].Flag == AlphaBetaFlag.BetaScore && m_hashTable[i].Score >= beta)
              {
            score = beta;
            return true;
              }
            }
              }

              return false;
        }
Example #2
0
        /// <summary>
        /// Adds a board to the history. If the board already exists the frequency is increased.
        /// </summary>
        /// <param name="board">Board to add.</param>
        public void AddBoard(Board board)
        {
            int         count;
            ZobristHash hash = board.BoardHash(true);

            m_gameHashHistory.TryGetValue(hash, out count);
            m_gameHashHistory[hash] = count + 1;
        }
Example #3
0
        public MoveIdentifier ProbePvMove(ZobristHash hash)
        {
            int i = CalculateHashIndex(hash);

              if (m_hashTable[i].Flag != AlphaBetaFlag.NoScore && hash.Equals(m_hashTable[i].Hash))
            return m_hashTable[i].PvMove;
              else
            return new MoveIdentifier(Square.None, Square.None, Piece.None);
        }
Example #4
0
        /// <summary>
        /// Compares this instance of a zobrist hash to a given object.
        /// </summary>
        /// <param name="obj">The object to compare this instance to.</param>
        /// <returns>Returns true if the two instances are equal.</returns>
        public override bool Equals(object obj)
        {
            ZobristHash hash = (ZobristHash)obj;

            if (hash == null)
            {
                return(false);
            }
            else
            {
                return(m_key == hash.m_key && m_lock == hash.m_lock);
            }
        }
Example #5
0
        /// <summary>
        /// Removes a board from the history. If the frequency for
        /// the board is larger then one the frequency is decreased.
        /// </summary>
        /// <param name="board">Board to remove.</param>
        public void RemoveBoard(Board board)
        {
            int         count;
            ZobristHash hash = board.BoardHash(true);

            m_gameHashHistory.TryGetValue(hash, out count);
            if (count > 1)
            {
                m_gameHashHistory[hash] = count - 1;
            }
            else
            {
                m_gameHashHistory.Remove(hash);
            }
        }
Example #6
0
        public Board(Board board)
        {
            m_squares = new Piece[board.m_squares.Length];
            for (int i = 0; i < m_squares.Length; ++i)
            {
                m_squares[i] = board.m_squares[i];
            }

            m_state = board.m_state;

            m_history           = new HashHistory(board.m_history);
            m_pieceFactory      = FlyweightPieceFactory.Instance();
            m_whiteLocationList = new PieceLocationManager(board.m_whiteLocationList);
            m_blackLocationList = new PieceLocationManager(board.BlackPieceLocations);
            m_whiteKingLocation = board.m_whiteKingLocation;
            m_blackKingLocation = board.m_blackKingLocation;
            m_boardHash         = new ZobristHash(board.m_boardHash);
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of a Zobrist hash identical in value to the passed Zobrist hash.
 /// </summary>
 /// <param name="zobristHash">The ZobristHash to clone.</param>
 public ZobristHash(ZobristHash zobristHash)
 {
     m_key = zobristHash.m_key;
       m_lock = zobristHash.m_lock;
 }
Example #8
0
        public void Load()
        {
            try
              {
            if (File.Exists(OPENING_BOOK_FILE))
            {
              FileStream fs = new FileStream(OPENING_BOOK_FILE, FileMode.Open);
              BinaryReader br = new BinaryReader(fs);
              while (br.BaseStream.Position < br.BaseStream.Length)
              {
            int keyVal = br.ReadInt32();
            int lockVal = br.ReadInt32();
            int queryVal = br.ReadInt32();
            ZobristHash zobristHash = new ZobristHash(keyVal, lockVal);
            m_positionTable[zobristHash] = queryVal;
              }
              br.Close();
              fs.Close();

              OutputWriter.Write(m_positionTable.Count.ToString() + " opening moves loaded");
            }
              }
              catch (Exception e)
              {
            OutputWriter.Write("Exception thrown when loading book: " + e.ToString());
              }
        }
Example #9
0
 private static int CalculateHashIndex(ZobristHash hash)
 {
     return hash.GetHashCode() & (TABLE_SIZE - 1);
 }
Example #10
0
        public void RecordHash(ZobristHash hash, AlphaBetaFlag flag, int depth, int score, Move pvMove)
        {
            if (!m_closed)
              {
            int i = CalculateHashIndex(hash);

            m_hashTable[i].Hash.Key = hash.Key;
            m_hashTable[i].Hash.Lock = hash.Lock;
            m_hashTable[i].Flag = flag;
            m_hashTable[i].Depth = depth;
            m_hashTable[i].Score = score;

            if (pvMove != null)
            {
              m_hashTable[i].PvMove.From = pvMove.From;
              m_hashTable[i].PvMove.To = pvMove.To;
              m_hashTable[i].PvMove.PromotionPiece = pvMove.PromoteTo;
            }
            else
            {
              m_hashTable[i].PvMove.From = Square.None;
              m_hashTable[i].PvMove.To = Square.None;
              m_hashTable[i].PvMove.PromotionPiece = Piece.None;
            }
              }
        }
Example #11
0
 /// <summary>
 /// Initializes a new instance of a Zobrist hash identical in value to the passed Zobrist hash.
 /// </summary>
 /// <param name="zobristHash">The ZobristHash to clone.</param>
 public ZobristHash(ZobristHash zobristHash)
 {
     m_key  = zobristHash.m_key;
     m_lock = zobristHash.m_lock;
 }
Example #12
0
        public void RecordHash(ZobristHash hash, QuiescentFlag flag, int score)
        {
            if (!m_closed)
              {
            int i = CalculateHashIndex(hash);

            m_hashTable[i].Hash.Key = hash.Key;
            m_hashTable[i].Hash.Lock = hash.Lock;
            m_hashTable[i].Flag = flag;
            m_hashTable[i].Score = score;
              }
        }
Example #13
0
        public Board(Board board)
        {
            m_squares = new Piece[board.m_squares.Length];
              for (int i = 0; i < m_squares.Length; ++i)
            m_squares[i] = board.m_squares[i];

              m_state = board.m_state;

              m_history = new HashHistory(board.m_history);
              m_pieceFactory = FlyweightPieceFactory.Instance();
              m_whiteLocationList = new PieceLocationManager(board.m_whiteLocationList);
              m_blackLocationList = new PieceLocationManager(board.BlackPieceLocations);
              m_whiteKingLocation = board.m_whiteKingLocation;
              m_blackKingLocation = board.m_blackKingLocation;
              m_boardHash = new ZobristHash(board.m_boardHash);
        }
Example #14
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 #15
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();
        }