Skip to content

Commit

Permalink
nninput; count area if draw; fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hzyhhzy committed Oct 8, 2024
1 parent 69160ec commit d482977
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
26 changes: 21 additions & 5 deletions cpp/game/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Color Board::getSubBoardResult(int idx) const{
auto checkLine = [&](Loc loc1, Loc loc2, Loc loc3) {
if(colors[loc1] == C_BLACK && colors[loc2] == C_BLACK && colors[loc3] == C_BLACK)
return C_BLACK;
if(colors[loc1] == C_BLACK && colors[loc2] == C_BLACK && colors[loc3] == C_BLACK)
if(colors[loc1] == C_WHITE && colors[loc2] == C_WHITE && colors[loc3] == C_WHITE)
return C_WHITE;
return C_EMPTY;
};
Expand Down Expand Up @@ -211,7 +211,7 @@ Color Board::updateSubBoardResult(int idx) {
return r;
}

Color Board::getWinner() const {
Color Board::getWinner(int rule) const {
static_assert(CON_LEN == 3, "");
const int lines[8][3] = {
{0,1,2},
Expand All @@ -232,11 +232,26 @@ Color Board::getWinner() const {
}

// full?
for(int li = 0; li < 8; li++) {
if(subBoardResult[li] == C_WALL)
for(int i = 0; i < CON_LEN * CON_LEN; i++) {
if(subBoardResult[i] == C_WALL)
return C_WALL;
}
if(rule == 1)
return C_EMPTY;//SCORING_CON

int score = 0;
for(int i = 0; i < CON_LEN * CON_LEN; i++) {
if(subBoardResult[i] == C_BLACK)
score += 1;
if(subBoardResult[i] == C_WHITE)
score -= 1;
}
if(score > 0)
return C_BLACK;
if(score < 0)
return C_WHITE;
return C_EMPTY;

}

void Board::setLastLocIdx(int x) {
Expand Down Expand Up @@ -348,7 +363,8 @@ bool Board::isLegal(Loc loc, Player pla) const
if(pla != P_BLACK && pla != P_WHITE)
return false;
if(loc == PASS_LOC)
return getWinner() != C_WALL; //pass is not allowed, allow pass after game finished just to avoid some crashes
return getWinner(0) !=
C_WALL; // pass is not allowed, allow pass after game finished just to avoid some crashes

if(!isOnBoard(loc))
return false;
Expand Down
2 changes: 1 addition & 1 deletion cpp/game/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct Board
short adj_offsets[8]; //Indices 0-3: Offsets to add for adjacent points. Indices 4-7: Offsets for diagonal points. 2 and 3 are +x and +y.

int inWhichSubBoard(Loc loc) const;
Color getWinner() const;
Color getWinner(int rule) const;
private:
void init(int xS, int yS);

Expand Down
2 changes: 1 addition & 1 deletion cpp/game/gamelogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Color GameLogic::checkWinnerAfterPlayed(
return getOpp(pla); //pass is not allowed

//write your own logic here
Color winner = board.getWinner();
Color winner = board.getWinner(hist.rules.scoringRule);
if(winner != C_WALL)
return winner;

Expand Down
4 changes: 3 additions & 1 deletion cpp/game/rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ Rules Rules::getTrompTaylorish() {


set<string> Rules::scoringRuleStrings() {
return {"AREA"};
return {"AREA", "CON"};
}

int Rules::parseScoringRule(const string& s) {
if(s == "AREA") return Rules::SCORING_AREA;
if(s == "CON") return Rules::SCORING_CON;
else throw IOError("Rules::parseScoringRule: Invalid scoring rule: " + s);
}

string Rules::writeScoringRule(int scoringRule) {
if(scoringRule == Rules::SCORING_AREA) return string("AREA");
if(scoringRule == Rules::SCORING_CON) return string("CON");
return string("UNKNOWN");
}

Expand Down
5 changes: 3 additions & 2 deletions cpp/game/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

struct Rules {


static const int SCORING_AREA = 0;

static const int SCORING_AREA = 0; //if no connection, whose area bigger wins
static const int SCORING_CON = 1; //only consider connection
int scoringRule;


Expand Down
18 changes: 17 additions & 1 deletion cpp/neuralnet/nninputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,20 @@ void NNInputs::fillRowV7(
else if(stone == opp)
setRowBin(rowBin,pos,2, 1.0f, posStride, featureStride);

int sub = board.inWhichSubBoard(loc);
Color sbr = board.subBoardResult[sub];
if(sbr == pla)
setRowBin(rowBin, pos, 3, 1.0f, posStride, featureStride);
else if(sbr == opp)
setRowBin(rowBin, pos, 4, 1.0f, posStride, featureStride);
else if(sbr == C_EMPTY)
setRowBin(rowBin, pos, 5, 1.0f, posStride, featureStride);
else
{
if(board.lastLocIdx == sub && board.lastLocIdx == -1)
setRowBin(rowBin, pos, 6, 1.0f, posStride, featureStride);
}

}
}

Expand All @@ -539,7 +553,7 @@ void NNInputs::fillRowV7(
setRowBin(
rowBin,
NNPos::locToPos(resultsBeforeNN.myOnlyLoc, board.x_size, nnXLen, nnYLen),
3,
20,
1.0f,
posStride,
featureStride);
Expand All @@ -560,6 +574,8 @@ void NNInputs::fillRowV7(
// Parameter 0 noResultUtilityForWhite, when draw, white's win rate = 0.5*(noResultUtilityForWhite+1)
rowGlobal[0] = pla == C_WHITE ? nnInputParams.noResultUtilityForWhite : -nnInputParams.noResultUtilityForWhite;

rowGlobal[6] = hist.rules.scoringRule == Rules::SCORING_CON;

// Parameter 15 is used because there's actually a discontinuity in how training behavior works when this is
// nonzero, no matter how slightly.
if(nnInputParams.playoutDoublingAdvantage != 0) {
Expand Down

0 comments on commit d482977

Please sign in to comment.