From 5d3af0900b842f81097d0c0f7a2efb9cf2af2c5a Mon Sep 17 00:00:00 2001 From: RainRat Date: Fri, 29 Sep 2023 01:43:19 -0700 Subject: [PATCH 01/29] add Ajax Orthodox, Petty, Haynie (#730) --- src/variants.ini | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/variants.ini b/src/variants.ini index 73bf9e99..104b038c 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -204,7 +204,7 @@ # mustDropType: piece type for which piece drops are mandatory [PieceType] (default: *) # pieceDrops: enable piece drops [bool] (default: false) # dropLoop: captures promoted pieces are not demoted [bool] (default: false) -# capturesToHand: captured pieces are go to opponent's hand [bool] (default: false) +# capturesToHand: captured pieces go to opponent's hand [bool] (default: false) # firstRankPawnDrops: allow pawn drops to first rank [bool] (default: false) # promotionZonePawnDrops: allow pawn drops in promotion zone [bool] (default: false) # dropOnTop: piece drops need to be on top of pieces on board (e.g., for connect4) [bool] (default: false) @@ -1777,4 +1777,37 @@ stalemateValue = loss wallingRule = edge #not ready yet. Other wall variants are "move and wall", this is "move or wall". #need to figure out way to do this ie. write code for: -#wallOrMove = true \ No newline at end of file +#wallOrMove = true + +#https://www.chessvariants.com/rules/ajax-orthodox-chess +[ajax-orthodox:chess] +pieceToCharTable = PNBRQ.............MKpnbrq.............mk +customPiece1 = r:RmF +customPiece2 = n:NmK +customPiece3 = b:BmW +customPiece1 = m:KAD +promotionPieceTypes = mqnbr +startFen = rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[MMmm] w KQkq - 0 1 +pieceDrops = true +whiteDropRegion = *1 +blackDropRegion = *8 + +#https://www.chessvariants.com/small.dir/petty.html +[petty:chess] +maxRank = 6 +maxFile = 5 +startFen = qkbnr/ppppp/5/5/PPPPP/QKBNR w - 0 1 +castling = false +doubleStep = false +promotionRegionWhite = *6 + +#https://www.chessvariants.com/small.dir/haynie.html +[haynie:chess] +maxRank = 6 +maxFile = 6 +startFen = rbqkbr/pppppp/6/6/PPPPPP/RBQKBR w KQkq - 0 1 +doubleStep = false +promotionPieceTypes = rbq +castlingQueensideFile = c +castlingKingsideFile = e +promotionRegionWhite = *6 From 3f40adab6b506d02172291ad36bbab8dd1c105ac Mon Sep 17 00:00:00 2001 From: RainRat Date: Tue, 24 Oct 2023 13:03:24 -0700 Subject: [PATCH 02/29] Add Cfour-anyside, Symphony, Teeko (#731) --- src/parser.cpp | 10 ++++++++-- src/position.cpp | 13 ++++++++++++ src/position.h | 52 ++++++++++++++++++++++++++++++++++++++---------- src/types.h | 2 +- src/variant.h | 2 +- src/variants.ini | 42 +++++++++++++++++++++++++++++++++++--- 6 files changed, 104 insertions(+), 17 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 48b8cc9a..1c4b4faf 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -107,8 +107,10 @@ namespace { : value == "ataxx" ? ATAXX : value == "quadwrangle" ? QUADWRANGLE : value == "snort" ? SNORT + : value == "anyside" ? ANYSIDE + : value == "top" ? TOP : NO_ENCLOSING; - return value == "reversi" || value == "ataxx" || value == "quadwrangle" || value =="snort" || value == "none"; + return value == "reversi" || value == "ataxx" || value == "quadwrangle" || value =="snort" || value =="anyside" || value =="top" || value == "none"; } template <> bool set(const std::string& value, WallingRule& target) { @@ -327,6 +329,10 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("castlingRookPiece", v->castlingRookPieces[WHITE], v->pieceToChar); parse_attribute("castlingRookPiece", v->castlingRookPieces[BLACK], v->pieceToChar); + bool dropOnTop = false; + parse_attribute("dropOnTop", dropOnTop); + if (dropOnTop) v->enclosingDrop=TOP; + // Parse aliases parse_attribute("pawnTypes", v->promotionPawnType[WHITE], v->pieceToChar); parse_attribute("pawnTypes", v->promotionPawnType[BLACK], v->pieceToChar); @@ -433,7 +439,6 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("capturesToHand", v->capturesToHand); parse_attribute("firstRankPawnDrops", v->firstRankPawnDrops); parse_attribute("promotionZonePawnDrops", v->promotionZonePawnDrops); - parse_attribute("dropOnTop", v->dropOnTop); parse_attribute("enclosingDrop", v->enclosingDrop); parse_attribute("enclosingDropStart", v->enclosingDropStart); parse_attribute("whiteDropRegion", v->whiteDropRegion); @@ -502,6 +507,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("connectHorizontal", v->connectHorizontal); parse_attribute("connectVertical", v->connectVertical); parse_attribute("connectDiagonal", v->connectDiagonal); + parse_attribute("connectNxN", v->connectNxN); parse_attribute("materialCounting", v->materialCounting); parse_attribute("countingRule", v->countingRule); parse_attribute("castlingWins", v->castlingWins); diff --git a/src/position.cpp b/src/position.cpp index 39f4888e..c72a07f9 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -2806,6 +2806,19 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { } } } + + if (connect_nxn()) + { + Bitboard connectors = pieces(~sideToMove); + for (int i = 1; i < connect_nxn() && connectors; i++) + connectors &= shift(connectors) & shift(connectors) & shift(connectors); + if (connectors) + { + result = mated_in(ply); + return true; + } + } + // Check for bikjang rule (Janggi) and double passing if (st->pliesFromNull > 0 && ((st->bikjang && st->previous->bikjang) || (st->pass && st->previous->pass))) { diff --git a/src/position.h b/src/position.h index 7b9b4cd8..81c61dd4 100644 --- a/src/position.h +++ b/src/position.h @@ -167,7 +167,6 @@ class Position { bool drop_loop() const; bool captures_to_hand() const; bool first_rank_pawn_drops() const; - bool drop_on_top() const; bool can_drop(Color c, PieceType pt) const; EnclosingRule enclosing_drop() const; Bitboard drop_region(Color c) const; @@ -210,6 +209,7 @@ class Position { bool connect_vertical() const; bool connect_diagonal() const; const std::vector& getConnectDirections() const; + int connect_nxn() const; CheckCount checks_remaining(Color c) const; MaterialCounting material_counting() const; @@ -373,6 +373,7 @@ class Position { void remove_from_hand(Piece pc); void drop_piece(Piece pc_hand, Piece pc_drop, Square s); void undrop_piece(Piece pc_hand, Square s); + Bitboard find_drop_region(Direction dir, Square s, Bitboard occupied) const; }; extern std::ostream& operator<<(std::ostream& os, const Position& pos); @@ -648,11 +649,6 @@ inline bool Position::first_rank_pawn_drops() const { return var->firstRankPawnDrops; } -inline bool Position::drop_on_top() const { - assert(var != nullptr); - return var->dropOnTop; -} - inline EnclosingRule Position::enclosing_drop() const { assert(var != nullptr); return var->enclosingDrop; @@ -666,9 +662,6 @@ inline Bitboard Position::drop_region(Color c) const { inline Bitboard Position::drop_region(Color c, PieceType pt) const { Bitboard b = drop_region(c) & board_bb(c, pt); - // Connect4-style drops - if (drop_on_top()) - b &= shift(pieces()) | Rank1BB; // Pawns on back ranks if (pt == PAWN) { @@ -686,7 +679,6 @@ inline Bitboard Position::drop_region(Color c, PieceType pt) const { if (pt == ROOK && sittuyin_rook_drop()) b &= rank_bb(relative_rank(c, RANK_1, max_rank())); - // Filter out squares where the drop does not enclose at least one opponent's piece if (enclosing_drop()) { // Reversi start @@ -694,6 +686,7 @@ inline Bitboard Position::drop_region(Color c, PieceType pt) const { b &= var->enclosingDropStart; else { + // Filter out squares where the drop does not enclose at least one opponent's piece if (enclosing_drop() == REVERSI) { Bitboard theirs = pieces(~c); @@ -715,6 +708,40 @@ inline Bitboard Position::drop_region(Color c, PieceType pt) const { b &= ~(shift(theirs) | shift(theirs) | shift(theirs) | shift(theirs)); } + else if (enclosing_drop() == ANYSIDE) + { + Bitboard occupied = pieces(); + b = 0ULL; + Bitboard candidates = (shift(occupied) | file_bb(max_file())) & ~occupied; + + for (Rank r = RANK_1; r <= max_rank(); ++r) { + if (!(occupied & make_square(FILE_A, r))) { + b |= lsb(candidates & rank_bb(r)); + } + } + candidates = (shift(occupied) | rank_bb(max_rank())) & ~occupied; + for (File f = FILE_A; f <= max_file(); ++f) { + if (!(occupied & make_square(f, RANK_1))) { + b |= lsb(candidates & file_bb(f)); + } + } + candidates = (shift(occupied) | rank_bb(RANK_1)) & ~occupied; + for (File f = FILE_A; f <= max_file(); ++f) { + if (!(occupied & make_square(f, max_rank()))) { + b |= lsb(candidates & file_bb(f)); + } + } + candidates = (shift(occupied) | file_bb(FILE_A)) & ~occupied; + for (Rank r = RANK_1; r <= max_rank(); ++r) { + if (!(occupied & make_square(max_file(), r))) { + b |= lsb(candidates & rank_bb(r)); + } + } + } + else if (enclosing_drop() == TOP) + { + b &= shift(pieces()) | Rank1BB; + } else { assert(enclosing_drop() == ATAXX); @@ -1026,6 +1053,11 @@ inline const std::vector& Position::getConnectDirections() const { return var->connect_directions; } +inline int Position::connect_nxn() const { + assert(var != nullptr); + return var->connectNxN; +} + inline CheckCount Position::checks_remaining(Color c) const { return st->checksRemaining[c]; } diff --git a/src/types.h b/src/types.h index 2fa03e7c..5abfac80 100644 --- a/src/types.h +++ b/src/types.h @@ -302,7 +302,7 @@ enum ChasingRule { }; enum EnclosingRule { - NO_ENCLOSING, REVERSI, ATAXX, QUADWRANGLE, SNORT + NO_ENCLOSING, REVERSI, ATAXX, QUADWRANGLE, SNORT, ANYSIDE, TOP }; enum WallingRule { diff --git a/src/variant.h b/src/variant.h index e8899f39..79cdc5a6 100644 --- a/src/variant.h +++ b/src/variant.h @@ -95,7 +95,6 @@ struct Variant { bool capturesToHand = false; bool firstRankPawnDrops = false; bool promotionZonePawnDrops = false; - bool dropOnTop = false; EnclosingRule enclosingDrop = NO_ENCLOSING; Bitboard enclosingDropStart = 0; Bitboard whiteDropRegion = AllSquares; @@ -153,6 +152,7 @@ struct Variant { bool connectHorizontal = true; bool connectVertical = true; bool connectDiagonal = true; + int connectNxN = 0; MaterialCounting materialCounting = NO_MATERIAL_COUNTING; CountingRule countingRule = NO_COUNTING; CastlingRights castlingWins = NO_CASTLING; diff --git a/src/variants.ini b/src/variants.ini index 104b038c..38c6cc7f 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -130,7 +130,17 @@ # [MaterialCounting]: material counting rules for adjudication [janggi, unweighted, whitedrawodds, blackdrawodds, none] # [CountingRule]: makruk, cambodian, or ASEAN counting rules [makruk, cambodian, asean, none] # [ChasingRule]: xiangqi chasing rules [axf, none] -# [EnclosingRule]: reversi or ataxx enclosing rules [reversi, ataxx, quadwrangle, snort, none] +# [EnclosingRule]: reversi, ataxx, etc. enclosing rules [reversi, ataxx, quadwrangle, snort, anyside, top, none] +# - in enclosingDrop: +# - reversi: must enclose opponent's pieces between yours by Queen move +# - ataxx: must be adjacent to own piece by King move +# - snort: most *not* be adjacent to opponent's piece by Wazir move +# - anyside: must be reached by inserting from an edge and sliding to opposite edge +# - top: must be reached by inserting from top and sliding to bottom (ie. Connect 4) +# - in flipEnclosedPieces: +# - reversi: flip opponent's pieces enclosed between yours by Queen move +# - quadwrangle: if a normal move *or* a drop with a friendly piece adjacent by King move, then flip opponent's pieces adjacent by King move +# - ataxx: flip opponent's pieces adjacent by King move # [WallingRule]: wall-placing rule [arrow, duck, edge, past, static, none] # - arrow: copies piece movement (ie. Game of the Amazons) # - duck: mobile square (ie. Duck chess) @@ -207,7 +217,7 @@ # capturesToHand: captured pieces go to opponent's hand [bool] (default: false) # firstRankPawnDrops: allow pawn drops to first rank [bool] (default: false) # promotionZonePawnDrops: allow pawn drops in promotion zone [bool] (default: false) -# dropOnTop: piece drops need to be on top of pieces on board (e.g., for connect4) [bool] (default: false) +# dropOnTop: DEPRECATED, use "enclosingDrop = top" # enclosingDrop: require piece drop to enclose pieces [EnclosingRule] (default: none) # enclosingDropStart: drop region for starting phase disregarding enclosingDrop (e.g., for reversi) [Bitboard] # whiteDropRegion: restrict region for piece drops of all white pieces [Bitboard] @@ -269,6 +279,7 @@ # 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) +# 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) # castlingWins: Specified castling moves are win conditions. Losing these rights is losing. [CastlingRights] (default: -) @@ -490,7 +501,7 @@ maxFile = 7 immobile = p startFen = 7/7/7/7/7/7[PPPPPPPPPPPPPPPPPPPPPppppppppppppppppppppp] w - - 0 1 pieceDrops = true -dropOnTop = true +enclosingDrop = top doubleStep = false castling = false stalemateValue = draw @@ -1801,6 +1812,15 @@ castling = false doubleStep = false promotionRegionWhite = *6 +#https://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id=655 +[teeko:picaria] +maxRank = 5 +maxFile = 5 +connectN = 4 +connectNxN = 2 +customPiece1 = p:mK +startFen = 5/5/5/5/5[PPPPpppp] w - - 0 1 + #https://www.chessvariants.com/small.dir/haynie.html [haynie:chess] maxRank = 6 @@ -1811,3 +1831,19 @@ promotionPieceTypes = rbq castlingQueensideFile = c castlingKingsideFile = e promotionRegionWhite = *6 + +#https://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id=1723 +[symphony:tictactoe] +maxRank = 8 +maxFile = 8 +connectN = 5 +customPiece1 = p:mfsW +nFoldRule = 3 +startFen = 8/8/8/8/8/8/8/8[PPPPPPpppppp] w - - 0 + +#https://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id=734 +#am calling it cfour-anyside so it's less confusable with roll-ing-to-four +[cfour-anyside:cfour] +maxRank = 7 +startFen = 7/7/7/7/7/7/7[PPPPPPPPPPPPPPPPPPPPPPPPPpppppppppppppppppppppppp] w - - 0 1 +enclosingDrop = anyside From 88f4b3d1505d6405910e1f41bf3341b5c6f0cb9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bajusz=20Tam=C3=A1s?= Date: Wed, 25 Oct 2023 14:29:05 +0200 Subject: [PATCH 03/29] Update wheels.yml (#742) --- .github/workflows/wheels.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 1eb31a56..ed14ee97 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/setup-python@v3 - name: Install cibuildwheel - run: python -m pip install cibuildwheel==2.11.2 + run: python -m pip install cibuildwheel==2.16.2 - name: Build wheels run: python -m cibuildwheel --output-dir wheelhouse @@ -31,7 +31,7 @@ jobs: env: MACOSX_DEPLOYMENT_TARGET: "10.14" CIBW_ARCHS_MACOS: "x86_64 arm64" - CIBW_SKIP: "pp* *-win32 *-manylinux_i686 *-musllinux_* cp36-*" + CIBW_SKIP: "pp* *-win32 *-manylinux_i686 *-musllinux_* cp36-* cp37-*" CIBW_TEST_COMMAND: python {project}/test.py - uses: actions/upload-artifact@v3 From a621470b91757699f935ba06d5f4bf48a60574b1 Mon Sep 17 00:00:00 2001 From: RainRat Date: Wed, 25 Oct 2023 13:20:50 -0700 Subject: [PATCH 04/29] add La Mancha, Argess, 4 Kings Quasi Shatranj (#739) --- src/variants.ini | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/variants.ini b/src/variants.ini index 38c6cc7f..926fa423 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -1832,6 +1832,44 @@ castlingQueensideFile = c castlingKingsideFile = e promotionRegionWhite = *6 +#https://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id=1367 +[la-mancha-squeez:snailtrail] +pieceToCharTable = P.....................p..................... +maxRank = 9 +maxFile = 9 +startFen = p7P/9/9/9/3p*P3/9/9/9/p7P w 0 1 + +[la-mancha-duel:la-mancha-squeez] +customPiece1 = p:K + +#https://www.chessvariants.com/diffsetup.dir/argess.html +[argess:chess] +pawnTypes = p +customPiece1 = p:mWcF +#yes, black moves first +startFen = rppppnbk/6qb/7n/7p/PPPP3p/RNPP3p/BQNP3p/KBRP3r b 0 1 +castling = false +promotionRegionWhite = g8 h8 h7 +promotionRegionBlack = a1 a2 b1 + +#https://www.chessvariants.com/rules/4-kings-quasi-shatranj +[quasi-shatranj:twokings2] +pieceToCharTable = PN....E...G..FZ....IAKpn....e...g..fz....iak +maxRank = 10 +maxFile = 10 +customPiece1 = a:AD +customPiece2 = e:AF +customPiece3 = f:AW +customPiece4 = i:DF +customPiece5 = z:DW +customPiece6 = g:K +extinctionPieceCount = 3 +startFen = kifkaakfik/znegaagenz/pppppppppp/10/10/10/10/PPPPPPPPPP/ZNEGAAGENZ/KIFKAAKFIK w 0 1 +promotionRegionWhite = *10 +promotionPieceTypes = zangief +doubleStepRegionWhite = *3 +doubleStepRegionBlack = *8 + #https://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id=1723 [symphony:tictactoe] maxRank = 8 @@ -1847,3 +1885,4 @@ startFen = 8/8/8/8/8/8/8/8[PPPPPPpppppp] w - - 0 maxRank = 7 startFen = 7/7/7/7/7/7/7[PPPPPPPPPPPPPPPPPPPPPPPPPpppppppppppppppppppppppp] w - - 0 1 enclosingDrop = anyside + From dc28770df1a9394e231f2985a7b188571df9655a Mon Sep 17 00:00:00 2001 From: RainRat Date: Tue, 31 Oct 2023 03:26:38 -0700 Subject: [PATCH 05/29] add Gale (#724) and improve bitboard parsing --- src/parser.cpp | 31 ++++++++++++++++++++++++++----- src/position.cpp | 26 ++++++++++++++++++++++++++ src/variant.h | 2 ++ src/variants.ini | 17 +++++++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 1c4b4faf..c925b2de 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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; @@ -507,6 +524,10 @@ Variant* VariantParser::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); diff --git a/src/position.cpp b/src/position.cpp index c72a07f9..9b8ce391 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -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); diff --git a/src/variant.h b/src/variant.h index 79cdc5a6..49bc5440 100644 --- a/src/variant.h +++ b/src/variant.h @@ -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; diff --git a/src/variants.ini b/src/variants.ini index 926fa423..b7dd011f 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -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) @@ -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 From ae72c8e94f52804c5a325a7197e349d3a236e512 Mon Sep 17 00:00:00 2001 From: RainRat Date: Sun, 26 Nov 2023 13:30:48 -0800 Subject: [PATCH 06/29] Cfour-misere, per-color passing (#746) --- src/movegen.cpp | 4 ++-- src/parser.cpp | 11 +++++++++-- src/position.cpp | 14 +++++++------- src/position.h | 12 ++++++------ src/variant.cpp | 12 ++++++++---- src/variant.h | 5 +++-- src/variants.ini | 8 ++++++++ src/xboard.cpp | 2 +- 8 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/movegen.cpp b/src/movegen.cpp index b658619d..43807bb0 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -441,7 +441,7 @@ namespace { } // Workaround for passing: Execute a non-move with any piece - if (pos.pass() && !pos.count(Us) && pos.pieces(Us)) + if (pos.pass(Us) && !pos.count(Us) && pos.pieces(Us)) *moveList++ = make(lsb(pos.pieces(Us)), lsb(pos.pieces(Us))); } @@ -454,7 +454,7 @@ namespace { moveList = make_move_and_gating(pos, moveList, Us, ksq, pop_lsb(b)); // Passing move by king - if (pos.pass()) + if (pos.pass(Us)) *moveList++ = make(ksq, ksq); if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING)) diff --git a/src/parser.cpp b/src/parser.cpp index c925b2de..7535e686 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -475,8 +475,14 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("seirawanGating", v->seirawanGating); parse_attribute("cambodianMoves", v->cambodianMoves); parse_attribute("diagonalLines", v->diagonalLines); - parse_attribute("pass", v->pass); - parse_attribute("passOnStalemate", v->passOnStalemate); + parse_attribute("pass", v->pass[WHITE]); + parse_attribute("pass", v->pass[BLACK]); + parse_attribute("passWhite", v->pass[WHITE]); + parse_attribute("passBlack", v->pass[BLACK]); + parse_attribute("passOnStalemate", v->passOnStalemate[WHITE]); + parse_attribute("passOnStalemate", v->passOnStalemate[BLACK]); + parse_attribute("passOnStalemateWhite", v->passOnStalemate[WHITE]); + parse_attribute("passOnStalemateBlack", v->passOnStalemate[BLACK]); parse_attribute("makpongRule", v->makpongRule); parse_attribute("flyingGeneral", v->flyingGeneral); parse_attribute("soldierPromotionRank", v->soldierPromotionRank); @@ -529,6 +535,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("connectRegion1Black", v->connectRegion1[BLACK]); parse_attribute("connectRegion2Black", v->connectRegion2[BLACK]); parse_attribute("connectNxN", v->connectNxN); + parse_attribute("connectValue", v->connectValue); parse_attribute("materialCounting", v->materialCounting); parse_attribute("countingRule", v->countingRule); parse_attribute("castlingWins", v->castlingWins); diff --git a/src/position.cpp b/src/position.cpp index 9b8ce391..a7ada05c 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1092,7 +1092,7 @@ bool Position::legal(Move m) const { return false; // Illegal king passing move - if (pass_on_stalemate() && is_pass(m) && !checkers()) + if (pass_on_stalemate(us) && is_pass(m) && !checkers()) { for (const auto& move : MoveList(*this)) if (!is_pass(move) && legal(move)) @@ -1557,7 +1557,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { Piece captured = piece_on(type_of(m) == EN_PASSANT ? capture_square(to) : to); if (to == from) { - assert((type_of(m) == PROMOTION && sittuyin_promotion()) || (is_pass(m) && pass())); + assert((type_of(m) == PROMOTION && sittuyin_promotion()) || (is_pass(m) && pass(us))); captured = NO_PIECE; } st->capturedpromoted = is_promoted(to); @@ -2126,7 +2126,7 @@ void Position::undo_move(Move m) { assert(type_of(m) == DROP || empty(from) || type_of(m) == CASTLING || is_gating(m) || (type_of(m) == PROMOTION && sittuyin_promotion()) - || (is_pass(m) && pass())); + || (is_pass(m) && pass(us))); assert(type_of(st->capturedPiece) != KING); // Reset wall squares @@ -2564,7 +2564,7 @@ bool Position::see_ge(Move m, Value threshold) const { return bool(res); } -/// Position::is_optinal_game_end() tests whether the position may end the game by +/// Position::is_optional_game_end() tests whether the position may end the game by /// 50-move rule, by repetition, or a variant rule that allows a player to claim a game result. bool Position::is_optional_game_end(Value& result, int ply, int countStarted) const { @@ -2801,7 +2801,7 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { b &= shift(d, b); if (b) { - result = mated_in(ply); + result = convert_mate_value(-var->connectValue, ply); return true; } } @@ -2820,7 +2820,7 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { if (newBitboard & target) { // A connection has been made - result = mated_in(ply); + result = convert_mate_value(-var->connectValue, ply); return true; } @@ -2840,7 +2840,7 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { connectors &= shift(connectors) & shift(connectors) & shift(connectors); if (connectors) { - result = mated_in(ply); + result = convert_mate_value(-var->connectValue, ply); return true; } } diff --git a/src/position.h b/src/position.h index 81c61dd4..4ee93665 100644 --- a/src/position.h +++ b/src/position.h @@ -182,8 +182,8 @@ class Position { bool seirawan_gating() const; bool cambodian_moves() const; Bitboard diagonal_lines() const; - bool pass() const; - bool pass_on_stalemate() const; + bool pass(Color c) const; + bool pass_on_stalemate(Color c) const; Bitboard promoted_soldiers(Color c) const; bool makpong() const; EnclosingRule flip_enclosed_pieces() const; @@ -812,14 +812,14 @@ inline Bitboard Position::diagonal_lines() const { return var->diagonalLines; } -inline bool Position::pass() const { +inline bool Position::pass(Color c) const { assert(var != nullptr); - return var->pass || var->passOnStalemate; + return var->pass[c] || var->passOnStalemate[c]; } -inline bool Position::pass_on_stalemate() const { +inline bool Position::pass_on_stalemate(Color c) const { assert(var != nullptr); - return var->passOnStalemate; + return var->passOnStalemate[c]; } inline Bitboard Position::promoted_soldiers(Color c) const { diff --git a/src/variant.cpp b/src/variant.cpp index 8a424681..cd565baf 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -1136,7 +1136,8 @@ namespace { v->immobilityIllegal = false; v->stalemateValue = -VALUE_MATE; v->stalematePieceCount = true; - v->passOnStalemate = true; + v->passOnStalemate[WHITE] = true; + v->passOnStalemate[BLACK] = true; v->enclosingDrop = ATAXX; v->flipEnclosedPieces = ATAXX; v->materialCounting = UNWEIGHTED_MATERIAL; @@ -1160,7 +1161,8 @@ namespace { v->immobilityIllegal = false; v->stalemateValue = -VALUE_MATE; v->stalematePieceCount = true; - v->passOnStalemate = false; + v->passOnStalemate[WHITE] = false; + v->passOnStalemate[BLACK] = false; v->enclosingDrop = REVERSI; v->enclosingDropStart = make_bitboard(SQ_D4, SQ_E4, SQ_D5, SQ_E5); v->flipEnclosedPieces = REVERSI; @@ -1172,7 +1174,8 @@ namespace { Variant* flipello_variant() { Variant* v = flipersi_variant()->init(); v->startFen = "8/8/8/3pP3/3Pp3/8/8/8[PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPpppppppppppppppppppppppppppppppp] w 0 1"; - v->passOnStalemate = true; + v->passOnStalemate[WHITE] = true; + v->passOnStalemate[BLACK] = true; return v; } // Minixiangqi @@ -1742,7 +1745,8 @@ namespace { v->materialCounting = JANGGI_MATERIAL; v->diagonalLines = make_bitboard(SQ_D1, SQ_F1, SQ_E2, SQ_D3, SQ_F3, SQ_D8, SQ_F8, SQ_E9, SQ_D10, SQ_F10); - v->pass = true; + v->pass[WHITE] = true; + v->pass[BLACK] = true; v->nFoldValue = VALUE_DRAW; v->perpetualCheckIllegal = true; return v; diff --git a/src/variant.h b/src/variant.h index 49bc5440..b9174ad0 100644 --- a/src/variant.h +++ b/src/variant.h @@ -111,8 +111,8 @@ struct Variant { bool seirawanGating = false; bool cambodianMoves = false; Bitboard diagonalLines = 0; - bool pass = false; - bool passOnStalemate = false; + bool pass[COLOR_NB] = {false, false}; + bool passOnStalemate[COLOR_NB] = {false, false}; bool makpongRule = false; bool flyingGeneral = false; Rank soldierPromotionRank = RANK_1; @@ -155,6 +155,7 @@ struct Variant { Bitboard connectRegion1[COLOR_NB] = {}; Bitboard connectRegion2[COLOR_NB] = {}; int connectNxN = 0; + Value connectValue = VALUE_MATE; MaterialCounting materialCounting = NO_MATERIAL_COUNTING; CountingRule countingRule = NO_COUNTING; CastlingRights castlingWins = NO_CASTLING; diff --git a/src/variants.ini b/src/variants.ini index b7dd011f..6b3ea740 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -236,7 +236,11 @@ # cambodianMoves: enable special moves of cambodian chess, requires "gating = true" [bool] (default: false) # diagonalLines: enable special moves along diagonal for specific squares (Janggi) [Bitboard] # pass: allow passing [bool] (default: false) +# passWhite: allow passing for white [bool] (default: false) +# passBlack: allow passing for black [bool] (default: false) # passOnStalemate: allow passing in case of stalemate [bool] (default: false) +# passOnStalemateWhite: allow passing in case of stalemate for white [bool] (default: false) +# passOnStalemateBlack: allow passing in case of stalemate for black [bool] (default: false) # makpongRule: the king may not move away from check [bool] (default: false) # flyingGeneral: disallow general face-off like in xiangqi [bool] (default: false) # soldierPromotionRank: restrict soldier to shogi pawn movements until reaching n-th rank [Rank] (default: 1) @@ -284,6 +288,7 @@ # connectRegion1Black: " # connectRegion2Black: " # connectNxN: connect a tight NxN square for win [int] (default: 0) +# connectValue: result in case of connect [Value] (default: win) # materialCounting: enable material counting rules [MaterialCounting] (default: none) # countingRule: enable counting rules [CountingRule] (default: none) # castlingWins: Specified castling moves are win conditions. Losing these rights is losing. [CastlingRights] (default: -) @@ -1903,3 +1908,6 @@ maxRank = 7 startFen = 7/7/7/7/7/7/7[PPPPPPPPPPPPPPPPPPPPPPPPPpppppppppppppppppppppppp] w - - 0 1 enclosingDrop = anyside +#http://gamescrafters.berkeley.edu/games.php?game=connect4 +[cfour-misere:cfour] +connectValue = loss diff --git a/src/xboard.cpp b/src/xboard.cpp index 482b5e47..09228edb 100644 --- a/src/xboard.cpp +++ b/src/xboard.cpp @@ -308,7 +308,7 @@ void StateMachine::process_command(std::string token, std::istringstream& is) { std::getline(is >> std::ws, fen); // Check if setboard actually indicates a passing move // to avoid unnecessarily clearing the move history - if (pos.pass()) + if (pos.pass(~pos.side_to_move())) { StateInfo st; Position p; From 3b71d409ff5e66576387174bb4e1983a89869349 Mon Sep 17 00:00:00 2001 From: Joost VandeVondele Date: Sat, 3 Sep 2022 11:03:09 +0200 Subject: [PATCH 07/29] Provide network download fallback in case the base infrastructure for providing the networks https://tests.stockfishchess.org/nns is down, use an alternate github repo for downloading networks during the build. No functional change --- src/Makefile | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Makefile b/src/Makefile index 602beefa..8572369b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -821,25 +821,35 @@ clean: objclean profileclean net: $(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/')) @echo "Default net: $(nnuenet)" - $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet)) + $(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet)) + $(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet)) $(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi)) - @if test -f "$(nnuenet)"; then \ - echo "Already available."; \ - else \ - if [ "x$(curl_or_wget)" = "x" ]; then \ - echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \ - else \ - echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\ - fi; \ - fi; + @if [ "x$(curl_or_wget)" = "x" ]; then \ + echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \ + fi $(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi)) - @if [ "x$(shasum_command)" != "x" ]; then \ - if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \ - echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \ - fi \ - else \ + @if [ "x$(shasum_command)" = "x" ]; then \ echo "shasum / sha256sum not found, skipping net validation"; \ fi + @for nnuedownloadurl in "$(nnuedownloadurl1)" "$(nnuedownloadurl2)"; do \ + if test -f "$(nnuenet)"; then \ + echo "$(nnuenet) available."; \ + else \ + if [ "x$(curl_or_wget)" != "x" ]; then \ + echo "Downloading $${nnuedownloadurl}"; $(curl_or_wget) $${nnuedownloadurl} > $(nnuenet);\ + fi; \ + fi; \ + if [ "x$(shasum_command)" != "x" ]; then \ + if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \ + echo "Removing failed download"; rm -f $(nnuenet); \ + else \ + echo "Network validated"; break; \ + fi; \ + fi; \ + done + @if ! test -f "$(nnuenet)"; then \ + echo "Failed to download $(nnuenet)."; \ + fi # clean binaries and objects objclean: From 8a4d0421de9d9f66a3e89d6892db82b4b00fbfe4 Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Sat, 23 Dec 2023 15:02:28 +0100 Subject: [PATCH 08/29] Adjudicate when board is full (#750) Closes #749. --- setup.py | 2 +- src/parser.cpp | 1 + src/position.cpp | 7 +++++-- src/pyffish.cpp | 2 +- src/variant.cpp | 2 ++ src/variant.h | 1 + src/variants.ini | 1 + test.py | 12 ++++++++++++ 8 files changed, 24 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index a03b8328..ab253c9a 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ sources=sources, extra_compile_args=args) -setup(name="pyffish", version="0.0.78", +setup(name="pyffish", version="0.0.80", description="Fairy-Stockfish Python wrapper", long_description=long_description, long_description_content_type="text/markdown", diff --git a/src/parser.cpp b/src/parser.cpp index 7535e686..48aa6870 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -537,6 +537,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("connectNxN", v->connectNxN); parse_attribute("connectValue", v->connectValue); parse_attribute("materialCounting", v->materialCounting); + parse_attribute("adjudicateFullBoard", v->adjudicateFullBoard); parse_attribute("countingRule", v->countingRule); parse_attribute("castlingWins", v->castlingWins); diff --git a/src/position.cpp b/src/position.cpp index a7ada05c..bf6e60f2 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -2845,18 +2845,21 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { } } - // Check for bikjang rule (Janggi) and double passing - if (st->pliesFromNull > 0 && ((st->bikjang && st->previous->bikjang) || (st->pass && st->previous->pass))) + // Check for bikjang rule (Janggi), double passing, or board running full + if ( (st->pliesFromNull > 0 && ((st->bikjang && st->previous->bikjang) || (st->pass && st->previous->pass))) + || (var->adjudicateFullBoard && !(~pieces() & board_bb()))) { result = var->materialCounting ? convert_mate_value(material_counting_result(), ply) : VALUE_DRAW; return true; } + // Tsume mode: Assume that side with king wins when not in check if (tsumeMode && !count(~sideToMove) && count(sideToMove) && !checkers()) { result = mate_in(ply); return true; } + // Failing to checkmate with virtual pieces is a loss if (two_boards() && !checkers()) { diff --git a/src/pyffish.cpp b/src/pyffish.cpp index 537148c4..d321a57c 100644 --- a/src/pyffish.cpp +++ b/src/pyffish.cpp @@ -54,7 +54,7 @@ void buildPosition(Position& pos, StateListPtr& states, const char *variant, con } extern "C" PyObject* pyffish_version(PyObject* self) { - return Py_BuildValue("(iii)", 0, 0, 78); + return Py_BuildValue("(iii)", 0, 0, 80); } extern "C" PyObject* pyffish_info(PyObject* self) { diff --git a/src/variant.cpp b/src/variant.cpp index cd565baf..386c21d5 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -1141,6 +1141,7 @@ namespace { v->enclosingDrop = ATAXX; v->flipEnclosedPieces = ATAXX; v->materialCounting = UNWEIGHTED_MATERIAL; + v->adjudicateFullBoard = true; v->nMoveRule = 0; v->freeDrops = true; return v; @@ -1167,6 +1168,7 @@ namespace { v->enclosingDropStart = make_bitboard(SQ_D4, SQ_E4, SQ_D5, SQ_E5); v->flipEnclosedPieces = REVERSI; v->materialCounting = UNWEIGHTED_MATERIAL; + v->adjudicateFullBoard = true; return v; } // Flipello diff --git a/src/variant.h b/src/variant.h index b9174ad0..ec199185 100644 --- a/src/variant.h +++ b/src/variant.h @@ -157,6 +157,7 @@ struct Variant { int connectNxN = 0; Value connectValue = VALUE_MATE; MaterialCounting materialCounting = NO_MATERIAL_COUNTING; + bool adjudicateFullBoard = false; CountingRule countingRule = NO_COUNTING; CastlingRights castlingWins = NO_CASTLING; diff --git a/src/variants.ini b/src/variants.ini index 6b3ea740..abc2c709 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -290,6 +290,7 @@ # connectNxN: connect a tight NxN square for win [int] (default: 0) # connectValue: result in case of connect [Value] (default: win) # materialCounting: enable material counting rules [MaterialCounting] (default: none) +# adjudicateFullBoard: apply material counting immediately when board is full [bool] (default: false) # countingRule: enable counting rules [CountingRule] (default: none) # castlingWins: Specified castling moves are win conditions. Losing these rights is losing. [CastlingRights] (default: -) diff --git a/test.py b/test.py index b2d953a5..9587501d 100644 --- a/test.py +++ b/test.py @@ -977,6 +977,13 @@ def test_game_result(self): result = sf.game_result("royalduck", "rnbqk1nr/pppp1ppp/4p3/8/7P/5Pb1/PPPPP*P1/RNBQKBNR w KQkq - 1 4", []) self.assertEqual(result, sf.VALUE_MATE) + def _check_immediate_game_end(self, variant, fen, moves, game_end, game_result=None): + with self.subTest(variant=variant, fen=fen, game_end=game_end, game_result=game_result): + result = sf.is_immediate_game_end(variant, fen, moves) + self.assertEqual(result[0], game_end) + if game_result is not None: + self.assertEqual(result[1], game_result) + def test_is_immediate_game_end(self): result = sf.is_immediate_game_end("capablanca", CAPA, []) self.assertFalse(result[0]) @@ -991,6 +998,11 @@ def test_is_immediate_game_end(self): self.assertTrue(result[0]) self.assertEqual(result[1], -sf.VALUE_MATE) + # full board adjudication + self._check_immediate_game_end("flipello", "pppppppp/pppppppp/pppPpppp/pPpPpppp/pppppppp/pPpPPPPP/ppPpPPpp/pppppppp[PPpp] b - - 63 32", [], True, sf.VALUE_MATE) + self._check_immediate_game_end("ataxx", "PPPpppp/pppPPPp/pPPPPPP/PPPPPPp/ppPPPpp/pPPPPpP/pPPPPPP b - - 99 50", [], True, -sf.VALUE_MATE) + self._check_immediate_game_end("ataxx", "PPPpppp/pppPPPp/pPP*PPP/PP*P*Pp/ppP*Ppp/pPPPPpP/pPPPPPP b - - 99 50", [], True, -sf.VALUE_MATE) + def test_is_optional_game_end(self): result = sf.is_optional_game_end("capablanca", CAPA, []) self.assertFalse(result[0]) From 1466609faa66655b557fff990a44ba21c5476e5d Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Sat, 23 Dec 2023 15:26:05 +0100 Subject: [PATCH 09/29] Use subtests for better testing output --- test.py | 174 ++++++++++++++++++-------------------------------------- 1 file changed, 56 insertions(+), 118 deletions(-) diff --git a/test.py b/test.py index 9587501d..9cf4c539 100644 --- a/test.py +++ b/test.py @@ -985,181 +985,119 @@ def _check_immediate_game_end(self, variant, fen, moves, game_end, game_result=N self.assertEqual(result[1], game_result) def test_is_immediate_game_end(self): - result = sf.is_immediate_game_end("capablanca", CAPA, []) - self.assertFalse(result[0]) + self._check_immediate_game_end("capablanca", CAPA, [], False) # bikjang (facing kings) moves = "e2e3 e9f9 h3d3 e7f7 i1i3 h10i8 i3h3 c10e7 h3h8 i10i9 h8b8 i9g9 d3f3 f9e9 f3f10 e7c10 f10c10 b10c8 c10g10 g9f9 b8c8 a10b10 b3f3 f9h9 a1a2 h9f9 a2d2 b10b9 d2d10 e9d10 c8c10 d10d9 f3f9 i8g9 f9b9 a7a6 g10g7 f7f6 e4e5 c7d7 g1e4 i7i6 e4b6 d9d8 c10c8 d8d9 b9g9 d7d6 b6e8 i6h6 e5e6 f6e6 c1e4 a6b6 e4b6 d6d5 c4c5 d9d10 e3d3 h6i6 c5c6 d5c5" - result = sf.is_immediate_game_end("janggi", JANGGI, moves.split()) - self.assertFalse(result[0]) + self._check_immediate_game_end("janggi", JANGGI, moves.split(), False) moves = "e2e3 e9f9 h3d3 e7f7 i1i3 h10i8 i3h3 c10e7 h3h8 i10i9 h8b8 i9g9 d3f3 f9e9 f3f10 e7c10 f10c10 b10c8 c10g10 g9f9 b8c8 a10b10 b3f3 f9h9 a1a2 h9f9 a2d2 b10b9 d2d10 e9d10 c8c10 d10d9 f3f9 i8g9 f9b9 a7a6 g10g7 f7f6 e4e5 c7d7 g1e4 i7i6 e4b6 d9d8 c10c8 d8d9 b9g9 d7d6 b6e8 i6h6 e5e6 f6e6 c1e4 a6b6 e4b6 d6d5 c4c5 d9d10 e3d3 h6i6 c5c6 d5c5 d3d3" - result = sf.is_immediate_game_end("janggi", JANGGI, moves.split()) - self.assertTrue(result[0]) - self.assertEqual(result[1], -sf.VALUE_MATE) + self._check_immediate_game_end("janggi", JANGGI, moves.split(), True, -sf.VALUE_MATE) # full board adjudication self._check_immediate_game_end("flipello", "pppppppp/pppppppp/pppPpppp/pPpPpppp/pppppppp/pPpPPPPP/ppPpPPpp/pppppppp[PPpp] b - - 63 32", [], True, sf.VALUE_MATE) self._check_immediate_game_end("ataxx", "PPPpppp/pppPPPp/pPPPPPP/PPPPPPp/ppPPPpp/pPPPPpP/pPPPPPP b - - 99 50", [], True, -sf.VALUE_MATE) self._check_immediate_game_end("ataxx", "PPPpppp/pppPPPp/pPP*PPP/PP*P*Pp/ppP*Ppp/pPPPPpP/pPPPPPP b - - 99 50", [], True, -sf.VALUE_MATE) + def _check_optional_game_end(self, variant, fen, moves, game_end, game_result=None): + with self.subTest(variant=variant, fen=fen, game_end=game_end, game_result=game_result): + result = sf.is_optional_game_end(variant, fen, moves) + self.assertEqual(result[0], game_end) + if game_result is not None: + self.assertEqual(result[1], game_result) + def test_is_optional_game_end(self): - result = sf.is_optional_game_end("capablanca", CAPA, []) - self.assertFalse(result[0]) + self._check_optional_game_end("capablanca", CAPA, [], False) # sittuyin stalemate due to optional promotion - result = sf.is_optional_game_end("sittuyin", "1k4PK/3r4/8/8/8/8/8/8[] w - - 0 1", []) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("sittuyin", "1k4PK/3r4/8/8/8/8/8/8[] w - - 0 1", [], True, sf.VALUE_DRAW) # Xiangqi chasing rules # Also see http://www.asianxiangqi.org/English/AXF_rules_Eng.pdf # Direct chase by cannon - result = sf.is_optional_game_end("xiangqi", "2bakabnr/9/r1n1c4/2p1p1p1p/PP7/9/4P1P1P/2C3NC1/9/1NBAKAB1R w - - 0 1", ["c3a3", "a8b8", "a3b3", "b8a8", "b3a3", "a8b8", "a3b3", "b8a8", "b3a3"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "2bakabnr/9/r1n1c4/2p1p1p1p/PP7/9/4P1P1P/2C3NC1/9/1NBAKAB1R w - - 0 1", ["c3a3", "a8b8", "a3b3", "b8a8", "b3a3", "a8b8", "a3b3", "b8a8", "b3a3"], True, sf.VALUE_MATE) # Chase with chasing side to move - result = sf.is_optional_game_end("xiangqi", "2bakabnr/9/r1n1c4/2p1p1p1p/PP7/9/4P1P1P/2C3NC1/9/1NBAKAB1R w - - 0 1", ["c3a3", "a8b8", "a3b3", "b8a8", "b3a3", "a8b8", "a3b3", "b8a8", "b3a3", "a8b8", "a3b3", "b8a8"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], -sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "2bakabnr/9/r1n1c4/2p1p1p1p/PP7/9/4P1P1P/2C3NC1/9/1NBAKAB1R w - - 0 1", ["c3a3", "a8b8", "a3b3", "b8a8", "b3a3", "a8b8", "a3b3", "b8a8", "b3a3", "a8b8", "a3b3", "b8a8"], True, -sf.VALUE_MATE) # Discovered chase by cannon (including pawn capture) - result = sf.is_optional_game_end("xiangqi", "2bakabr1/9/9/r1p1p1p2/p7R/P8/9/9/9/CC1AKA3 w - - 0 1", ["a5a6", "a7b7", "a6b6", "b7a7", "b6a6", "a7b7", "a6b6", "b7a7", "b6a6"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "2bakabr1/9/9/r1p1p1p2/p7R/P8/9/9/9/CC1AKA3 w - - 0 1", ["a5a6", "a7b7", "a6b6", "b7a7", "b6a6", "a7b7", "a6b6", "b7a7", "b6a6"], True, sf.VALUE_MATE) # Chase by soldier (draw) - result = sf.is_optional_game_end("xiangqi", "2bakabr1/9/9/r1p1p1p2/p7R/P8/9/9/9/1C1AKA3 w - - 0 1", ["a5a6", "a7b7", "a6b6", "b7a7", "b6a6", "a7b7", "a6b6", "b7a7", "b6a6"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "2bakabr1/9/9/r1p1p1p2/p7R/P8/9/9/9/1C1AKA3 w - - 0 1", ["a5a6", "a7b7", "a6b6", "b7a7", "b6a6", "a7b7", "a6b6", "b7a7", "b6a6"], True, sf.VALUE_DRAW) # Discovered and anti-discovered chase by cannon - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/5C3/5c3/5C3/9/9/5p3/4K4 w - - 0 1", ["f5d5", "f6d6", "d5f5", "d6f6", "f5d5", "f6d6", "d5f5", "d6f6"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], -sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/5C3/5c3/5C3/9/9/5p3/4K4 w - - 0 1", ["f5d5", "f6d6", "d5f5", "d6f6", "f5d5", "f6d6", "d5f5", "d6f6"], True, -sf.VALUE_MATE) # Mutual chase (draw) - result = sf.is_optional_game_end("xiangqi", "4k4/7n1/9/4pR3/9/9/4P4/9/9/4K4 w - - 0 1", ["f7h7"] + 2 * ["h9f8", "h7h8", "f8g6", "h8g8", "g6i7", "g8g7", "i7h9", "g7h7"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "4k4/7n1/9/4pR3/9/9/4P4/9/9/4K4 w - - 0 1", ["f7h7"] + 2 * ["h9f8", "h7h8", "f8g6", "h8g8", "g6i7", "g8g7", "i7h9", "g7h7"], True, sf.VALUE_DRAW) # Perpetual check vs. intermittent checks - result = sf.is_optional_game_end("xiangqi", "9/3kc4/3a5/3P5/9/4p4/9/4K4/9/3C5 w - - 0 1", 2 * ['d7e7', 'e5d5', 'e7d7', 'd5e5']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "9/3kc4/3a5/3P5/9/4p4/9/4K4/9/3C5 w - - 0 1", 2 * ['d7e7', 'e5d5', 'e7d7', 'd5e5'], True, sf.VALUE_MATE) # Perpetual check by soldier - result = sf.is_optional_game_end("xiangqi", "3k5/9/9/9/9/5p3/9/5p3/5K3/5C3 w - - 0 1", 2 * ['f2e2', 'f3e3', 'e2f2', 'e3f3']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "3k5/4P4/4b4/3C5/4c4/9/9/9/9/5K3 w - - 0 1", 2 * ['d7e7', 'e8g6', 'e7d7', 'g6e8']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "3k5/9/9/9/9/9/9/9/cr1CAK3/9 w - - 0 1", 2 * ['d2d4', 'b2b4', 'd4d2', 'b4b2']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/5C3/5c3/5C3/9/9/5p3/4K4 w - - 0 1", 2 * ['f5d5', 'f6d6', 'd5f5', 'd6f6']) - self.assertTrue(result[0]) - self.assertEqual(result[1], -sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "4k4/9/4b4/2c2nR2/9/9/9/9/9/3K5 w - - 0 1", 2 * ['g7g6', 'f7g9', 'g6g7', 'g9f7']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "3P5/3k5/3nn4/9/9/9/9/9/9/5K3 w - - 0 1", 2 * ['d10e10', 'd9e9', 'e10d10', 'e9d9']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "4ck3/9/9/9/9/2r1R4/9/9/4A4/3AK4 w - - 0 1", 2 * ['e5e4', 'c5c4', 'e4e5', 'c4c5']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) - result = sf.is_optional_game_end("xiangqi", "4k4/9/9/c1c6/9/r8/9/9/C8/3K5 w - - 0 1", 2 * ['a2c2', 'a5c5', 'c2a2', 'c5a5']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "3k5/9/9/9/9/5p3/9/5p3/5K3/5C3 w - - 0 1", 2 * ['f2e2', 'f3e3', 'e2f2', 'e3f3'], True, sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "3k5/4P4/4b4/3C5/4c4/9/9/9/9/5K3 w - - 0 1", 2 * ['d7e7', 'e8g6', 'e7d7', 'g6e8'], True, sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "3k5/9/9/9/9/9/9/9/cr1CAK3/9 w - - 0 1", 2 * ['d2d4', 'b2b4', 'd4d2', 'b4b2'], True, sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/5C3/5c3/5C3/9/9/5p3/4K4 w - - 0 1", 2 * ['f5d5', 'f6d6', 'd5f5', 'd6f6'], True, -sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "4k4/9/4b4/2c2nR2/9/9/9/9/9/3K5 w - - 0 1", 2 * ['g7g6', 'f7g9', 'g6g7', 'g9f7'], True, sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "3P5/3k5/3nn4/9/9/9/9/9/9/5K3 w - - 0 1", 2 * ['d10e10', 'd9e9', 'e10d10', 'e9d9'], True, sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "4ck3/9/9/9/9/2r1R4/9/9/4A4/3AK4 w - - 0 1", 2 * ['e5e4', 'c5c4', 'e4e5', 'c4c5'], True, sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "4k4/9/9/c1c6/9/r8/9/9/C8/3K5 w - - 0 1", 2 * ['a2c2', 'a5c5', 'c2a2', 'c5a5'], True, sf.VALUE_MATE) # Mutual perpetual check - result = sf.is_optional_game_end("xiangqi", "9/4c4/3k5/3r5/9/9/4C4/9/4K4/3R5 w - - 0 1", 2 * ['e4d4', 'd7e7', 'd4e4', 'e7d7']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) - result = sf.is_optional_game_end("xiangqi", "3k5/6c2/9/7P1/6c2/6P2/9/9/9/5K3 w - - 0 1", 2 * ['h7g7', 'g6h6', 'g7h7', 'h6g6']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) - result = sf.is_optional_game_end("xiangqi", "4ck3/9/9/9/9/2r1R1N2/6N2/9/4A4/3AK4 w - - 0 1", 2 * ['e5e4', 'c5c4', 'e4e5', 'c4c5']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/c8/9/P1P6/9/2C6/9/3K5 w - - 0 1", 2 * ['c3a3', 'a7c7', 'a3c3', 'c7a7']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) - result = sf.is_optional_game_end("xiangqi", "4k4/9/r1r6/9/PPPP5/9/9/9/1C7/5K3 w - - 0 1", ['b2a2'] + 2 * ['a8b8', 'a2c2', 'c8d8', 'c2b2', 'b8a8', 'b2d2', 'd8c8', 'd2a2']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "9/4c4/3k5/3r5/9/9/4C4/9/4K4/3R5 w - - 0 1", 2 * ['e4d4', 'd7e7', 'd4e4', 'e7d7'], True, sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "3k5/6c2/9/7P1/6c2/6P2/9/9/9/5K3 w - - 0 1", 2 * ['h7g7', 'g6h6', 'g7h7', 'h6g6'], True, sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "4ck3/9/9/9/9/2r1R1N2/6N2/9/4A4/3AK4 w - - 0 1", 2 * ['e5e4', 'c5c4', 'e4e5', 'c4c5'], True, sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "5k3/9/9/c8/9/P1P6/9/2C6/9/3K5 w - - 0 1", 2 * ['c3a3', 'a7c7', 'a3c3', 'c7a7'], True, sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "4k4/9/r1r6/9/PPPP5/9/9/9/1C7/5K3 w - - 0 1", ['b2a2'] + 2 * ['a8b8', 'a2c2', 'c8d8', 'c2b2', 'b8a8', 'b2d2', 'd8c8', 'd2a2'], True, sf.VALUE_DRAW) # Corner cases # D106: Chariot chases cannon, but attack actually does not change (draw) - result = sf.is_optional_game_end("xiangqi", "3k2b2/4P4/4b4/9/8p/6Bc1/6P1P/3AB4/4pp3/1p1K3R1[] w - - 0 1", 2 * ["h1h2", "h5h4", "h2h1", "h4h5"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "3k2b2/4P4/4b4/9/8p/6Bc1/6P1P/3AB4/4pp3/1p1K3R1[] w - - 0 1", 2 * ["h1h2", "h5h4", "h2h1", "h4h5"], True, sf.VALUE_DRAW) # D39: Chased chariot pinned by horse + mutual chase (controversial if pinned chariot chases) - result = sf.is_optional_game_end("xiangqi", "2baka1r1/C4rN2/9/1Rp1p4/9/9/4P4/9/4A4/4KA3 w - - 0 1", ["b7b9"] + 2 * ["f10e9", "b9b10", "e9f10", "b10b9"]) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "2baka1r1/C4rN2/9/1Rp1p4/9/9/4P4/9/4A4/4KA3 w - - 0 1", ["b7b9"] + 2 * ["f10e9", "b9b10", "e9f10", "b10b9"], True, sf.VALUE_MATE) # D39: Chased chariot pinned by horse + mutual chase (controversial if pinned chariot chases) - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/9/9/9/7r1/9/2nRA3c/4K4 w - - 0 1", 2 * ['e2f1', 'h4h2', 'f1e2', 'h2h4']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/9/9/9/7r1/9/2nRA3c/4K4 w - - 0 1", 2 * ['e2f1', 'h4h2', 'f1e2', 'h2h4'], True, sf.VALUE_MATE) # Creating pins to undermine root - result = sf.is_optional_game_end("xiangqi", "4k4/4c4/9/4p4/9/9/3rn4/3NR4/4K4/9 b - - 0 1", 2 * ['e4g5', 'e2f2', 'g5e4', 'f2e2']) - self.assertTrue(result[0]) - self.assertEqual(result[1], -sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "4k4/4c4/9/4p4/9/9/3rn4/3NR4/4K4/9 b - - 0 1", 2 * ['e4g5', 'e2f2', 'g5e4', 'f2e2'], True, -sf.VALUE_MATE) # Discovered check capture threat by rook - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/9/9/1N2P1C2/9/4BC3/9/cr1RK4 w - - 0 1", 2 * ['b5c3', 'b1c1', 'c3b5', 'c1b1']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/9/9/1N2P1C2/9/4BC3/9/cr1RK4 w - - 0 1", 2 * ['b5c3', 'b1c1', 'c3b5', 'c1b1'], True, sf.VALUE_MATE) # Creating a pin to undermine root + discovered check threat by horse - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/9/9/4c4/3n5/3NBA3/4A4/4K4 w - - 0 1", 2 * ['e1d1', 'e5d5', 'd1e1', 'd5e5']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/9/9/4c4/3n5/3NBA3/4A4/4K4 w - - 0 1", 2 * ['e1d1', 'e5d5', 'd1e1', 'd5e5'], True, sf.VALUE_MATE) # Creating a pin to undermine root + discovered check threat by rook - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/9/9/4c4/3r5/3NB4/4A4/4K4 w - - 0 1", 2 * ['e1d1', 'e5d5', 'd1e1', 'd5e5']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/9/9/4c4/3r5/3NB4/4A4/4K4 w - - 0 1", 2 * ['e1d1', 'e5d5', 'd1e1', 'd5e5'], True, sf.VALUE_MATE) # X-Ray protected discovered check - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/9/9/9/9/9/9/3NK1cr1 w - - 0 1", 2 * ['d1c3', 'h1h3', 'c3d1', 'h3h1']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/9/9/9/9/9/9/3NK1cr1 w - - 0 1", 2 * ['d1c3', 'h1h3', 'c3d1', 'h3h1'], True, sf.VALUE_MATE) # No overprotection by king - result = sf.is_optional_game_end("xiangqi", "3k5/9/9/3n5/9/9/3r5/9/9/3NK4 w - - 0 1", 2 * ['d1c3', 'd4c4', 'c3d1', 'c4d4']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "3k5/9/9/3n5/9/9/3r5/9/9/3NK4 w - - 0 1", 2 * ['d1c3', 'd4c4', 'c3d1', 'c4d4'], True, sf.VALUE_DRAW) # Overprotection by king - result = sf.is_optional_game_end("xiangqi", "3k5/9/9/9/9/9/3r5/9/9/3NK4 w - - 0 1", 2 * ['d1c3', 'd4c4', 'c3d1', 'c4d4']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "3k5/9/9/9/9/9/3r5/9/9/3NK4 w - - 0 1", 2 * ['d1c3', 'd4c4', 'c3d1', 'c4d4'], True, sf.VALUE_MATE) # Mutual pins by flying generals - result = sf.is_optional_game_end("xiangqi", "4k4/9/9/9/4n4/9/5C3/9/4N4/4K4 w - - 0 1", 2 * ['e2g1', 'e10f10', 'g1e2', 'f10e10']) - self.assertTrue(result[0]) - #self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "4k4/9/9/9/4n4/9/5C3/9/4N4/4K4 w - - 0 1", 2 * ['e2g1', 'e10f10', 'g1e2', 'f10e10'], True) #, sf.VALUE_MATE) # Fake protection by cannon - result = sf.is_optional_game_end("xiangqi", "5k3/9/9/9/9/1C7/1r7/9/1C7/4K4 w - - 0 1", 2 * ['b5c5', 'b4c4', 'c5b5', 'c4b4']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_MATE) + self._check_optional_game_end("xiangqi", "5k3/9/9/9/9/1C7/1r7/9/1C7/4K4 w - - 0 1", 2 * ['b5c5', 'b4c4', 'c5b5', 'c4b4'], True, sf.VALUE_MATE) # Fake protection by cannon + mutual chase - result = sf.is_optional_game_end("xiangqi", "4ka3/c2R1R2c/4b4/9/9/9/9/9/9/4K4 w - - 0 1", 2 * ['f9f7', 'f10e9', 'f7f9', 'e9f10']) - self.assertTrue(result[0]) - self.assertEqual(result[1], sf.VALUE_DRAW) + self._check_optional_game_end("xiangqi", "4ka3/c2R1R2c/4b4/9/9/9/9/9/9/4K4 w - - 0 1", 2 * ['f9f7', 'f10e9', 'f7f9', 'e9f10'], True, sf.VALUE_DRAW) def test_has_insufficient_material(self): for variant, positions in variant_positions.items(): for fen, expected_result in positions.items(): - result = sf.has_insufficient_material(variant, fen, []) - self.assertEqual(result, expected_result, "{}: {}".format(variant, fen)) + with self.subTest(variant=variant, fen=fen): + result = sf.has_insufficient_material(variant, fen, []) + self.assertEqual(result, expected_result) def test_validate_fen(self): # valid for variant, positions in variant_positions.items(): for fen in positions: - self.assertEqual(sf.validate_fen(fen, variant), sf.FEN_OK, "{}: {}".format(variant, fen)) + with self.subTest(variant=variant, fen=fen): + self.assertEqual(sf.validate_fen(fen, variant), sf.FEN_OK) # invalid for variant, positions in invalid_variant_positions.items(): for fen in positions: - self.assertNotEqual(sf.validate_fen(fen, variant), sf.FEN_OK, "{}: {}".format(variant, fen)) + with self.subTest(variant=variant, fen=fen): + self.assertNotEqual(sf.validate_fen(fen, variant), sf.FEN_OK) # chess960 - self.assertEqual(sf.validate_fen(CHESS960, "chess", True), sf.FEN_OK, "{}: {}".format(variant, fen)) + self.assertEqual(sf.validate_fen(CHESS960, "chess", True), sf.FEN_OK) self.assertEqual(sf.validate_fen("nrbqbkrn/pppppppp/8/8/8/8/PPPPPPPP/NRBQBKRN w BGbg - 0 1", "newzealand", True), sf.FEN_OK, "{}: {}".format(variant, fen)) # all variants starting positions for variant in sf.variants(): - fen = sf.start_fen(variant) - self.assertEqual(sf.validate_fen(fen, variant), sf.FEN_OK, "{}: {}".format(variant, fen)) + with self.subTest(variant=variant): + fen = sf.start_fen(variant) + self.assertEqual(sf.validate_fen(fen, variant), sf.FEN_OK) if __name__ == '__main__': unittest.main(verbosity=2) From cf7570938c03447a802df106775cbe2a5110c96f Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Sat, 23 Dec 2023 17:03:57 +0100 Subject: [PATCH 10/29] Use NNUE fallback URL for appveyor (#752) --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index c073aa73..d6f5226e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -70,7 +70,7 @@ build_script: $dummy = $nnuenet -match "(?nn-[a-z0-9]{12}.nnue)" $nnuenet = $Matches.nnuenet Write-Host "Default net:" $nnuenet - $nnuedownloadurl = "https://tests.stockfishchess.org/api/nn/$nnuenet" + $nnuedownloadurl = "https://github.com/official-stockfish/networks/raw/master/$nnuenet" $nnuefilepath = "src\${env:CONFIGURATION}\$nnuenet" if (Test-Path -Path $nnuefilepath) { Write-Host "Already available." From 6c8fb4630cc3a3771b6f7e7fa723464c1795a037 Mon Sep 17 00:00:00 2001 From: RainRat Date: Wed, 24 Jan 2024 15:14:46 -0800 Subject: [PATCH 11/29] fix typos --- src/apiutil.h | 2 +- src/parser.cpp | 2 +- src/parser.h | 2 +- tests/js/test.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/apiutil.h b/src/apiutil.h index 0794abc3..4b364f63 100644 --- a/src/apiutil.h +++ b/src/apiutil.h @@ -237,7 +237,7 @@ inline Disambiguation disambiguation_level(const Position& pos, Move m, Notation return SQUARE_DISAMBIGUATION; } - // A disambiguation occurs if we have more then one piece of type 'pt' + // A disambiguation occurs if we have more than one piece of type 'pt' // that can reach 'to' with a legal move. Bitboard b = pos.pieces(us, pt) ^ from; Bitboard others = 0; diff --git a/src/parser.cpp b/src/parser.cpp index 48aa6870..9d2b55b9 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -544,7 +544,7 @@ Variant* VariantParser::parse(Variant* v) { // Report invalid options if (DoCheck) { - const std::set& parsedKeys = config.get_comsumed_keys(); + const std::set& parsedKeys = config.get_consumed_keys(); for (const auto& it : config) if (parsedKeys.find(it.first) == parsedKeys.end()) std::cerr << "Invalid option: " << it.first << std::endl; diff --git a/src/parser.h b/src/parser.h index 36b208e0..04610b2f 100644 --- a/src/parser.h +++ b/src/parser.h @@ -34,7 +34,7 @@ class Config : public std::map { consumedKeys.insert(s); return std::map::find(s); } - const std::set& get_comsumed_keys() { + const std::set& get_consumed_keys() { return consumedKeys; } private: diff --git a/tests/js/test.js b/tests/js/test.js index 88f4c59e..3c82032c 100644 --- a/tests/js/test.js +++ b/tests/js/test.js @@ -768,7 +768,7 @@ describe('ffish.setOption(name, value)', function () { }); describe('ffish.setOptionInt(name, value)', function () { - it("it sets a int uci option value pair", () => { + it("it sets an int uci option value pair", () => { ffish.setOptionInt("Threads", 4); chai.expect(true).to.equal(true); }); From 052dce6a150c5dfc4a4775fc23319028904a86ab Mon Sep 17 00:00:00 2001 From: Sebastian Buchwald Date: Sat, 11 Mar 2023 22:08:35 +0100 Subject: [PATCH 12/29] Fix Makefile for clang 16 The clang 16 release will remove the -fexperimental-new-pass-manager flag (see https://github.com/llvm/llvm-project/commit/69b2b7282e92a1b576b7bd26f3b16716a5027e8e). Thus, the commit adapts the Makefile to use this flag only for older clang versions. closes https://github.com/official-stockfish/Stockfish/pull/4437 No functional change --- src/Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 8572369b..4fe40955 100644 --- a/src/Makefile +++ b/src/Makefile @@ -539,7 +539,10 @@ ifeq ($(optimize),yes) endif ifeq ($(comp),clang) - CXXFLAGS += -fexperimental-new-pass-manager + clangmajorversion = $(shell $(CXX) -dumpversion 2>/dev/null | cut -f1 -d.) + ifeq ($(shell expr $(clangmajorversion) \< 16),1) + CXXFLAGS += -fexperimental-new-pass-manager + endif endif endif From 7d42030f631551af033ada63c6eaa2d9ab922ddb Mon Sep 17 00:00:00 2001 From: RainRat Date: Tue, 13 Feb 2024 04:49:27 -0800 Subject: [PATCH 13/29] add Three Musketeers. needs collinearN and connectPieceTypes. (#755) --- src/parser.cpp | 2 ++ src/position.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++------ src/position.h | 12 ++++++++++++ src/variant.cpp | 11 +++++++++++ src/variant.h | 2 ++ src/variants.ini | 18 ++++++++++++++++- 6 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 9d2b55b9..2a4db2d2 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -527,6 +527,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("flagPieceSafe", v->flagPieceSafe); parse_attribute("checkCounting", v->checkCounting); parse_attribute("connectN", v->connectN); + parse_attribute("connectPieceTypes", v->connectPieceTypes, v->pieceToChar); parse_attribute("connectHorizontal", v->connectHorizontal); parse_attribute("connectVertical", v->connectVertical); parse_attribute("connectDiagonal", v->connectDiagonal); @@ -535,6 +536,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("connectRegion1Black", v->connectRegion1[BLACK]); parse_attribute("connectRegion2Black", v->connectRegion2[BLACK]); parse_attribute("connectNxN", v->connectNxN); + parse_attribute("collinearN", v->collinearN); parse_attribute("connectValue", v->connectValue); parse_attribute("materialCounting", v->materialCounting); parse_attribute("adjudicateFullBoard", v->adjudicateFullBoard); diff --git a/src/position.cpp b/src/position.cpp index bf6e60f2..c449b6a9 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -2703,7 +2703,7 @@ bool Position::is_optional_game_end(Value& result, int ply, int countStarted) co /// Position::is_immediate_game_end() tests whether the position ends the game /// immediately by a variant rule, i.e., there are no more legal moves. -/// It does not not detect stalemates. +/// It does not detect stalemates. bool Position::is_immediate_game_end(Value& result, int ply) const { @@ -2789,6 +2789,15 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { result = mated_in(ply); return true; } + + //Calculate eligible pieces for connection once. + Bitboard connectPieces = 0; + for (PieceSet ps = connect_piece_types(); ps;){ + PieceType pt = pop_lsb(ps); + connectPieces |= pieces(pt); + }; + connectPieces &= pieces(~sideToMove); + // Connect-n if (connect_n() > 0) { @@ -2796,7 +2805,7 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { for (Direction d : var->connect_directions) { - b = pieces(~sideToMove); + b = connectPieces; for (int i = 1; i < connect_n() && b; i++) b &= shift(d, b); if (b) @@ -2807,15 +2816,15 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { } } - if ((var->connectRegion1[~sideToMove] & pieces(~sideToMove)) && (var->connectRegion2[~sideToMove] & pieces(~sideToMove))) + if ((var->connectRegion1[~sideToMove] & connectPieces) && (var->connectRegion2[~sideToMove] & connectPieces)) { Bitboard target = var->connectRegion2[~sideToMove]; - Bitboard current = var->connectRegion1[~sideToMove] & pieces(~sideToMove); + Bitboard current = var->connectRegion1[~sideToMove] & connectPieces; 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 + newBitboard |= shift(d, current | newBitboard) & connectPieces; // the "| newBitboard" here probably saves a few loops } if (newBitboard & target) { @@ -2835,7 +2844,7 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { if (connect_nxn()) { - Bitboard connectors = pieces(~sideToMove); + Bitboard connectors = connectPieces; for (int i = 1; i < connect_nxn() && connectors; i++) connectors &= shift(connectors) & shift(connectors) & shift(connectors); if (connectors) @@ -2845,6 +2854,36 @@ bool Position::is_immediate_game_end(Value& result, int ply) const { } } + // Collinear-n + if (collinear_n() > 0) { + Bitboard allPieces = connectPieces; + for (Direction d : var->connect_directions) { + Bitboard b = allPieces; + while (b) { + Square s = pop_lsb(b); + + int total_count = 1; // Start with the current piece + + // Check in both directions + for (int sign : {-1, 1}) { + Bitboard shifted = shift(sign * d, square_bb(s)); + while (shifted) { + if (shifted & b) { + total_count++; + b &= ~shifted; // Remove this piece from further consideration + } + shifted = shift(sign * d, shifted); + } + } + + if (total_count >= collinear_n()) { + result = convert_mate_value(-var->connectValue, ply); + return true; + } + } + } + } + // Check for bikjang rule (Janggi), double passing, or board running full if ( (st->pliesFromNull > 0 && ((st->bikjang && st->previous->bikjang) || (st->pass && st->previous->pass))) || (var->adjudicateFullBoard && !(~pieces() & board_bb()))) diff --git a/src/position.h b/src/position.h index 4ee93665..adaeeb6a 100644 --- a/src/position.h +++ b/src/position.h @@ -205,11 +205,13 @@ class Position { bool flag_reached(Color c) const; bool check_counting() const; int connect_n() const; + PieceSet connect_piece_types() const; bool connect_horizontal() const; bool connect_vertical() const; bool connect_diagonal() const; const std::vector& getConnectDirections() const; int connect_nxn() const; + int collinear_n() const; CheckCount checks_remaining(Color c) const; MaterialCounting material_counting() const; @@ -1035,6 +1037,11 @@ inline int Position::connect_n() const { return var->connectN; } +inline PieceSet Position::connect_piece_types() const { + assert(var != nullptr); + return var->connectPieceTypes; +} + inline bool Position::connect_horizontal() const { assert(var != nullptr); return var->connectHorizontal; @@ -1058,6 +1065,11 @@ inline int Position::connect_nxn() const { return var->connectNxN; } +inline int Position::collinear_n() const { + assert(var != nullptr); + return var->collinearN; +} + inline CheckCount Position::checks_remaining(Color c) const { return st->checksRemaining[c]; } diff --git a/src/variant.cpp b/src/variant.cpp index 386c21d5..5b21b53d 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -2064,6 +2064,17 @@ Variant* Variant::conclude() { connect_directions.push_back(SOUTH_EAST); } + // If not a connect variant, set connectPieceTypes to no pieces. + if ( !(connectRegion1[WHITE] || connectRegion1[BLACK] || connectN || connectNxN || collinearN) ) + { + connectPieceTypes = NO_PIECE_SET; + } + //Otherwise optimize to pieces actually in the game. + else + { + connectPieceTypes = connectPieceTypes & pieceTypes; + }; + return this; } diff --git a/src/variant.h b/src/variant.h index ec199185..a34b37de 100644 --- a/src/variant.h +++ b/src/variant.h @@ -149,12 +149,14 @@ struct Variant { bool flagPieceSafe = false; bool checkCounting = false; int connectN = 0; + PieceSet connectPieceTypes = ~NO_PIECE_SET; bool connectHorizontal = true; bool connectVertical = true; bool connectDiagonal = true; Bitboard connectRegion1[COLOR_NB] = {}; Bitboard connectRegion2[COLOR_NB] = {}; int connectNxN = 0; + int collinearN = 0; Value connectValue = VALUE_MATE; MaterialCounting materialCounting = NO_MATERIAL_COUNTING; bool adjudicateFullBoard = false; diff --git a/src/variants.ini b/src/variants.ini index abc2c709..aaf40810 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -280,6 +280,7 @@ # flagPieceSafe: the flag piece must be safe to win [bool] (default: false) # checkCounting: enable check count win rule (check count is communicated via FEN, see 3check) [bool] (default: false) # connectN: number of aligned pieces for win [int] (default: 0) +# connectPieceTypes: pieces evaluated for connection rule [PieceSet] (default: *) # 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) @@ -288,6 +289,7 @@ # connectRegion1Black: " # connectRegion2Black: " # connectNxN: connect a tight NxN square for win [int] (default: 0) +# collinearN: arrange N pieces collinearly (other squares can be between pieces) [int] (default: 0) # connectValue: result in case of connect [Value] (default: win) # materialCounting: enable material counting rules [MaterialCounting] (default: none) # adjudicateFullBoard: apply material counting immediately when board is full [bool] (default: false) @@ -1546,7 +1548,7 @@ nMoveRule = 0 #https://ludii.games/details.php?keyword=Djara-Badakh #https://ludii.games/details.php?keyword=Tuk%20Tak customPiece1 = p:mKmNmAmD -#moves anywhere on the board, KNAD is an list of all possible moves on a 3x3 +#moves anywhere on the board, KNAD is a list of all possible moves on a 3x3 startFen = 3/3/3[PPPppp] w - - 0 1 mustDrop = true nMoveRule = 0 @@ -1912,3 +1914,17 @@ enclosingDrop = anyside #http://gamescrafters.berkeley.edu/games.php?game=connect4 [cfour-misere:cfour] connectValue = loss + +#https://www.ludii.games/details.php?keyword=Three%20Musketeers +[three-musketeers] +pieceToCharTable = P......M..............p......m.............. +startFen = ppppM/ppppp/ppMpp/ppppp/Mpppp +maxRank = 5 +maxFile = 5 +collinearN = 3 +connectDiagonal = false +customPiece1 = m:cW +customPiece2 = p:mW +connectValue = loss +stalemateValue = win +connectPieceTypes = m From 0bfa4fcdfe9889bdf1b136f4739a3e21e25fb7d8 Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Fri, 16 Feb 2024 19:42:26 +0100 Subject: [PATCH 14/29] Fix cannonshogi (#757) --- src/bitboard.cpp | 10 ++++++---- src/variants.ini | 4 ++-- test.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 3939296c..4f005759 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -111,7 +111,7 @@ namespace { const std::map GrasshopperDirectionsH { {EAST, 1}, {WEST, 1} }; const std::map GrasshopperDirectionsD { {NORTH_EAST, 1}, {SOUTH_EAST, 1}, {SOUTH_WEST, 1}, {NORTH_WEST, 1} }; - enum MovementType { RIDER, HOPPER, LAME_LEAPER, UNLIMITED_RIDER }; + enum MovementType { RIDER, HOPPER, LAME_LEAPER, HOPPER_RANGE }; template #ifdef PRECOMPUTED_MAGICS @@ -137,7 +137,9 @@ namespace { if (MT != HOPPER || hurdle) { attack |= s; - if (limit && MT != UNLIMITED_RIDER && ++count >= limit) + // For hoppers we consider limit == 1 as a grasshopper, + // but limit > 1 as a limited distance hopper + if (limit && !(MT == HOPPER_RANGE && limit == 1) && ++count >= limit) break; } @@ -300,7 +302,7 @@ void Bitboards::init_pieces() { leaper |= safe_destination(s, c == WHITE ? d : -d); } pseudo |= sliding_attack(pi->slider[initial][modality], s, 0, c); - pseudo |= sliding_attack(pi->hopper[initial][modality], s, 0, c); + pseudo |= sliding_attack(pi->hopper[initial][modality], s, 0, c); } } } @@ -420,7 +422,7 @@ namespace { // apply to the 64 or 32 bits word to get the index. Magic& m = magics[s]; // The mask for hoppers is unlimited distance, even if the hopper is limited distance (e.g., grasshopper) - m.mask = (MT == LAME_LEAPER ? lame_leaper_path(directions, s) : sliding_attack(directions, s, 0)) & ~edges; + m.mask = (MT == LAME_LEAPER ? lame_leaper_path(directions, s) : sliding_attack(directions, s, 0)) & ~edges; #ifdef LARGEBOARDS m.shift = 128 - popcount(m.mask); #else diff --git a/src/variants.ini b/src/variants.ini index aaf40810..09be0e40 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -1662,8 +1662,8 @@ cannon = u customPiece1 = a:pR customPiece2 = c:mBcpB customPiece3 = i:pB -customPiece4 = w:mRpRFAcpR -customPiece5 = f:mBpBWDcpB +customPiece4 = w:mRpRmFpB2 +customPiece5 = f:mBpBmWpR2 promotedPieceType = u:w a:w c:f i:f startFen = lnsgkgsnl/1rci1uab1/p1p1p1p1p/9/9/9/P1P1P1P1P/1BAU1ICR1/LNSGKGSNL[-] w 0 1 diff --git a/test.py b/test.py index 9cf4c539..7a5aa456 100644 --- a/test.py +++ b/test.py @@ -100,6 +100,19 @@ customPiece3 = c:hlN customPiece4 = d:hrN startFen = 7/7/7/3A3/7/7/7 w - - 0 1 + +[cannonshogi:shogi] +dropNoDoubled = - +shogiPawnDropMateIllegal = false +soldier = p +cannon = u +customPiece1 = a:pR +customPiece2 = c:mBcpB +customPiece3 = i:pB +customPiece4 = w:mRpRmFpB2 +customPiece5 = f:mBpBmWpR2 +promotedPieceType = u:w a:w c:f i:f +startFen = lnsgkgsnl/1rci1uab1/p1p1p1p1p/9/9/9/P1P1P1P1P/1BAU1ICR1/LNSGKGSNL[-] w 0 1 """ sf.load_variant_config(ini_text) @@ -317,6 +330,24 @@ def test_legal_moves(self): result = sf.legal_moves("shogun", SHOGUN, ["c2c4", "b8c6", "b2b4", "b7b5", "c4b5", "c6b8"]) self.assertIn("b5b6+", result) + # In Cannon Shogi the FGC and FSC can also move one square diagonally and, besides, + # move or capture two squares diagonally, by leaping an adjacent piece. + fen = "lnsg1gsnl/1rc1kuab1/p1+A1p1p1p/3P5/6i2/6P2/P1P1P3P/1B1U1ICR1/LNSGKGSNL[] w - - 1 3" + result = sf.legal_moves("cannonshogi", fen, []) + # mF + self.assertIn("c7b6", result) + self.assertIn("c7d8", result) + self.assertNotIn("c7d6", result) + self.assertNotIn("c7b8", result) + # pB2 + self.assertIn("c7a9", result) + self.assertIn("c7e5", result) + self.assertNotIn("c7a5", result) + self.assertNotIn("c7e9", result) + # verify distance limited to 2 + self.assertNotIn("c7f4", result) + self.assertNotIn("c7g3", result) + # Cambodian queen cannot capture with its leap # Cambodian king cannot leap to escape check result = sf.legal_moves("cambodian", CAMBODIAN, ["b1d2", "g8e7", "d2e4", "d6d5", "e4d6"]) From 203f8f8c1b6ed935e62f5e7d54fc655a951774b1 Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Fri, 16 Feb 2024 20:10:00 +0100 Subject: [PATCH 15/29] Bump pyffish version --- setup.py | 2 +- src/pyffish.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index ab253c9a..357001db 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ sources=sources, extra_compile_args=args) -setup(name="pyffish", version="0.0.80", +setup(name="pyffish", version="0.0.81", description="Fairy-Stockfish Python wrapper", long_description=long_description, long_description_content_type="text/markdown", diff --git a/src/pyffish.cpp b/src/pyffish.cpp index d321a57c..8439acd0 100644 --- a/src/pyffish.cpp +++ b/src/pyffish.cpp @@ -54,7 +54,7 @@ void buildPosition(Position& pos, StateListPtr& states, const char *variant, con } extern "C" PyObject* pyffish_version(PyObject* self) { - return Py_BuildValue("(iii)", 0, 0, 80); + return Py_BuildValue("(iii)", 0, 0, 81); } extern "C" PyObject* pyffish_info(PyObject* self) { From 2c22a948ed309c4c15f3534818a8d2935812f898 Mon Sep 17 00:00:00 2001 From: QueensGambit Date: Sat, 17 Feb 2024 10:51:15 +0100 Subject: [PATCH 16/29] Bump version --- tests/js/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/js/package.json b/tests/js/package.json index cc960f14..aa744fd5 100644 --- a/tests/js/package.json +++ b/tests/js/package.json @@ -1,6 +1,6 @@ { "name": "ffish", - "version": "0.7.4", + "version": "0.7.5", "description": "A high performance WebAssembly chess variant library based on Fairy-Stockfish", "main": "ffish.js", "types": "ffish.d.ts", From 69cdd8257634612c52e4c032c50f3d5026b4710f Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Fri, 23 Feb 2024 15:25:11 +0100 Subject: [PATCH 17/29] Update reference bench bench: 6180480 From def2560cb130cd2b8eab4bbaf56fa91b14b4dd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bajusz=20Tam=C3=A1s?= Date: Mon, 26 Feb 2024 15:53:51 +0100 Subject: [PATCH 18/29] Add missing soldier promotion to cannonshogi --- src/variants.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variants.ini b/src/variants.ini index 09be0e40..0b27fc2d 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -1664,7 +1664,7 @@ customPiece2 = c:mBcpB customPiece3 = i:pB customPiece4 = w:mRpRmFpB2 customPiece5 = f:mBpBmWpR2 -promotedPieceType = u:w a:w c:f i:f +promotedPieceType = u:w a:w c:f i:f p:g startFen = lnsgkgsnl/1rci1uab1/p1p1p1p1p/9/9/9/P1P1P1P1P/1BAU1ICR1/LNSGKGSNL[-] w 0 1 #https://www.chessvariants.com/difftaking.dir/deadsquare.html From fb8cf35d9dbd9278f00c772100abf5888e444c1a Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Fri, 1 Mar 2024 11:55:17 +0100 Subject: [PATCH 19/29] Validate max one king per side --- src/apiutil.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/apiutil.h b/src/apiutil.h index 4b364f63..d39102fc 100644 --- a/src/apiutil.h +++ b/src/apiutil.h @@ -821,6 +821,16 @@ inline Validation check_number_of_kings(const std::string& fenBoard, const std:: int nbWhiteKingsStart = piece_count(startFenBoard, WHITE, KING, v); int nbBlackKingsStart = piece_count(startFenBoard, BLACK, KING, v); + if (nbWhiteKings > 1) + { + std::cerr << "Invalid number of white kings. Maximum: 1. Given: " << nbWhiteKings << std::endl; + return NOK; + } + if (nbBlackKings > 1) + { + std::cerr << "Invalid number of black kings. Maximum: 1. Given: " << nbBlackKings << std::endl; + return NOK; + } if (nbWhiteKings != nbWhiteKingsStart) { std::cerr << "Invalid number of white kings. Expected: " << nbWhiteKingsStart << ". Given: " << nbWhiteKings << std::endl; From bbe0d954a18a258cff38c155e8b62925cd80f0de Mon Sep 17 00:00:00 2001 From: RainRat Date: Fri, 1 Mar 2024 05:01:13 -0800 Subject: [PATCH 20/29] add 'wall or move' rule (for Atlantis) (#728) --- src/movegen.cpp | 9 ++++++++- src/parser.cpp | 1 + src/position.cpp | 6 ++++-- src/variant.h | 1 + src/variants.ini | 5 ++--- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/movegen.cpp b/src/movegen.cpp index 43807bb0..f5a16eb4 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -29,7 +29,8 @@ namespace { ExtMove* make_move_and_gating(const Position& pos, ExtMove* moveList, Color us, Square from, Square to, PieceType pt = NO_PIECE_TYPE) { // Wall placing moves - if (pos.walling()) + //if it's "wall or move", and they chose non-null move, skip even generating wall move + if (pos.walling() && !(pos.variant()->wallOrMove && (from!=to))) { Bitboard b = pos.board_bb() & ~((pos.pieces() ^ from) | to); if (T == CASTLING) @@ -443,6 +444,12 @@ namespace { // Workaround for passing: Execute a non-move with any piece if (pos.pass(Us) && !pos.count(Us) && pos.pieces(Us)) *moveList++ = make(lsb(pos.pieces(Us)), lsb(pos.pieces(Us))); + + //if "wall or move", generate walling action with null move + if (pos.variant()->wallOrMove) + { + moveList = make_move_and_gating(pos, moveList, Us, lsb(pos.pieces(Us)), lsb(pos.pieces(Us))); + } } // King moves diff --git a/src/parser.cpp b/src/parser.cpp index 2a4db2d2..3ebf1b16 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -472,6 +472,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("wallingRegionBlack", v->wallingRegion[BLACK]); parse_attribute("wallingRegion", v->wallingRegion[WHITE]); parse_attribute("wallingRegion", v->wallingRegion[BLACK]); + parse_attribute("wallOrMove", v->wallOrMove); parse_attribute("seirawanGating", v->seirawanGating); parse_attribute("cambodianMoves", v->cambodianMoves); parse_attribute("diagonalLines", v->diagonalLines); diff --git a/src/position.cpp b/src/position.cpp index c449b6a9..023cdfa7 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1303,7 +1303,8 @@ bool Position::pseudo_legal(const Move m) const { return checkers() ? MoveList< EVASIONS>(*this).contains(m) : MoveList(*this).contains(m); - if (walling()) + //if walling, and walling is not optional, or they didn't move, do the checks. + if (walling() && (!var->wallOrMove || (from==to))) { Bitboard wallsquares = st->wallSquares; @@ -2045,7 +2046,8 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { } // Add gated wall square - if (walling()) + // if wallOrMove, only actually place the wall if they gave up their move + if (walling() && (!var->wallOrMove || (from==to))) { // Reset wall squares for duck walling if (walling_rule() == DUCK) diff --git a/src/variant.h b/src/variant.h index a34b37de..aa273835 100644 --- a/src/variant.h +++ b/src/variant.h @@ -108,6 +108,7 @@ struct Variant { bool gating = false; WallingRule wallingRule = NO_WALLING; Bitboard wallingRegion[COLOR_NB] = {AllSquares, AllSquares}; + bool wallOrMove = false; bool seirawanGating = false; bool cambodianMoves = false; Bitboard diagonalLines = 0; diff --git a/src/variants.ini b/src/variants.ini index 0b27fc2d..fd5b5049 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -232,6 +232,7 @@ # wallingRule: rule on where wall can be placed [WallingRule] (default: none) # wallingRegionWhite: mask where wall squares (including duck) can be placed by white [Bitboard] (default: all squares) # wallingRegionBlack: mask where wall squares (including duck) can be placed by black [Bitboard] (default: all squares) +# wallOrMove: can wall or move, but not both [bool] (default: false) # seirawanGating: allow gating of pieces in hand like in S-Chess, requires "gating = true" [bool] (default: false) # cambodianMoves: enable special moves of cambodian chess, requires "gating = true" [bool] (default: false) # diagonalLines: enable special moves along diagonal for specific squares (Janggi) [Bitboard] @@ -1811,9 +1812,7 @@ connectDiagonal = false #https://www.chessvariants.com/boardrules.dir/atlantis.html [atlantis:chess] wallingRule = edge -#not ready yet. Other wall variants are "move and wall", this is "move or wall". -#need to figure out way to do this ie. write code for: -#wallOrMove = true +wallOrMove = true #https://www.chessvariants.com/rules/ajax-orthodox-chess [ajax-orthodox:chess] From 62577a4039e48c55a579edac78dbdbec5d3bf71e Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Fri, 1 Mar 2024 15:28:21 +0100 Subject: [PATCH 21/29] Handle invalid promoted piece (#416) --- src/position.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/position.cpp b/src/position.cpp index 023cdfa7..ade4798d 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -314,7 +314,7 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960, } // Promoted shogi pieces - else if (token == '+' && (idx = piece_to_char().find(ss.peek())) != string::npos) + else if (token == '+' && (idx = piece_to_char().find(ss.peek())) != string::npos && promoted_piece_type(type_of(Piece(idx)))) { ss >> token; put_piece(make_piece(color_of(Piece(idx)), promoted_piece_type(type_of(Piece(idx)))), sq, true, Piece(idx)); From 1ab906f152c89426563453da8a4c414048e55596 Mon Sep 17 00:00:00 2001 From: gbtami Date: Mon, 25 Mar 2024 21:31:37 +0100 Subject: [PATCH 22/29] Add missing dragon promotion --- src/variant.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/variant.cpp b/src/variant.cpp index 5b21b53d..bf61a049 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -744,6 +744,8 @@ namespace { v->capturesToHand = false; v->whiteDropRegion = Rank1BB; v->blackDropRegion = Rank8BB; + v->promotionPieceTypes[WHITE] = piece_set(ARCHBISHOP) | QUEEN | ROOK | BISHOP | KNIGHT; + v->promotionPieceTypes[BLACK] = piece_set(ARCHBISHOP) | QUEEN | ROOK | BISHOP | KNIGHT; return v; } // Paradigm chess30 From d8b5bdf68ec7b47dd0b1b6cd6bf16dd958901c0c Mon Sep 17 00:00:00 2001 From: gbtami Date: Tue, 26 Mar 2024 00:26:25 +0100 Subject: [PATCH 23/29] Bump pyffish version --- setup.py | 2 +- src/pyffish.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 357001db..fa72818d 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ sources=sources, extra_compile_args=args) -setup(name="pyffish", version="0.0.81", +setup(name="pyffish", version="0.0.82", description="Fairy-Stockfish Python wrapper", long_description=long_description, long_description_content_type="text/markdown", diff --git a/src/pyffish.cpp b/src/pyffish.cpp index 8439acd0..fb8094f2 100644 --- a/src/pyffish.cpp +++ b/src/pyffish.cpp @@ -54,7 +54,7 @@ void buildPosition(Position& pos, StateListPtr& states, const char *variant, con } extern "C" PyObject* pyffish_version(PyObject* self) { - return Py_BuildValue("(iii)", 0, 0, 81); + return Py_BuildValue("(iii)", 0, 0, 82); } extern "C" PyObject* pyffish_info(PyObject* self) { From f3c982bdb5a1882c45d9fab71b16f72de5191f19 Mon Sep 17 00:00:00 2001 From: gbtami Date: Thu, 28 Mar 2024 12:14:13 +0100 Subject: [PATCH 24/29] Bump ffish.js version --- tests/js/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/js/package.json b/tests/js/package.json index aa744fd5..5fbb6c48 100644 --- a/tests/js/package.json +++ b/tests/js/package.json @@ -1,6 +1,6 @@ { "name": "ffish", - "version": "0.7.5", + "version": "0.7.6", "description": "A high performance WebAssembly chess variant library based on Fairy-Stockfish", "main": "ffish.js", "types": "ffish.d.ts", From c0a94e4231633b6894e1ea313bdcf253b0d0019c Mon Sep 17 00:00:00 2001 From: RainRat Date: Wed, 17 Apr 2024 08:11:50 -0700 Subject: [PATCH 25/29] wallOrMove bug fix (#763) --- src/position.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index ade4798d..b9f34be4 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1558,7 +1558,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { Piece captured = piece_on(type_of(m) == EN_PASSANT ? capture_square(to) : to); if (to == from) { - assert((type_of(m) == PROMOTION && sittuyin_promotion()) || (is_pass(m) && pass(us))); + assert((type_of(m) == PROMOTION && sittuyin_promotion()) || (is_pass(m) && (pass(us) || var->wallOrMove ))); captured = NO_PIECE; } st->capturedpromoted = is_promoted(to); @@ -2128,7 +2128,7 @@ void Position::undo_move(Move m) { assert(type_of(m) == DROP || empty(from) || type_of(m) == CASTLING || is_gating(m) || (type_of(m) == PROMOTION && sittuyin_promotion()) - || (is_pass(m) && pass(us))); + || (is_pass(m) && (pass(us) || var->wallOrMove))); assert(type_of(st->capturedPiece) != KING); // Reset wall squares From 0e89b9aa4341fe68d6f1202e7c395d70cf3d9b0b Mon Sep 17 00:00:00 2001 From: yjf2002ghty <47345902+yjf2002ghty@users.noreply.github.com> Date: Tue, 23 Apr 2024 00:47:10 +0800 Subject: [PATCH 26/29] Khan's chess & Shinobi Chess+ (#778) --- src/variants.ini | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/variants.ini b/src/variants.ini index fd5b5049..688cce0d 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -1927,3 +1927,46 @@ customPiece2 = p:mW connectValue = loss stalemateValue = win connectPieceTypes = m + +#https://www.pychess.org/variants/shinobiplus +[shinobiplus:crazyhouse] +pieceToCharTable = - +commoner = c +bers = d +dragonHorse = f +archbishop = j +fers = m +shogiKnight = h +lance = l +promotionRegionWhite = *7 *8 +promotionRegionBlack = *1 *2 *3 +promotionPieceTypes = - +promotedPieceType = p:c m:b h:n l:r +mandatoryPiecePromotion = true +stalemateValue = loss +nFoldRule = 4 +perpetualCheckIllegal = true +startFen = rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/4K3[JDFCLHM] w kq - 0 1 +capturesToHand = false +whiteDropRegion = *1 *2 *3 *4 +immobilityIllegal = true +flagPiece = k +flagRegionWhite = *8 +flagRegionBlack = *1 + +#https://www.pychess.org/variants/khans +[khans:chess] +pieceToCharTable = - +centaur = h +knibis = a +kniroo = l +customPiece1 = t:mNcK +customPiece2 = s:mfhNcfW +promotionPawnTypesBlack = s +promotionPieceTypesBlack = t +stalemateValue = loss +nMoveRuleTypesBlack = s +flagPiece = k +flagRegionWhite = *8 +flagRegionBlack = *1 +startFen = lhatkahl/ssssssss/8/8/8/8/PPPPPPPP/RNBQKBNR w KQ - 0 1 From 50adcffd957aaa2b4729409518549fc3107b9c33 Mon Sep 17 00:00:00 2001 From: yjf2002ghty <47345902+yjf2002ghty@users.noreply.github.com> Date: Mon, 29 Apr 2024 20:02:17 +0800 Subject: [PATCH 27/29] Fix stack overflow when depth is too deep (#780) --- src/movegen.h | 34 ++++++++++++++++++++++++++++++++-- src/types.h | 13 +++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/movegen.h b/src/movegen.h index bbb35b39..3047bf6c 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -55,12 +55,37 @@ inline bool operator<(const ExtMove& f, const ExtMove& s) { template ExtMove* generate(const Position& pos, ExtMove* moveList); +constexpr size_t moveListSize = sizeof(ExtMove) * MAX_MOVES; + /// The MoveList struct is a simple wrapper around generate(). It sometimes comes /// in handy to use this class instead of the low level generate() function. template struct MoveList { - explicit MoveList(const Position& pos) : last(generate(pos, moveList)) {} + +#ifdef USE_HEAP_INSTEAD_OF_STACK_FOR_MOVE_LIST + explicit MoveList(const Position& pos) + { + this->moveList = (ExtMove*)malloc(moveListSize); + if (this->moveList == 0) + { + printf("Error: Failed to allocate memory in heap."); + exit(1); + } + this->last = generate(pos, this->moveList); + } + + ~MoveList() + { + free(this->moveList); + } +#else + explicit MoveList(const Position& pos) : last(generate(pos, moveList)) + { + ; + } +#endif + const ExtMove* begin() const { return moveList; } const ExtMove* end() const { return last; } size_t size() const { return last - moveList; } @@ -69,7 +94,12 @@ struct MoveList { } private: - ExtMove moveList[MAX_MOVES], *last; + ExtMove* last; +#ifdef USE_HEAP_INSTEAD_OF_STACK_FOR_MOVE_LIST + ExtMove* moveList = 0; +#else + ExtMove moveList[MAX_MOVES]; +#endif }; } // namespace Stockfish diff --git a/src/types.h b/src/types.h index 5abfac80..b049736b 100644 --- a/src/types.h +++ b/src/types.h @@ -227,13 +227,22 @@ typedef uint64_t Bitboard; constexpr int SQUARE_BITS = 6; #endif +//When defined, move list will be stored in heap. Delete this if you want to use stack to store move list. Using stack can cause overflow (Segmentation Fault) when the search is too deep. +#define USE_HEAP_INSTEAD_OF_STACK_FOR_MOVE_LIST + #ifdef ALLVARS constexpr int MAX_MOVES = 8192; -constexpr int MAX_PLY = 60; +#ifdef USE_HEAP_INSTEAD_OF_STACK_FOR_MOVE_LIST +constexpr int MAX_PLY = 246; +#else +constexpr int MAX_PLY = 60; +#endif +/// endif USE_HEAP_INSTEAD_OF_STACK_FOR_MOVE_LIST #else constexpr int MAX_MOVES = 1024; -constexpr int MAX_PLY = 246; +constexpr int MAX_PLY = 246; #endif +/// endif ALLVARS /// A move needs 16 bits to be stored /// From 81e3ceec0b16b76a6b2de7da92d15a2dcab362c3 Mon Sep 17 00:00:00 2001 From: yjf2002ghty <47345902+yjf2002ghty@users.noreply.github.com> Date: Sat, 1 Jun 2024 11:53:19 +0800 Subject: [PATCH 28/29] Fix Mini House --- src/variants.ini | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/variants.ini b/src/variants.ini index 688cce0d..383b7ba7 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -575,6 +575,9 @@ promotionPawnTypes = - maxRank = 6 maxFile = 6 startFen = 2bnrk/5p/6/6/P5/KRNB2 +promotionPieceTypes = nbr +promotionRegionWhite = *6 +doubleStep = false #https://www.chess.com/variants/rookmate [rookmate:chess] From a449a8be8a576bd1d5dd348ec849796a661707c0 Mon Sep 17 00:00:00 2001 From: chocolatebakery Date: Sat, 1 Jun 2024 08:57:01 +0000 Subject: [PATCH 29/29] Small contribution on a compatible anti/giveaway variant --- src/variants.ini | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/variants.ini b/src/variants.ini index 383b7ba7..7eaeafb3 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -1973,3 +1973,13 @@ flagPiece = k flagRegionWhite = *8 flagRegionBlack = *1 startFen = lhatkahl/ssssssss/8/8/8/8/PPPPPPPP/RNBQKBNR w KQ - 0 1 + +#https://www.chessvariants.com/diffobjective.dir/giveaway.html +#When stalemate, the stalemated player does not move but the opponent can if he wish to play for win go on moving and do as many moves he wants to do. +#If the stalemate then disappear, both players move again as usual. So, if white for example has a pawn on h2 and nothing more and black a pawn on h3, pawn on a7 and rooks on a8 and h8 +# black can win by moving : 1.-,Rh4 2.-, a5. 3.-, a4 4.- Ra5 5.-,a3 6.-,a2 7.-,a1=R 8.-,Rg1 9.-,Rg3 10.hxg3,h2 11.gxh4,Rg5 12.hxg5,h1=Q 13.g6,Qh7 14.gxh7 and black has won. +[andersgiveaway:giveaway] +passOnStalemate = true + +[andersanti:antichess] +passOnStalemate = true