Skip to content

Commit

Permalink
allow pass rule
Browse files Browse the repository at this point in the history
  • Loading branch information
hzyhhzy committed Sep 22, 2024
1 parent e24de85 commit 6c695b6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
14 changes: 13 additions & 1 deletion cpp/game/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ Hash128 Board::ZOBRIST_BOARD_HASH[MAX_ARR_SIZE][4];
Hash128 Board::ZOBRIST_PLAYER_HASH[4];
Hash128 Board::ZOBRIST_MOVENUM_HASH[MAX_ARR_SIZE];
Hash128 Board::ZOBRIST_BOARD_HASH2[MAX_ARR_SIZE][4];
const Hash128 Board::ZOBRIST_GAME_IS_OVER = //Based on sha256 hash of Board::ZOBRIST_GAME_IS_OVER
const Hash128 Board::ZOBRIST_GAME_IS_OVER = // Based on sha256 hash of Board::ZOBRIST_GAME_IS_OVER
Hash128(0xb6f9e465597a77eeULL, 0xf1d583d960a4ce7fULL);
const Hash128 Board::ZOBRIST_LAST_MOVE_PASS =
Hash128(0x890d44c415a4224cULL, 0xdea1afbf9c07a697ULL);

//LOCATION--------------------------------------------------------------------------------
Loc Location::getLoc(int x, int y, int x_size)
Expand Down Expand Up @@ -109,6 +111,7 @@ Board::Board(const Board& other)

movenum = other.movenum;
stonenum = other.stonenum;
isLastMovePass = other.isLastMovePass;
pos_hash = other.pos_hash;

memcpy(adj_offsets, other.adj_offsets, sizeof(short)*8);
Expand All @@ -128,6 +131,7 @@ void Board::init(int xS, int yS)

movenum = 0;
stonenum = 0;
isLastMovePass = false;

for(int y = 0; y < y_size; y++)
{
Expand Down Expand Up @@ -310,8 +314,14 @@ void Board::playMoveAssumeLegal(Loc loc, Player pla)
//Pass?
if(loc == PASS_LOC)
{
if(!isLastMovePass)
pos_hash ^= ZOBRIST_LAST_MOVE_PASS;
isLastMovePass = true;
return;
}
if(isLastMovePass)
pos_hash ^= ZOBRIST_LAST_MOVE_PASS;
isLastMovePass = false;
setStone(loc, pla);
}

Expand Down Expand Up @@ -364,6 +374,8 @@ void Board::checkConsistency() const {
}

tmp_pos_hash ^= ZOBRIST_MOVENUM_HASH[movenum];
if(isLastMovePass)
tmp_pos_hash ^= ZOBRIST_LAST_MOVE_PASS;

if(pos_hash != tmp_pos_hash)
throw StringError(errLabel + "Pos hash does not match expected");
Expand Down
5 changes: 4 additions & 1 deletion cpp/game/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
#ifdef COMPILE_MAX_BOARD_LEN
static_assert(COMPILE_MAX_BOARD_LEN should not be defined);
#endif
#define COMPILE_MAX_BOARD_LEN 9
#define COMPILE_MAX_BOARD_LEN 15

static const bool LONGWIN = true;
static const bool ALLOW_PASS = false;



Expand Down Expand Up @@ -121,6 +122,7 @@ struct Board
static Hash128 ZOBRIST_BOARD_HASH2[MAX_ARR_SIZE][4];
static Hash128 ZOBRIST_PLAYER_HASH[4];
static const Hash128 ZOBRIST_GAME_IS_OVER;
static const Hash128 ZOBRIST_LAST_MOVE_PASS;

//Structs---------------------------------------

Expand Down Expand Up @@ -182,6 +184,7 @@ struct Board
int movenum; //how many moves
int stonenum; //how many stones on board

bool isLastMovePass;
/* PointList empty_list; //List of all empty locations on board */

Hash128 pos_hash; //A zobrist hash of the current board position (does not include ko point or player to move)
Expand Down
3 changes: 2 additions & 1 deletion cpp/game/boardhistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ void BoardHistory::makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player mo
isNoResult = false;
isResignation = false;

bool isLastMovePass = board.isLastMovePass;
board.playMoveAssumeLegal(moveLoc,movePla);


Expand All @@ -227,7 +228,7 @@ void BoardHistory::makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player mo
presumedNextMovePla = getOpp(movePla);


Color maybeWinner = GameLogic::checkWinnerAfterPlayed(board, *this, movePla, moveLoc);
Color maybeWinner = GameLogic::checkWinnerAfterPlayed(board, *this, movePla, moveLoc, isLastMovePass);
if(maybeWinner!=C_WALL) { //game finished
setWinner(maybeWinner);
}
Expand Down
20 changes: 14 additions & 6 deletions cpp/game/gamelogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,23 @@ Color GameLogic::checkWinnerAfterPlayed(
const Board& board,
const BoardHistory& hist,
Player pla,
Loc loc) {

if(loc == Board::PASS_LOC)
return getOpp(pla); //pass is not allowed
Loc loc,
bool isLastMovePass) {

if(loc == Board::PASS_LOC) {
if (ALLOW_PASS)
{
if(isLastMovePass)
return C_EMPTY;
}
else
return getOpp(pla); // pass is not allowed
}
if(isFour(board, pla, LONGWIN, loc))
return pla;


if(board.movenum >= board.x_size * board.y_size)
if(board.stonenum >= board.x_size * board.y_size)
return C_EMPTY;

return C_WALL;
Expand All @@ -91,7 +99,7 @@ void GameLogic::ResultsBeforeNN::init(const Board& board, const BoardHistory& hi
int8_t Board::movePriority(Loc loc, Player pla) const {
if(!isLegal(loc, pla))
return -2;
if(loc == PASS_LOC)
if(!ALLOW_PASS && loc == PASS_LOC)
return -1;
if(isFour(*this, pla, LONGWIN, loc))
return 2;
Expand Down
2 changes: 1 addition & 1 deletion cpp/game/gamelogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace GameLogic {
static const MovePriority MP_ILLEGAL = -1;//illegal moves

//C_EMPTY = draw, C_WALL = not finished
Color checkWinnerAfterPlayed(const Board& board, const BoardHistory& hist, Player pla, Loc loc);
Color checkWinnerAfterPlayed(const Board& board, const BoardHistory& hist, Player pla, Loc loc, bool isLastMovePass);


//some results calculated before calculating NN
Expand Down
2 changes: 2 additions & 0 deletions cpp/neuralnet/nninputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ void NNInputs::fillRowV7(

rowGlobal[6] = board.x_size % 2 == 1;
rowGlobal[7] = board.y_size % 2 == 1;
rowGlobal[8] = board.isLastMovePass;


// Parameter 0 noResultUtilityForWhite, when draw, white's win rate = 0.5*(noResultUtilityForWhite+1)
rowGlobal[14] = pla == C_WHITE ? nnInputParams.noResultUtilityForWhite : -nnInputParams.noResultUtilityForWhite;
Expand Down

0 comments on commit 6c695b6

Please sign in to comment.