Subversion Repositories Games.Chess Giants

Rev

Rev 169 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
  3.   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
  4.   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
  5.   Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
  6.  
  7.   Stockfish is free software: you can redistribute it and/or modify
  8.   it under the terms of the GNU General Public License as published by
  9.   the Free Software Foundation, either version 3 of the License, or
  10.   (at your option) any later version.
  11.  
  12.   Stockfish is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License
  18.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. #ifndef MOVEPICK_H_INCLUDED
  22. #define MOVEPICK_H_INCLUDED
  23.  
  24. #include <algorithm> // For std::max
  25. #include <cstring>   // For std::memset
  26.  
  27. #include "movegen.h"
  28. #include "position.h"
  29. #include "search.h"
  30. #include "types.h"
  31.  
  32.  
  33. /// The Stats struct stores moves statistics. According to the template parameter
  34. /// the class can store History and Countermoves. History records how often
  35. /// different moves have been successful or unsuccessful during the current search
  36. /// and is used for reduction and move ordering decisions.
  37. /// Countermoves store the move that refute a previous one. Entries are stored
  38. /// using only the moving piece and destination square, hence two moves with
  39. /// different origin but same destination and piece will be considered identical.
  40. template<typename T, bool CM = false>
  41. struct Stats {
  42.  
  43.   static const Value Max = Value(1 << 28);
  44.  
  45.   const T* operator[](Piece pc) const { return table[pc]; }
  46.   T* operator[](Piece pc) { return table[pc]; }
  47.   void clear() { std::memset(table, 0, sizeof(table)); }
  48.  
  49.   void update(Piece pc, Square to, Move m) {
  50.  
  51.     if (m != table[pc][to])
  52.         table[pc][to] = m;
  53.   }
  54.  
  55.   void update(Piece pc, Square to, Value v) {
  56.  
  57.     if (abs(int(v)) >= 324)
  58.         return;
  59.  
  60.     table[pc][to] -= table[pc][to] * abs(int(v)) / (CM ? 512 : 324);
  61.     table[pc][to] += int(v) * (CM ? 64 : 32);
  62.   }
  63.  
  64. private:
  65.   T table[PIECE_NB][SQUARE_NB];
  66. };
  67.  
  68. typedef Stats<Move> MoveStats;
  69. typedef Stats<Value, false> HistoryStats;
  70. typedef Stats<Value,  true> CounterMoveStats;
  71. typedef Stats<CounterMoveStats> CounterMoveHistoryStats;
  72.  
  73.  
  74. /// MovePicker class is used to pick one pseudo legal move at a time from the
  75. /// current position. The most important method is next_move(), which returns a
  76. /// new pseudo legal move each time it is called, until there are no moves left,
  77. /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
  78. /// beta algorithm, MovePicker attempts to return the moves which are most likely
  79. /// to get a cut-off first.
  80.  
  81. class MovePicker {
  82. public:
  83.   MovePicker(const MovePicker&) = delete;
  84.   MovePicker& operator=(const MovePicker&) = delete;
  85.  
  86.   MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
  87.   MovePicker(const Position&, Move, const HistoryStats&, Value);
  88.   MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMoveStats&, Move, Search::Stack*);
  89.  
  90.   Move next_move();
  91.  
  92. private:
  93.   template<GenType> void score();
  94.   void generate_next_stage();
  95.   ExtMove* begin() { return moves; }
  96.   ExtMove* end() { return endMoves; }
  97.  
  98.   const Position& pos;
  99.   const HistoryStats& history;
  100.   const CounterMoveStats* counterMoveHistory;
  101.   Search::Stack* ss;
  102.   Move countermove;
  103.   Depth depth;
  104.   Move ttMove;
  105.   ExtMove killers[3];
  106.   Square recaptureSquare;
  107.   Value threshold;
  108.   int stage;
  109.   ExtMove *endQuiets, *endBadCaptures = moves + MAX_MOVES - 1;
  110.   ExtMove moves[MAX_MOVES], *cur = moves, *endMoves = moves;
  111. };
  112.  
  113. #endif // #ifndef MOVEPICK_H_INCLUDED
  114.