Skip to content

Commit

Permalink
add Gale (fairy-stockfish#724)
Browse files Browse the repository at this point in the history
and improve bitboard parsing
  • Loading branch information
RainRat authored Oct 31, 2023
1 parent a621470 commit dc28770
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,36 @@ namespace {
}

template <> bool set(const std::string& value, Bitboard& target) {
char file;
int rank;
std::string symbol;
std::stringstream ss(value);
target = 0;
while (!ss.eof() && ss >> file && file != '-' && ss >> rank)
while (!ss.eof() && ss >> symbol && symbol != "-")
{
if (Rank(rank - 1) > RANK_MAX || (file != '*' && File(tolower(file) - 'a') > FILE_MAX))
if (symbol.back() == '*') {
if (isalpha(symbol[0]) && symbol.length() == 2) {
char file = tolower(symbol[0]);
if (File(file - 'a') > FILE_MAX) return false;
target |= file_bb(File(file - 'a'));
} else {
return false;
}
} else if (symbol[0] == '*') {
int rank = std::stoi(symbol.substr(1));
if (Rank(rank - 1) > RANK_MAX) return false;
target |= rank_bb(Rank(rank - 1));
} else if (isalpha(symbol[0]) && symbol.length() > 1) {
char file = tolower(symbol[0]);
int rank = std::stoi(symbol.substr(1));
if (Rank(rank - 1) > RANK_MAX || File(file - 'a') > FILE_MAX) return false;
target |= square_bb(make_square(File(file - 'a'), Rank(rank - 1)));
} else {
return false;
target |= file == '*' ? rank_bb(Rank(rank - 1)) : square_bb(make_square(File(tolower(file) - 'a'), Rank(rank - 1)));
}
}
return !ss.fail();
}


template <> bool set(const std::string& value, CastlingRights& target) {
char c;
CastlingRights castlingRight;
Expand Down Expand Up @@ -507,6 +524,10 @@ Variant* VariantParser<DoCheck>::parse(Variant* v) {
parse_attribute("connectHorizontal", v->connectHorizontal);
parse_attribute("connectVertical", v->connectVertical);
parse_attribute("connectDiagonal", v->connectDiagonal);
parse_attribute("connectRegion1White", v->connectRegion1[WHITE]);
parse_attribute("connectRegion2White", v->connectRegion2[WHITE]);
parse_attribute("connectRegion1Black", v->connectRegion1[BLACK]);
parse_attribute("connectRegion2Black", v->connectRegion2[BLACK]);
parse_attribute("connectNxN", v->connectNxN);
parse_attribute("materialCounting", v->materialCounting);
parse_attribute("countingRule", v->countingRule);
Expand Down
26 changes: 26 additions & 0 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,32 @@ bool Position::is_immediate_game_end(Value& result, int ply) const {
}
}

if ((var->connectRegion1[~sideToMove] & pieces(~sideToMove)) && (var->connectRegion2[~sideToMove] & pieces(~sideToMove)))
{
Bitboard target = var->connectRegion2[~sideToMove];
Bitboard current = var->connectRegion1[~sideToMove] & pieces(~sideToMove);

while (true) {
Bitboard newBitboard = 0;
for (Direction d : var->connect_directions) {
newBitboard |= shift(d, current | newBitboard) & pieces(~sideToMove); // the "| newBitboard" here probably saves a few loops
}

if (newBitboard & target) {
// A connection has been made
result = mated_in(ply);
return true;
}

if (!(newBitboard & ~current)) {
// The expansion got stuck; no further squares to explore
break;
}

current |= newBitboard;
}
}

if (connect_nxn())
{
Bitboard connectors = pieces(~sideToMove);
Expand Down
2 changes: 2 additions & 0 deletions src/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ struct Variant {
bool connectHorizontal = true;
bool connectVertical = true;
bool connectDiagonal = true;
Bitboard connectRegion1[COLOR_NB] = {};
Bitboard connectRegion2[COLOR_NB] = {};
int connectNxN = 0;
MaterialCounting materialCounting = NO_MATERIAL_COUNTING;
CountingRule countingRule = NO_COUNTING;
Expand Down
17 changes: 17 additions & 0 deletions src/variants.ini
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@
# connectVertical: connectN looks at Vertical rows [bool] (default: true)
# connectHorizontal: connectN looks at Horizontal rows [bool] (default: true)
# connectDiagonal: connectN looks at Diagonal rows [bool] (default: true)
# connectRegion1White: connect Region 1 to Region 2 for win. obeys connectVertical, connectHorizontal, connectDiagonal [Bitboard] (default: -)
# connectRegion2White: "
# connectRegion1Black: "
# connectRegion2Black: "
# connectNxN: connect a tight NxN square for win [int] (default: 0)
# materialCounting: enable material counting rules [MaterialCounting] (default: none)
# countingRule: enable counting rules [CountingRule] (default: none)
Expand Down Expand Up @@ -1783,6 +1787,19 @@ extinctionPieceTypes = kq
extinctionPseudoRoyal = true
stalemateValue = loss

#https://www.ludii.games/details.php?keyword=Gale
[gale:snort]
maxRank = 9
maxFile = 9
startFen = 1p1p1p1p1/P1P1P1P1P/1p1p1p1p1/P1P1P1P1P/1p1p1p1p1/P1P1P1P1P/1p1p1p1p1/P1P1P1P1P/1p1p1p1p1
enclosingDrop = none
connectRegion1White = a*
connectRegion2White = i*
connectRegion1Black = *1
connectRegion2Black = *9
#should be impossible anyway
connectDiagonal = false

#https://www.chessvariants.com/boardrules.dir/atlantis.html
[atlantis:chess]
wallingRule = edge
Expand Down

0 comments on commit dc28770

Please sign in to comment.