00001
00010 #pragma once
00011
00012 #include "board.h"
00013
00014 #define OB_EMPTY_SQUARE 0x0U
00015 #define OB_EMPTY 0x0U
00016 #define OB_OFF_BOARD_SQUARE 0x9FU
00017
00018 #define OB_GOLD 0x10U
00019 #define OB_SILVER 0x8U
00020 #define OB_PIECE_OFF_BOARD 0x7U
00021 #define OB_PIECE_ELEPHANT 0x6U
00022 #define OB_PIECE_CAMEL 0x5U
00023 #define OB_PIECE_HORSE 0x4U
00024 #define OB_PIECE_DOG 0x3U
00025 #define OB_PIECE_CAT 0x2U
00026 #define OB_PIECE_RABBIT 0x1U
00027 #define OB_PIECE_EMPTY 0x0U
00028 #define OB_PIECE_MASK 0x7U
00029 #define OB_OWNER_MASK 0x18U
00030 #define OB_NORTH 10
00031 #define OB_SOUTH -10
00032 #define OB_EAST 1
00033 #define OB_WEST -1
00034
00035 #define TOP_ROW 8
00036 #define BOTTOM_ROW 1
00037 #define LEFT_COL 1
00038 #define RIGHT_COL 8
00039
00040 #define OB_OWNER(square) (square & OB_OWNER_MASK)
00041 #define OB_PIECE(square) (square & OB_PIECE_MASK)
00042 #define OB_OPP(player) ((16 - player) + 8)
00043
00044
00045
00046
00048 #define PLAYER_TO_INDEX(player) (player == OB_GOLD ? 0 : 1 )
00049 #define INDEX_TO_PLAYER(index) (uint) (16-8*index)
00050
00051 #define OB_SQUARE_DISTANCE(s1, s2) (abs(s1/10 - s2/10) + abs(s1%10 - s2%10))
00052
00053 #define OB_IS_TRAP(index) (index == 33 || index == 36 || index == 63 || index == 66 )
00054 #define OB_IS_PLAYER(square) (OB_OWNER(square) == OB_GOLD || OB_OWNER(square) == OB_SILVER )
00055
00056 #define OB_PIECE_NUM 7
00057 #define OB_SQUARE_NUM 100
00058
00059 extern const int direction[4];
00060 extern const int rabbitForward[2];
00061 extern const int rabbitWinRow[2];
00062 extern const int trap[4];
00063
00064 typedef uint ob_player_t;
00065 typedef int ob_square_t;
00066 typedef uint ob_piece_t;
00067 typedef int FlagBoard[OB_SQUARE_NUM];
00068 typedef uint board_t[OB_SQUARE_NUM];
00069
00070 typedef pair<ob_player_t, ob_piece_t> PiecePair;
00071
00072 #define FLAG_BOARD_EMPTY -1
00073
00081 class PieceArray
00082 {
00083 public:
00084 PieceArray();
00085
00091 void add(coord_t);
00092
00099 void del(coord_t);
00100
00104 void clear();
00105
00106 uint getLen() const;
00107 coord_t operator[](uint) const;
00108
00112 string toString() const;
00113
00117 coord_t getRandom() const;
00118
00119 private:
00120 coord_t elems[MAX_PIECES];
00121 uint len;
00122 };
00123
00129 class OB_Board
00130 {
00131 public:
00132 OB_Board();
00133
00137 void initNewGame();
00138
00145 bool initFromRecord(const char* fn);
00146
00152 bool initFromPosition(const char* fn);
00153
00162 bool initFromPositionCompactString(const string& s);
00163
00171 Step findMCstep();
00172
00176 void findMCmoveAndMake();
00177
00184 bool operator== (const OB_Board& board) const;
00185
00186
00187
00196 bool makeStepTryCommitMove(const Step&);
00197
00204 void makeMove(const string& move);
00205
00212 void makeMoveNoCommit(const Move& move);
00213
00219 void makeMove(const Move& move);
00220
00226 void commitMove();
00227
00233 void updateWinner();
00234
00235
00236
00247 bool quickGoalCheck(ob_player_t player, int stepLimit, Move* move=NULL) const;
00248
00256 bool quickGoalCheck(Move* move=NULL) const;
00257
00264 Move tracebackFlagBoard(const FlagBoard& flagBoard, int win_square, ob_player_t player) const;
00265
00273 int filterRepetitions(StepArray&, int ) const;
00274
00281 bool isSetupPhase() const;
00282
00286 ob_player_t getPlayerToMove() const;
00287
00291 ob_player_t getPlayerToMoveAfterStep(const Step& step) const;
00292
00296 string toString() const;
00297
00301 string MovetoStringWithKills(const Move& m) const;
00302
00310 bool checkKillForward(ob_square_t from, ob_square_t to, KillInfo* killInfo=NULL) const;
00311
00315 u64 calcAfterStepSignature(const Step& step) const;
00316
00323 int generateAllStepsNoPass(ob_player_t, StepArray&) const;
00324
00330 int generateAllSteps(ob_player_t, StepArray&) const;
00331
00339 void generateStepsForPiece(
00340 ob_square_t square, StepArray& steps, uint& stepsNum) const;
00341
00350 void getHeuristics(const StepArray& steps, uint stepsNum, HeurArray& heurs) const;
00351
00352 u64 getSignature() const;
00353 ob_player_t getWinner() const;
00354
00358 ob_player_t gameOver() const;
00359
00367 bool canContinue(const Move& move) const;
00368
00374 bool canPass() const;
00375
00379 Step lastStep() const;
00380
00381 private:
00388 void init(bool newGame=false);
00389
00396 bool initFromPositionStream(istream& ss);
00397
00403 void afterPositionLoad();
00404
00410 ob_player_t sideCharToPlayer(char side) const;
00411
00422 recordAction_e parseRecordActionToken(const string& token, ob_player_t& player,
00423 ob_piece_t& piece, ob_square_t& from, ob_square_t& to);
00424
00431 PiecePair parsePieceChar(char pieceChar);
00432
00439 void initZobrist() const;
00440
00446 void makeSignature();
00447
00456 void makeStep(const Step& step);
00457
00464 bool findRandomStep(Step&) const;
00465
00466
00467
00475 Step chooseStepWithKnowledge(StepArray& steps, uint stepsNum) const;
00476
00482 float evaluateStep(const Step& step) const;
00483
00489 bool checkKill(ob_square_t square);
00490
00496 void performKill(ob_square_t trapPos);
00497
00505 bool stepIsVirtualPass( Step& ) const;
00506
00514 bool stepIsThirdRepetition(const Step& ) const;
00515
00522 bool hasFriend(ob_square_t) const;
00523
00530 inline bool hasFriend(ob_square_t, ob_player_t owner) const;
00531
00538 bool hasTwoFriends(ob_square_t, ob_player_t) const;
00539
00545 bool hasStrongerEnemy(ob_square_t) const;
00546
00553 inline bool hasStrongerEnemy(ob_square_t, ob_player_t owner, ob_piece_t piece) const;
00554
00560 bool isFrozen(ob_square_t) const;
00561
00562 uint getStepCount() const;
00563 u64 getPreMoveSignature() const;
00564
00568 void setSquare(ob_square_t, ob_player_t, ob_piece_t);
00569
00573 void clearSquare(ob_square_t);
00574
00575 string allStepsToString() const;
00576 void dumpAllSteps() const;
00577 void dump() const;
00578
00579
00580
00581 static bool classInit;
00582 static ThirdRep* thirdRep_;
00583
00584 board_t board_;
00585 bool frozenBoard_[OB_SQUARE_NUM];
00586
00587 PieceArray pieceArray[2];
00588 uint rabbitsNum[2];
00589
00590 StepArray stepArray;
00591 uint stepArrayLen;
00592
00593 u64 signature_;
00594 u64 preMoveSignature_;
00595
00597 Step lastStep_;
00598
00599 uint moveCount_;
00600
00601
00602
00603 uint stepCount_;
00604
00605 ob_player_t toMove_;
00606 uint toMoveIndex_;
00607 ob_player_t winner_;
00608
00609 friend class Eval;
00610
00611 };
00612
00613
00617 class OB_SimplePlayout
00618 {
00619 public:
00623 OB_SimplePlayout(OB_Board*, uint maxPlayoutLength, uint evalAfterLength);
00624
00632 void doPlayout();
00633
00637 uint getPlayoutLength();
00638
00639 protected:
00645 void playOne();
00646
00650 bool hasWinner();
00651
00652 OB_SimplePlayout();
00653
00654 OB_Board* board_;
00655 uint playoutLength_;
00656 uint maxPlayoutLength_;
00657 uint evalAfterLength_;
00658
00659 };