test1978 commited on
Commit
720d46a
·
verified ·
1 Parent(s): 5456563

update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -90,35 +90,46 @@ 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
- # Default: infer from FEN suffix or assume white
99
  game.move_count = 0 if " w" in board_text.lower() 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
 
104
- # Debug: show how many legal moves and visits
105
  legal_moves = list(game.get_legal_moves())
106
  print(f"[DEBUG] legal moves count: {len(legal_moves)}")
107
  if legal_moves:
108
- print(f"[DEBUG] sample legal move: {legal_moves[0] if len(legal_moves) > 0 else None}")
109
  print(f"[DEBUG] visits count: {len(visits)}")
110
  if visits:
111
  print(f"[DEBUG] sample visit key: {next(iter(visits))}")
112
 
113
- # Safely pick a move
114
  if visits:
115
- best_move = max(visits, key=visits.get)
 
 
 
 
 
 
 
 
 
 
116
  else:
117
  if not legal_moves:
118
- raise ValueError("get_move: No legal moves in position.")
119
- # Fallback: use first legal move
120
- best_move = legal_moves[0]
121
- print(f"[WARNING] visits empty; using fallback move: {best_move}")
 
122
 
123
  move = move_to_uci(best_move)
124
  print(f"[DEBUG] get_move OK: FEN={board_text} -> {move}")
 
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
+
94
  if player == "1":
95
  game.move_count = 0
96
  elif player == "2":
97
  game.move_count = 1
98
  else:
 
99
  game.move_count = 0 if " w" in board_text.lower() else 1
100
+
101
  game._cached_matrix = None
102
  searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
103
  visits, _ = searcher.search_with_time_budget(game, 1.0)
104
 
 
105
  legal_moves = list(game.get_legal_moves())
106
  print(f"[DEBUG] legal moves count: {len(legal_moves)}")
107
  if legal_moves:
108
+ print(f"[DEBUG] sample legal move: {legal_moves[0]}")
109
  print(f"[DEBUG] visits count: {len(visits)}")
110
  if visits:
111
  print(f"[DEBUG] sample visit key: {next(iter(visits))}")
112
 
113
+ # Safely pick a move that is actually legal
114
  if visits:
115
+ candidates = [(move, visits[move]) for move in visits if move in set(legal_moves)]
116
+ if candidates:
117
+ best_move, _ = max(candidates, key=lambda x: x[1])
118
+ else:
119
+ # Fallback to first legal move if no visited move is legal
120
+ if not legal_moves:
121
+ print("[WARNING] No legal moves; returning dummy move d4d4.")
122
+ best_move = (3, 3, 3, 3) # d4 -> d4
123
+ else:
124
+ best_move = legal_moves[0]
125
+ print(f"[WARNING] no visited move is legal; using fallback move: {best_move}")
126
  else:
127
  if not legal_moves:
128
+ print("[WARNING] No legal moves; returning dummy move d4d4.")
129
+ best_move = (3, 3, 3, 3)
130
+ else:
131
+ best_move = legal_moves[0]
132
+ print(f"[WARNING] visits empty; using fallback move: {best_move}")
133
 
134
  move = move_to_uci(best_move)
135
  print(f"[DEBUG] get_move OK: FEN={board_text} -> {move}")