1 #ifndef __BOARD_H__ 2 #define __BOARD_H__ 3 4 /* 5 * Board.h (c) Noah Roberts 2003-02-23 6 * class interface, keeps track of the current state of the board. 7 */ 8 9 #include <vector> 10 #include <string> 11 #include <iostream> 12 13 14 #include "Move.h" 15 16 typedef enum { BLUE = 0, RED = 8, NOCOLOR = 16} color; 17 typedef enum { EMPTY, ZU, PAO, CHE, MA, XIANG, SHI, LAO } piece; 18 #define JIANG LAO 19 20 21 #define BOARD_AREA 90 22 #define BOARD_WIDTH 9 23 #define BOARD_HEIGHT 10 24 25 26 #define COLOR_SWITCH_KEY 0x80000000 27 28 typedef unsigned long u_int32; 29 30 class Board; 31 32 class BoardObserver 33 { 34 public: boardChanged(Board * board,int changeType)35 virtual void boardChanged(Board *board, int changeType) {} 36 }; 37 38 enum { GAME_OVER, MOVE_MADE, MOVE_UNDONE, BOARD_ALTERED }; 39 40 class Board 41 { 42 private: 43 unsigned char board[90]; 44 int _sideToMove; 45 46 std::vector<BoardObserver*> observers; 47 48 bool _gameOver; 49 std::vector<Move> moveHistory; 50 std::string _startPos; 51 52 int kings[2]; 53 std::vector< std::vector<int> > rpieces; 54 std::vector< std::vector<int> > bpieces; 55 56 // Note: since the random values are generated by the 57 // constructor, these may not be the same across boards; 58 // therefore the hash keys will not match if the keys 59 // are generated by different instances. 60 static u_int32 hashValues[2][90][15]; 61 static bool hashValuesFilled; 62 u_int32 _primaryHash; 63 u_int32 _secondaryHash; 64 65 void generateValues(); // Generates the hash values for zoberist keys 66 // hash key management... alterHashes(int loc)67 void alterHashes(int loc) 68 { 69 _primaryHash ^= hashValues[0][loc][board[loc]]; 70 _secondaryHash ^= hashValues[1][loc][board[loc]]; 71 } 72 73 // Piece index management... 74 void addPiece(int location); 75 void removePiece(int location); 76 void movePiece(int origin, int dest); 77 78 public: 79 Board(); 80 Board(std::string position); 81 82 // Move management... 83 void makeMove(Move &theMove); 84 void unmakeMove(); // unmakes the top move in moveHistory. makeNullMove()85 void makeNullMove() { makeMove(Move::nullMove()); } unmakeNullMove()86 void unmakeNullMove() { unmakeMove(); } 87 88 // Square based access operators pieceAt(int index)89 piece pieceAt(int index) { return (piece)(board[index]&7); } colorAt(int index)90 color colorAt(int index) { return (color)(board[index]&8); } 91 unsigned char operator[](int index) { return board[index]; } 92 sideToMove()93 color sideToMove() { return (color)_sideToMove; } 94 95 // Position managament... 96 bool setPosition(std::string fen); 97 std::string getPosition(); 98 void resetBoard(); 99 100 // Game move history access... history()101 std::vector<Move>& history() { return moveHistory; } 102 // Game starting position... startingPosition()103 std::string& startingPosition() { return _startPos; } 104 gameOver()105 bool gameOver() { return _gameOver; } gameOver(bool go)106 void gameOver(bool go) { _gameOver = go; notifyObservers(GAME_OVER);} 107 108 // Piece index access... king(color c)109 int king(color c) { return kings[c==RED?1:0]; } 110 std::vector<int> pieces(color c, piece p); 111 112 // Zoberist keys... primaryHash()113 u_int32 primaryHash() { return _primaryHash; } secondaryHash()114 u_int32 secondaryHash() { return _secondaryHash; } 115 116 // Display... 117 friend std::ostream& operator<<(std::ostream& out, Board &theBoard); 118 addObserver(BoardObserver * observer)119 void addObserver(BoardObserver *observer) { observers.push_back(observer); } 120 //void removeObserver(BoardObserver *observer); 121 void notifyObservers(int message); 122 }; 123 124 #endif /* __BOARD_H__ */ 125