Fix ambiguous castling notation
authorFabian Fichter <ianfab@users.noreply.github.com>
Fri, 23 Apr 2021 15:49:44 +0000 (17:49 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Fri, 23 Apr 2021 15:49:44 +0000 (17:49 +0200)
Use chess960 castling notation when castling move would be ambiguous.

Closes #295.

src/uci.cpp
src/variants.ini
test.py

index 524b081..0e9237d 100644 (file)
@@ -472,7 +472,12 @@ string UCI::move(const Position& pos, Move m) {
   if (is_gating(m) && gating_square(m) == to)
       from = to_sq(m), to = from_sq(m);
   else if (type_of(m) == CASTLING && !pos.is_chess960())
+  {
       to = make_square(to > from ? pos.castling_kingside_file() : pos.castling_queenside_file(), rank_of(from));
+      // If the castling move is ambiguous with a normal king move, switch to 960 notation
+      if (pos.pseudo_legal(make_move(from, to)))
+          to = to_sq(m);
+  }
 
   string move = (type_of(m) == DROP ? UCI::dropped_piece(pos, m) + (Options["Protocol"] == "usi" ? '*' : '@')
                                     : UCI::square(pos, from)) + UCI::square(pos, to);
index df10ccb..b388f2d 100644 (file)
@@ -589,7 +589,7 @@ doubleStep = false
 castling = false
 startFen = r6r/1nbqkbn1/pppppppp/8/8/PPPPPPPP/1NBQKBN1/R6R w - - 0 1
 
-# Diana Chess // Kingside castling not working!
+# Diana Chess
 # https://greenchess.net/rules.php?v=diana
 [diana:losalamos]
 pieceToCharTable = PNBRQ................Kpnbrq................k
diff --git a/test.py b/test.py
index dae8d9b..ce86be9 100644 (file)
--- a/test.py
+++ b/test.py
@@ -59,6 +59,15 @@ promotionPieceTypes = qh
 flagPiece = k
 whiteFlag = *8
 blackFlag = *1
+
+[diana:losalamos]
+pieceToCharTable = PNBRQ................Kpnbrq................k
+bishop = b
+promotionPieceTypes = rbn
+castling = true
+castlingKingsideFile = e
+castlingQueensideFile = b
+startFen = rbnkbr/pppppp/6/6/PPPPPP/RBNKBR w KQkq - 0 1
 """
 
 sf.load_variant_config(ini_text)
@@ -265,6 +274,11 @@ class TestPyffish(unittest.TestCase):
         result = sf.legal_moves("capablanca", CAPA, moves)
         self.assertIn("f1i1", result)
 
+        # Check that chess960 castling notation is used for otherwise ambiguous castling move
+        # d1e1 is a normal king move, so castling has to be d1f1
+        result = sf.legal_moves("diana", "rbnk1r/pppbpp/3p2/5P/PPPPPB/RBNK1R w KQkq - 2 3", [])
+        self.assertIn("d1f1", result)
+
     def test_get_fen(self):
         result = sf.get_fen("chess", CHESS, [])
         self.assertEqual(result, CHESS)