Cinnamon  1.1
chess engine
GenMoves.h
Go to the documentation of this file.
00001 #ifndef GENMOVES_H_
00002 #define GENMOVES_H_
00003 
00004 #include "ChessBoard.h"
00005 using namespace _eval;
00006 class GenMoves: public ChessBoard {
00007 public:
00008     static const int MAX_PLY = 72;
00009     GenMoves();
00010     virtual ~GenMoves();
00011     void setPerft(const bool b);
00012     bool generateCaptures(const int side,u64,u64);
00013     void generateMoves(const int side,const u64);
00014     template <int side> bool generateCaptures(const u64,const u64);
00015     template <int side> void generateMoves(const u64);
00016     int getMoveFromSan(const string fenStr,_Tmove* move);
00017     void init();
00018     virtual int loadFen();
00019     virtual int loadFen(string fen);
00020     void makemove(_Tmove * move) ;
00021     void setRepetitionMapCount(int i);
00022     bool performKingShiftCapture(int side,  const u64 enemies) ;
00023     bool performKnightShiftCapture( const int piece, const u64 enemies, const int side);
00024     bool performDiagCapture( const int piece, const u64 enemies, const int side, const u64 allpieces);
00025     bool performRankFileCapture( const int piece, const u64 enemies, const int side, const u64 allpieces);
00026     template <int side> bool performPawnCapture( const u64 enemies);
00027     template <int side> void performPawnShift( const u64 xallpieces) ;
00028     int performPawnShiftCount(int side, const u64 xallpieces) ;
00029     void performDiagShift( const int piece, const int side, const u64 allpieces);
00030     void performRankFileShift( const int piece, const int side, const u64 allpieces);
00031     bool makemove(_Tmove * move,bool rep,bool) ;
00032     bool isPinned(const int side, const uchar Position, const uchar piece);
00033 
00034     void incListId() {
00035         listId++;
00036         ASSERT(listId < MAX_PLY);
00037         ASSERT( listId>=0);
00038     }
00039 
00040     void decListId() {
00041         gen_list[listId--].size=0;
00042     }
00043 
00044     int getListSize() {
00045         return gen_list[listId].size;
00046     }
00047 
00048     void pushStackMove() {
00049         pushStackMove(zobristKey);
00050     }
00051 
00052     _Tmove * getMove(int i) {
00053         return &gen_list[listId].moveList[i];
00054     }
00055 
00056     void resetList() {
00057         gen_list[listId].size = 0;
00058     }
00059 
00060 
00061 protected:
00062 
00063     static const u64 RANK_1 = 0xff00ULL;
00064     static const u64 RANK_3 = 0xff000000ULL;
00065     static const u64 RANK_4 = 0xff00000000ULL;
00066     static const u64  RANK_6 = 0xff000000000000ULL;
00067     static const uchar STANDARD_MOVE_MASK = 0b00000011;
00068     static const uchar ENPASSANT_MOVE_MASK = 0b00000001;
00069     static const uchar PROMOTION_MOVE_MASK = 0b00000010;
00070     static const int MAX_REP_COUNT = 1024;
00071     int repetitionMapCount;
00072     u64* repetitionMap;
00073     int currentPly;
00074     bool perftMode;
00075     u64 numMoves, numMovesq;
00076     int listId;
00077     _TmoveP* gen_list;
00078     _Tmove* getNextMove(decltype(gen_list));
00079     u64 getKingAttackers(const int xside,u64,int) ;
00080     void clearKillerHeuristic();
00081     u64 getTotMoves();
00082     int getMobilityBishop(int , u64 ,u64 );
00083     int getMobilityRook(const int position,const u64 enemies,const u64 friends);
00084     int getMobilityPawns(const int side,const int ep,const u64 ped_friends,const u64 enemies,const u64 xallpieces) ;
00085     int getMobilityCastle(const int side, const u64 allpieces);
00086     int getMobilityQueen(const int position,const u64 enemies,const u64 friends);
00087     template <int side> bool attackSquare(const uchar Position,u64);
00088     void initKillerHeuristic();
00089 #ifdef DEBUG_MODE
00090     int nCutAB, nNullMoveCut, nCutFp, nCutRazor,nCutInsufficientMaterial;
00091     double betaEfficiencyCumulative, betaEfficiency;
00092 #endif
00093     void pushRepetition(u64);
00094     int killerHeuristic[64][64];
00095     template <int side> bool inCheckPerft(const int from, const int to, const uchar type, const int pieceFrom, const int pieceTo, int promotionPiece);
00096     void performCastle(const int side, const uchar type);
00097     void unPerformCastle(const int side, const uchar type);
00098     void tryAllCastle(const int side, const u64 allpieces);
00099     void takeback(_Tmove * move,  const u64 oldkey,bool rep) ;
00100     template <uchar type> bool pushmove( const int from, const int to, const int side, int promotionPiece, int pieceFrom) ;
00101 
00102     template <int side> bool inCheck() {
00103         return attackSquare<side>(BITScanForward (chessboard[KING_BLACK+side]));
00104     }
00105 
00106     template <int side> bool attackSquare(const uchar position) {
00107         return  attackSquare<side>( position,getBitBoard<BLACK>() | getBitBoard<WHITE>());
00108     }
00109     void setKillerHeuristic(const int from,const int to,const int value) {
00110         ASSERT(from >= 0 && from <64 && to >=0 && to <64);
00111         killerHeuristic[from][to] = value;
00112     }
00113 
00114     void incKillerHeuristic(const int from,const int to,const int value) {
00115         ASSERT(from >= 0 && from <64 && to >=0 && to <64);
00116         ASSERT (killerHeuristic[from][to] <= killerHeuristic[from][to] + value);
00117         killerHeuristic[from][to] += value;
00118     }
00119 
00120 private:
00121 
00122     static const int NO_PROMOTION = -1;
00123     static const int MAX_MOVE = 130;
00124     static const u64 TABJUMPPAWN = 0xFF00000000FF00ULL;
00125     static const u64 TABCAPTUREPAWN_RIGHT = 0xFEFEFEFEFEFEFEFEULL;
00126     static const u64 TABCAPTUREPAWN_LEFT = 0x7F7F7F7F7F7F7F7FULL;
00127 
00128     template <int side> void checkJumpPawn(u64 x, const u64 xallpieces);
00129 
00130     int performRankFileCaptureCount( const int , const u64 enemies, const u64 allpieces);
00131     int performDiagCaptureCount( const int , const u64 enemies, const u64 allpieces);
00132     int performDiagShiftCount( const int , const u64 allpieces);
00133     int performRankFileShiftCount( const int piece, const u64 allpieces);
00134 
00135     void popStackMove() {
00136         ASSERT(repetitionMapCount>0);
00137         if(--repetitionMapCount && repetitionMap[repetitionMapCount-1]==0)
00138             repetitionMapCount--;
00139     }
00140 
00141     void pushStackMove( u64 key) {
00142         ASSERT(repetitionMapCount<MAX_REP_COUNT-1);
00143         repetitionMap[repetitionMapCount++]= key;
00144     }
00145 
00146 };
00147 #endif
00148 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines