Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.     Texel - A UCI chess engine.
  3.     Copyright (C) 2012-2014  Peter Ă–sterlund, peterosterlund2@gmail.com
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. /*
  20.  * book.hpp
  21.  *
  22.  *  Created on: Feb 25, 2012
  23.  *      Author: petero
  24.  */
  25.  
  26. #ifndef BOOK_HPP_
  27. #define BOOK_HPP_
  28.  
  29. #include "move.hpp"
  30. #include "util/util.hpp"
  31. #include "util/random.hpp"
  32.  
  33. #include <map>
  34. #include <vector>
  35. #include <cmath>
  36.  
  37. class Position;
  38.  
  39. /**
  40.  * Implements an opening book.
  41.  */
  42. class Book {
  43. public:
  44.     /** Constructor. */
  45.     Book(bool verbose0);
  46.  
  47.     /** Return a random book move for a position, or empty move if out of book. */
  48.     void getBookMove(Position& pos, Move& out);
  49.  
  50.     /** Return a string describing all book moves. */
  51.     std::string getAllBookMoves(const Position& pos);
  52.  
  53. private:
  54.  
  55.     void initBook();
  56.  
  57.     /** Add a move to a position in the opening book. */
  58.     void addToBook(const Position& pos, const Move& moveToAdd);
  59.  
  60.     int getWeight(int count);
  61.  
  62.     static void createBinBook(std::vector<S8>& binBook);
  63.  
  64.     /** Add a sequence of moves, starting from the initial position, to the binary opening book. */
  65.     static bool addBookLine(const std::string& line, std::vector<S8>& binBook);
  66.  
  67.     static int pieceToProm(int p);
  68.  
  69.     static int promToPiece(int prom, bool whiteMove);
  70.  
  71.  
  72.     struct BookEntry {
  73.         Move move;
  74.         int count;
  75.         BookEntry(const Move& m) : move(m), count(1) { }
  76.     };
  77.  
  78.     using BookMap = std::map<U64, std::vector<BookEntry>>;
  79.     static BookMap bookMap;
  80.     static Random rndGen;
  81.     static int numBookMoves;
  82.     bool verbose;
  83.  
  84.     static const char* bookLines[];
  85. };
  86.  
  87. inline
  88. Book::Book(bool verbose0)
  89.     : verbose(verbose0) {
  90. }
  91.  
  92. #endif /* BOOK_HPP_ */
  93.