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

update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -56,10 +56,13 @@ def parse_board(board_text):
56
  return board
57
 
58
 
 
 
 
59
  def move_to_uci(move):
60
  fr, fc, tr, tc = move
61
  cols = "abcdefgh"
62
- return cols[fc] + str(8 - fr) + cols[tc] + str(8 - tr)
63
 
64
 
65
  def fen_to_board(fen):
@@ -86,23 +89,25 @@ def fen_to_board(fen):
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
-
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]}")
@@ -110,26 +115,25 @@ def get_move(board_text, player=None):
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}")
 
56
  return board
57
 
58
 
59
+ # ALIGN: coordinate system with bot_runner
60
+ # Rows are 0-indexed, row 0 = TOP (black side), row 7 = BOTTOM (white side).
61
+ # UCI rank = row + 1 (so row 0 -> rank 1, row 7 -> rank 8).
62
  def move_to_uci(move):
63
  fr, fc, tr, tc = move
64
  cols = "abcdefgh"
65
+ return cols[fc] + str(fr + 1) + cols[tc] + str(tr + 1)
66
 
67
 
68
  def fen_to_board(fen):
 
89
  raise ValueError(f"Row {r} has {c} columns, should be 8")
90
  return board
91
 
92
+ # ALIGN: only return legal moves in get_move
93
  def get_move(board_text, player=None):
94
  print(f"[DEBUG] get_move FEN board_text={repr(board_text)}, player={player}")
95
  game = Breakthrough()
96
  game.board = fen_to_board(board_text)
 
97
  if player == "1":
98
  game.move_count = 0
99
  elif player == "2":
100
  game.move_count = 1
101
  else:
102
+ # Default: infer from FEN suffix or assume white
103
  game.move_count = 0 if " w" in board_text.lower() else 1
 
104
  game._cached_matrix = None
105
  searcher = MCVSSearcher(None, None, zonedb, lambda_zone=1.0, k_zone=5)
106
  visits, _ = searcher.search_with_time_budget(game, 1.0)
107
 
108
+ # Debug: show how many legal moves and visits
109
  legal_moves = list(game.get_legal_moves())
110
+ legal_set = set(legal_moves)
111
  print(f"[DEBUG] legal moves count: {len(legal_moves)}")
112
  if legal_moves:
113
  print(f"[DEBUG] sample legal move: {legal_moves[0]}")
 
115
  if visits:
116
  print(f"[DEBUG] sample visit key: {next(iter(visits))}")
117
 
118
+ # Pick the best visited move that is also legal
119
+ best_move = None
120
  if visits:
121
+ for candidate in sorted(visits, key=visits.get, reverse=True):
122
+ if candidate in legal_set:
123
+ best_move = candidate
124
+ break
125
+ if best_move is None:
126
+ print(f"[WARNING] no visited move is legal; visited={list(visits.keys())[:5]}")
127
+
128
+ # Fall back to first legal move if needed
129
+ if best_move is None:
130
+ if legal_moves:
 
 
 
 
 
 
131
  best_move = legal_moves[0]
132
+ print(f"[WARNING] using first legal move as fallback: {best_move}")
133
+ else:
134
+ # No legal moves at all — return a safe dummy
135
+ print("[WARNING] no legal moves in position; returning dummy move")
136
+ return move_to_uci((3, 3, 3, 3))
137
 
138
  move = move_to_uci(best_move)
139
  print(f"[DEBUG] get_move OK: FEN={board_text} -> {move}")