1 #ifndef __ENGINE_H__ 2 #define __ENGINE_H__ 3 4 /* 5 * Engine.h (c) Noah Roberts 2003-02-26 6 * The Engine class is the brain of it all, this is the class that ties the rest of 7 * the classes together to actually perform the function of playing chess. This class 8 * creates and navigates the search tree and decides what move to make next. This class 9 * is also the one that will notice and respond when the game has ended. 10 */ 11 12 13 #include <string> 14 #include <vector> 15 #include "Move.h" 16 #include "Options.h" 17 #include "Timer.h" 18 19 #include <sys/timeb.h> 20 21 class Board; 22 class Evaluator; 23 class Lawyer; 24 class OpeningBook; 25 class TranspositionTable; 26 class Interface; 27 //class Move; 28 enum { NO_CUTOFF, HASH_CUTOFF, NULL_CUTOFF, MATE_CUTOFF, MISC_CUTOFF }; 29 struct PVEntry 30 { 31 Move move; 32 int cutoff; PVEntryPVEntry33 PVEntry(Move m, int c) : move(m),cutoff(c) {} 34 }; 35 36 //#define INFIN 0x0FFFFFFF; 37 #define INFIN 3000 38 #define CHECKMATE (-2000) 39 40 class Engine : public OptionsObserver 41 { 42 private: 43 Board *board; 44 Evaluator *evaluator; 45 Lawyer *lawyer; 46 OpeningBook *openingBook; 47 Interface *iface; 48 TranspositionTable *transposTable; 49 50 // search result 51 std::vector<PVEntry> principleVariation; 52 //bool continueSearching; 53 short searchState; 54 55 56 // Search information 57 int maxPly; // also user configurable. 58 std::vector<Move> killer1; 59 std::vector<Move> killer2; 60 std::vector<Move> moveSortPriorityTable; 61 int nullMoveReductionFactor; 62 short searchAborted; 63 64 Timer *myTimer; 65 66 67 // Search statistics 68 int nodeCount; 69 int hashHits; 70 int nullCutoffs; 71 struct timeb startTime; 72 73 // User configurable options 74 75 bool useOpeningBook; 76 bool displayThinking; 77 78 bool quiesc; // perform quiescence search or not 79 bool qnull; // null cutoff in quiescence 80 bool qhash; // use table in quiescence 81 82 83 int searchMethod; // Tells system which algorithm to use 84 bool allowNull; // If false then null move will never be tried. 85 bool useMTDF; // Use the MTD(f) algorithm. 86 bool useIterDeep; // if yes then the search is tried at depths 1-maxPly. 87 // otherwise just maxPly. 88 bool verifyNull; // Use verified null-move pruning or standard. 89 90 bool allowTableWindowAdjustments; // If table contains a depth < our target the 91 // norm is to adjust the window. User may 92 // disable that behaviour. 93 bool useTable; 94 95 // Search functions 96 long quiescence(long alpha, long beta, int ply, int depth, bool nullOk = true); 97 void filterOutNonCaptures(std::list<Move> &l); 98 99 // Search algs... 100 101 //long alphaBeta(long alpha, long beta, int ply, int depth); 102 long alphaBeta(std::vector<PVEntry> &pv, std::list<Move> moveList, 103 long alpha, long beta, int ply, int depth, 104 bool legalonly = false, bool nullOk = true, bool verify = true); 105 //long pvSearch(std::vector<PVEntry> &pv, long alpha, long beta, int ply, int depth, 106 // bool legalonly = false, bool nullOk = true, bool verify = true); 107 long negaScout(std::vector<PVEntry> &pv, std::list<Move> moveList, 108 long alpha, long beta, int ply, int depth, 109 bool legalonly = false, bool nullOk = true, bool verify = true); 110 111 // Generic search - calls specific search, will add heuristics here... 112 long search(std::vector<PVEntry> &pv, long alpha, long beta, int ply, int depth, 113 bool legalonly = false, bool nullOk = true, bool verify = true); 114 115 // MTD(f) algorithm...calls search. 116 long mtd(std::vector<PVEntry> &pv, long guess, int depth); 117 118 // Support functions... 119 // looks for position in table. 120 bool tableSearch(int ply, int depth, long &alpha, long &beta, Move &m, long &score, bool &nullok); 121 // stores position in table. 122 void tableSet(int ply, int depth, long alpha, long beta, Move m, long score); 123 // looks for killers and alters priority table 124 void setUpKillers(int ply); 125 // Adds killer move at ply 126 void newKiller(Move& theMove, int ply); 127 public: 128 Engine(Board *brd, Lawyer *law, Interface *ifc); 129 130 // If board is replaced... setBoard(Board * brd)131 void setBoard(Board *brd) { board = brd; } 132 133 // Tells engine to think... 134 long think(); 135 136 // Tells engine that something happened such that search must be stopped. 137 void endSearch(); 138 bool doneThinking(); 139 bool thinking(); 140 141 // Search information retrieval... 142 Move getMove(); 143 std::string variationText(std::vector<PVEntry> pv); 144 145 // OptionObserver requirements 146 void optionChanged(std::string whatOption); 147 148 // In case compareMoves needs to access 149 friend bool compareMoves(Move& m1, Move &m2); 150 }; 151 152 /* 153 * Move ordering sort predicate used by list to sort moves. 154 * Major hackery - would like to get rid of this. 155 */ 156 extern bool compareMoves(Move& m1, Move& m2); 157 158 159 160 #endif /* __ENGINE_H__ */ 161