Cinnamon  1.1
chess engine
ChessBoard.h
Go to the documentation of this file.
00001 #ifndef CHESSBOARD_H_
00002 #define CHESSBOARD_H_
00003 #include <iostream>
00004 #include <sstream>
00005 #include <string.h>
00006 #include "Hash.h"
00007 #include "namespaces.h"
00008 
00009 using namespace _bits;
00010 using namespace _board;
00011 
00012 class ChessBoard {
00013 public:
00014 
00015     ChessBoard();
00016     virtual ~ChessBoard();
00017     static const u64 CENTER_MASK = 0x1818000000ULL;
00018     static const u64 BIG_DIAG_LEFT = 0x102040810204080ULL;
00019     static const u64 BIG_DIAG_RIGHT = 0x8040201008040201ULL;
00020     static const int SQUARE_FREE = 12;
00021     static const int PAWN_BLACK = 0;
00022     static const int PAWN_WHITE = 1;
00023     static const int ROOK_BLACK = 2;
00024     static const int ROOK_WHITE = 3;
00025     static const int BISHOP_BLACK = 4;
00026     static const int BISHOP_WHITE = 5;
00027     static const int KNIGHT_BLACK = 6;
00028     static const int KNIGHT_WHITE = 7;
00029     static const int KING_BLACK = 8;
00030     static const int KING_WHITE = 9;
00031     static const int QUEEN_BLACK = 10;
00032     static const int QUEEN_WHITE = 11;
00033     static const int NO_ENPASSANT = -1;
00034     void display();
00035     string getFen();
00036     char decodeBoard(string) ;
00037     int getPieceByChar(char) ;
00038 #ifdef DEBUG_MODE
00039     u64 getBitBoard(int side);
00040 #endif
00041 
00042     template <int side> u64 getBitBoard() {
00043         return chessboard[PAWN_BLACK+side] | chessboard[ROOK_BLACK+side] | chessboard[BISHOP_BLACK+side] | chessboard[KNIGHT_BLACK+side]
00044                | chessboard[KING_BLACK+side] | chessboard[QUEEN_BLACK+side];
00045     }
00046 
00047     void setSide(bool b) {
00048         sideToMove=b;
00049     }
00050 
00051     int getSide() {
00052         return sideToMove;
00053     }
00054 
00055     template <int side> int getPieceAt( u64 bitmapPos) {
00056         return ((chessboard[PAWN_BLACK+side] & bitmapPos) ? PAWN_BLACK+side :
00057                 ((chessboard[ROOK_BLACK+side] & bitmapPos) ? ROOK_BLACK+side :
00058                  ((chessboard[BISHOP_BLACK+side] & bitmapPos) ? BISHOP_BLACK+side :
00059                   ((chessboard[KNIGHT_BLACK+side] & bitmapPos) ? KNIGHT_BLACK+side :
00060                    ((chessboard[QUEEN_BLACK+side] & bitmapPos) ? QUEEN_BLACK+side :
00061                     ((chessboard[KING_BLACK+side] & bitmapPos) ? KING_BLACK+side : SQUARE_FREE))))));
00062     }
00063 
00064 protected:
00065 
00066     typedef struct {
00067         u64 allPieces;
00068         u64 kingAttackers[2];
00069         u64 allPiecesSide[2];
00070         u64 pawns[2];
00071         u64 rooks[2];
00072         u64 openColumn;
00073         u64 semiOpenColumn[2];
00074         u64 isolated[2];
00075         int kingSecurityDistance[2];
00076         unsigned short posKing[2];
00077     } _Tboard;
00078 
00079     static const u64 A7bit = 0x80000000000000ULL;
00080     static const u64 B7bit = 0x40000000000000ULL;
00081     static const u64 C6bit = 0x200000000000ULL;
00082     static const u64 A6bit = 0x800000000000ULL;
00083     static const u64 H7bit = 0x1000000000000ULL;
00084     static const u64 G7bit = 0x2000000000000ULL;
00085     static const u64 F6bit = 0x40000000000ULL;
00086     static const u64 H6bit = 0x10000000000ULL;
00087     static const u64 A8bit = 0x8000000000000000ULL;
00088     static const u64 H8bit = 0x100000000000000ULL;
00089     static const u64 A2bit = 0x8000ULL;
00090     static const u64 B2bit = 0x4000ULL;
00091     static const u64 A3bit = 0x800000ULL;
00092     static const u64 H2bit = 0x100ULL;
00093     static const u64 G2bit = 0x200ULL;
00094     static const u64 H3bit = 0x10000ULL;
00095     static const u64 A1bit = 0x80ULL;
00096     static const u64 H1bit = 0x1ULL;
00097     static const u64 B5bit = 0x4000000000ULL;
00098     static const u64 G5bit = 0x200000000ULL;
00099     static const u64 B4bit = 0x40000000ULL;
00100     static const u64 G4bit = 0x2000000ULL;
00101     static const u64 F1G1bit = 0x6ULL;
00102     static const u64 H1H2G1bit = 0x103ULL;
00103     static const u64 C1B1bit = 0x60ULL;
00104     static const u64 A1A2B1bit = 0x80c0ULL;
00105     static const u64 F8G8bit = 0x600000000000000ULL;
00106     static const u64 H8H7G8bit = 0x301000000000000ULL;
00107     static const u64 C8B8bit = 0x6000000000000000ULL;
00108     static const u64 A8A7B8bit = 0xc080000000000000ULL;
00109     static const u64 C6A6bit = 0xa00000000000ULL;
00110     static const u64 F6H6bit = 0x50000000000ULL;
00111     static const u64 A7C7bit = 0xa0000000000000ULL;
00112     static const u64 H7G7bit = 0x3000000000000ULL;
00113     static const u64 C3A3bit = 0xa00000ULL;
00114     static const u64 F3H3bit = 0x50000ULL;
00115     static const u64 A2C2bit = 0xa000ULL;
00116     static const u64 H2G2bit = 0x300ULL;
00117 
00118     static const int E1 = 3;
00119     static const int E8 = 59;
00120     static const int C1 = 5;
00121     static const int F1 = 2;
00122     static const int C8 = 58;
00123     static const int F8 = 61;
00124     static const u64 BLACK_SQUARES = 0x55AA55AA55AA55AAULL;
00125     static const u64 WHITE_SQUARES = 0xAA55AA55AA55AA55ULL;
00126     static const uchar KING_SIDE_CASTLE_MOVE_MASK = 0b00000100;
00127     static const uchar QUEEN_SIDE_CASTLE_MOVE_MASK = 0b00001000;
00128     static const uchar RIGHT_KING_CASTLE_WHITE_MASK = 0b00010000;
00129     static const uchar RIGHT_QUEEN_CASTLE_WHITE_MASK = 0b00100000;
00130     static const uchar RIGHT_KING_CASTLE_BLACK_MASK = 0b01000000;
00131     static const uchar RIGHT_QUEEN_CASTLE_BLACK_MASK = 0b10000000;
00132     u64 zobristKey;
00133     int enpassantPosition;
00134     uchar rightCastle;
00135     u64 chessboard[12];
00136     _Tboard structure;
00137     bool sideToMove;
00138     int friendKing[2];
00139     virtual int loadFen(string);
00140     string decodeBoardinv(const uchar type, const int a, const int side);
00141     void makeZobristKey();
00142 
00143     template <int side> int getNpiecesNoPawnNoKing() {
00144         return _bits::bitCount(chessboard[ROOK_BLACK+side] | chessboard[BISHOP_BLACK+side] | chessboard[KNIGHT_BLACK+side] | chessboard[QUEEN_BLACK+side]);
00145     }
00146 #define updateZobristKey(piece,  position) (zobristKey ^= RANDOM_KEY[piece][position])
00147 
00148 #ifdef DEBUG_MODE
00149     int getPieceAt(int side, u64 bitmapPos);
00150 #endif
00151 private:
00152     string fenString;
00153     void setRightCastle(uchar r);
00154     int loadFen();
00155     uchar getRightCastle();
00156     void boardToFen(string& fen);
00157 };
00158 #endif
00159 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines