Cinnamon  1.0
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     string START_FEN;
00034     void display();
00035     char decodeBoard(string) ;
00036     void setUci(bool);
00037     bool getUci();
00038     int getPieceByChar(char) ;
00039 #ifdef DEBUG_MODE
00040     u64 getBitBoard(int side);
00041 #endif
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[KING_BLACK+side] & bitmapPos) ? KING_BLACK+side :
00058                  ((chessboard[ROOK_BLACK+side] & bitmapPos) ? ROOK_BLACK+side :
00059                   ((chessboard[KNIGHT_BLACK+side] & bitmapPos) ? KNIGHT_BLACK+side :
00060                    ((chessboard[BISHOP_BLACK+side] & bitmapPos) ? BISHOP_BLACK+side :
00061                     ((chessboard[QUEEN_BLACK+side] & bitmapPos) ? QUEEN_BLACK+side : SQUARE_FREE))))));
00062     }
00063 protected:
00064 
00065     typedef struct {
00066         u64 allPieces;
00067         unsigned short posKing[2];
00068         u64 kingAttackers[2];
00069         u64 allPiecesSide[2];
00070         u64 pawns[2];
00071         u64 queens[2];
00072         u64 rooks[2];
00073         u64 openColumn;
00074         u64 semiOpenColumn[2];
00075         int kingSecurityDistance[2];
00076         u64 isolated[2];
00077     } _Tboard;
00078 
00079     static const int E1= 3;
00080     static const int E8 =59;
00081     static const int C1= 5;
00082     static const int F1= 2;
00083     static const int C8 =58;
00084     static const int F8 =61;
00085     static const u64 BLACK_SQUARES = 0x55AA55AA55AA55AAULL;
00086     static const u64 WHITE_SQUARES = 0xAA55AA55AA55AA55ULL;
00087     static const uchar KING_SIDE_CASTLE_MOVE_MASK =0b00000100;
00088     static const uchar QUEEN_SIDE_CASTLE_MOVE_MASK =0b00001000;
00089     static const uchar RIGHT_KING_CASTLE_WHITE_MASK =0b00010000;
00090     static const uchar RIGHT_QUEEN_CASTLE_WHITE_MASK =0b00100000;
00091     static const uchar RIGHT_KING_CASTLE_BLACK_MASK =0b01000000;
00092     static const uchar RIGHT_QUEEN_CASTLE_BLACK_MASK =0b10000000;
00093     u64 zobristKey;
00094     int enpassantPosition;
00095     uchar RIGHT_CASTLE;
00096     u64 chessboard[12];
00097     _Tboard structure;
00098     bool sideToMove;
00099     int friendKing[2];
00100     virtual int loadFen(string);
00101     string decodeBoardinv(const uchar type, const int a, const int side);
00102     u64 makeZobristKey();
00103 
00104     template <int side> int getNpiecesNoPawnNoKing() {
00105         return _bits::bitCount(chessboard[ROOK_BLACK+side] | chessboard[BISHOP_BLACK+side] | chessboard[KNIGHT_BLACK+side] | chessboard[QUEEN_BLACK+side]);
00106     }
00107 
00108     void updateZobristKey(u64* key, uchar piece, uchar position) {
00109         *(key) ^= RANDOM_KEY[piece][position];
00110     }
00111 #ifdef DEBUG_MODE
00112     int getPieceAt(int side, u64 bitmapPos);
00113 #endif
00114 private:
00115     void setRightCastle(uchar r);
00116     int loadFen();
00117     bool uci;
00118     uchar getRightCastle();
00119     void boardToFen(string& fen);
00120 };
00121 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines