Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -62,30 +62,48 @@ def move_to_uci(move):
|
|
| 62 |
return cols[fc] + str(8 - fr) + cols[tc] + str(8 - tr)
|
| 63 |
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if len(
|
| 69 |
-
raise ValueError(f"
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
|
|
|
| 72 |
game = Breakthrough()
|
| 73 |
-
game.board =
|
| 74 |
if player == "1":
|
| 75 |
game.move_count = 0
|
| 76 |
elif player == "2":
|
| 77 |
game.move_count = 1
|
| 78 |
else:
|
| 79 |
-
#
|
| 80 |
-
game.move_count = 0
|
| 81 |
game._cached_matrix = None
|
| 82 |
searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
|
| 83 |
visits, _ = searcher.search_with_time_budget(game, 1.0)
|
| 84 |
best_move = max(visits, key=visits.get)
|
| 85 |
-
|
| 86 |
-
print(f"[DEBUG] get_move OK:
|
| 87 |
-
return
|
| 88 |
-
|
| 89 |
|
| 90 |
demo = gr.Interface(
|
| 91 |
fn=get_move,
|
|
|
|
| 62 |
return cols[fc] + str(8 - fr) + cols[tc] + str(8 - tr)
|
| 63 |
|
| 64 |
|
| 65 |
+
def fen_to_board(fen):
|
| 66 |
+
rows_str = fen.strip().split(" ")[0] # strip off ' w' or ' b'
|
| 67 |
+
parts = rows_str.split("/")
|
| 68 |
+
if len(parts) != 8:
|
| 69 |
+
raise ValueError(f"FEN must have 8 rows, got {len(parts)}")
|
| 70 |
+
board = np.zeros((8, 8), dtype=np.int32)
|
| 71 |
+
for r, row in enumerate(parts):
|
| 72 |
+
c = 0
|
| 73 |
+
for ch in row:
|
| 74 |
+
if ch == "B":
|
| 75 |
+
board[r, c] = 2
|
| 76 |
+
c += 1
|
| 77 |
+
elif ch == "W":
|
| 78 |
+
board[r, c] = 1
|
| 79 |
+
c += 1
|
| 80 |
+
elif ch.isdigit():
|
| 81 |
+
skip = int(ch)
|
| 82 |
+
c += skip
|
| 83 |
+
else:
|
| 84 |
+
raise ValueError(f"Invalid FEN char {ch} in row {r} ({row})")
|
| 85 |
+
if c != 8:
|
| 86 |
+
raise ValueError(f"Row {r} has {c} columns, should be 8")
|
| 87 |
+
return board
|
| 88 |
|
| 89 |
+
def get_move(board_text, player=None):
|
| 90 |
+
print(f"[DEBUG] get_move FEN board_text={repr(board_text)}, player={player}")
|
| 91 |
game = Breakthrough()
|
| 92 |
+
game.board = fen_to_board(board_text)
|
| 93 |
if player == "1":
|
| 94 |
game.move_count = 0
|
| 95 |
elif player == "2":
|
| 96 |
game.move_count = 1
|
| 97 |
else:
|
| 98 |
+
# infer from the last part of FEN if you want, or keep fixed
|
| 99 |
+
game.move_count = 0 if player is None or "w" in board_text else 1
|
| 100 |
game._cached_matrix = None
|
| 101 |
searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
|
| 102 |
visits, _ = searcher.search_with_time_budget(game, 1.0)
|
| 103 |
best_move = max(visits, key=visits.get)
|
| 104 |
+
move = move_to_uci(best_move)
|
| 105 |
+
print(f"[DEBUG] get_move OK: FEN={board_text} -> {move}")
|
| 106 |
+
return move
|
|
|
|
| 107 |
|
| 108 |
demo = gr.Interface(
|
| 109 |
fn=get_move,
|