Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +58 -42
src/streamlit_app.py
CHANGED
@@ -146,24 +146,61 @@ if st.button("Reset Game"):
|
|
146 |
st.session_state.history = []
|
147 |
st.rerun()
|
148 |
|
149 |
-
|
|
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
158 |
|
159 |
-
render_board()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
st.
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
if not board.is_game_over() and board.turn == chess.WHITE:
|
169 |
st.write("### Your Turn (White)")
|
@@ -194,6 +231,8 @@ if not board.is_game_over() and board.turn == chess.WHITE:
|
|
194 |
try:
|
195 |
board.push(parsed_move)
|
196 |
history.append(parsed_move.uci())
|
|
|
|
|
197 |
st.success(f"You played: {board.san(parsed_move)} ({parsed_move.uci()})")
|
198 |
st.rerun()
|
199 |
except Exception as e:
|
@@ -208,6 +247,8 @@ if not board.is_game_over() and board.turn == chess.WHITE:
|
|
208 |
random_move = random.choice(legal_moves)
|
209 |
board.push(random_move)
|
210 |
history.append(random_move.uci())
|
|
|
|
|
211 |
st.rerun()
|
212 |
|
213 |
if not board.is_game_over() and board.turn == chess.BLACK:
|
@@ -224,6 +265,8 @@ if not board.is_game_over() and board.turn == chess.BLACK:
|
|
224 |
move_san = board.san(move)
|
225 |
board.push(move)
|
226 |
history.append(move.uci())
|
|
|
|
|
227 |
st.success(f"{ai_name} played: {move_san} ({move.uci()})")
|
228 |
st.rerun()
|
229 |
else:
|
@@ -231,33 +274,6 @@ if not board.is_game_over() and board.turn == chess.BLACK:
|
|
231 |
except Exception as e:
|
232 |
st.error(f"Error during {ai_name} move: {e}")
|
233 |
|
234 |
-
if history:
|
235 |
-
st.write("### Game History")
|
236 |
-
try:
|
237 |
-
game = chess.pgn.Game()
|
238 |
-
game.headers["Event"] = "Human vs o2"
|
239 |
-
game.headers["White"] = "Human"
|
240 |
-
game.headers["Black"] = "o2" if agent_loaded else "Random AI"
|
241 |
-
node = game
|
242 |
-
temp_board = chess.Board()
|
243 |
-
for uci in history:
|
244 |
-
move = chess.Move.from_uci(uci)
|
245 |
-
if move in temp_board.legal_moves:
|
246 |
-
node = node.add_main_variation(move)
|
247 |
-
temp_board.push(move)
|
248 |
-
else:
|
249 |
-
st.warning(f"Invalid move in history: {uci}")
|
250 |
-
break
|
251 |
-
st.code(str(game), language="pgn")
|
252 |
-
except Exception as e:
|
253 |
-
st.error(f"Error generating PGN: {e}")
|
254 |
-
move_pairs = []
|
255 |
-
for i in range(0, len(history), 2):
|
256 |
-
white_move = history[i]
|
257 |
-
black_move = history[i+1] if i+1 < len(history) else ""
|
258 |
-
move_pairs.append(f"{i//2 + 1}. {white_move} {black_move}")
|
259 |
-
st.code("\n".join(move_pairs))
|
260 |
-
|
261 |
if board.is_game_over():
|
262 |
st.write("### Game Over!")
|
263 |
result = board.result()
|
|
|
146 |
st.session_state.history = []
|
147 |
st.rerun()
|
148 |
|
149 |
+
# Create two columns for layout
|
150 |
+
col_board, col_pgn = st.columns([2, 1])
|
151 |
|
152 |
+
with col_board:
|
153 |
+
board_placeholder = st.empty()
|
154 |
+
|
155 |
+
def render_board():
|
156 |
+
try:
|
157 |
+
last_move = board.peek() if board.move_stack else None
|
158 |
+
svg_board = chess.svg.board(board=board, lastmove=last_move, size=400)
|
159 |
+
board_placeholder.markdown(f'<div style="display: flex; justify-content: center;">{svg_board}</div>', unsafe_allow_html=True)
|
160 |
+
except Exception as e:
|
161 |
+
st.error(f"Error rendering board: {e}")
|
162 |
|
163 |
+
render_board()
|
164 |
+
|
165 |
+
col1, col2 = st.columns(2)
|
166 |
+
with col1:
|
167 |
+
st.write(f"**Turn:** {'White' if board.turn == chess.WHITE else 'Black'}")
|
168 |
+
with col2:
|
169 |
+
if board.is_check():
|
170 |
+
st.write("**Check!**")
|
171 |
|
172 |
+
with col_pgn:
|
173 |
+
st.write("### Game History")
|
174 |
+
pgn_placeholder = st.empty()
|
175 |
+
|
176 |
+
def render_pgn():
|
177 |
+
if history:
|
178 |
+
try:
|
179 |
+
game = chess.pgn.Game()
|
180 |
+
game.headers["Event"] = "Human vs o2"
|
181 |
+
game.headers["White"] = "Human"
|
182 |
+
game.headers["Black"] = "o2" if agent_loaded else "Random AI"
|
183 |
+
node = game
|
184 |
+
temp_board = chess.Board()
|
185 |
+
for uci in history:
|
186 |
+
move = chess.Move.from_uci(uci)
|
187 |
+
if move in temp_board.legal_moves:
|
188 |
+
node = node.add_main_variation(move)
|
189 |
+
temp_board.push(move)
|
190 |
+
else:
|
191 |
+
break
|
192 |
+
pgn_placeholder.code(str(game), language="pgn")
|
193 |
+
except Exception as e:
|
194 |
+
move_pairs = []
|
195 |
+
for i in range(0, len(history), 2):
|
196 |
+
white_move = history[i]
|
197 |
+
black_move = history[i+1] if i+1 < len(history) else ""
|
198 |
+
move_pairs.append(f"{i//2 + 1}. {white_move} {black_move}")
|
199 |
+
pgn_placeholder.code("\n".join(move_pairs))
|
200 |
+
else:
|
201 |
+
pgn_placeholder.text("No moves yet")
|
202 |
+
|
203 |
+
render_pgn()
|
204 |
|
205 |
if not board.is_game_over() and board.turn == chess.WHITE:
|
206 |
st.write("### Your Turn (White)")
|
|
|
231 |
try:
|
232 |
board.push(parsed_move)
|
233 |
history.append(parsed_move.uci())
|
234 |
+
render_board() # Update board immediately
|
235 |
+
render_pgn() # Update PGN immediately
|
236 |
st.success(f"You played: {board.san(parsed_move)} ({parsed_move.uci()})")
|
237 |
st.rerun()
|
238 |
except Exception as e:
|
|
|
247 |
random_move = random.choice(legal_moves)
|
248 |
board.push(random_move)
|
249 |
history.append(random_move.uci())
|
250 |
+
render_board() # Update board immediately
|
251 |
+
render_pgn() # Update PGN immediately
|
252 |
st.rerun()
|
253 |
|
254 |
if not board.is_game_over() and board.turn == chess.BLACK:
|
|
|
265 |
move_san = board.san(move)
|
266 |
board.push(move)
|
267 |
history.append(move.uci())
|
268 |
+
render_board() # Update board immediately
|
269 |
+
render_pgn() # Update PGN immediately
|
270 |
st.success(f"{ai_name} played: {move_san} ({move.uci()})")
|
271 |
st.rerun()
|
272 |
else:
|
|
|
274 |
except Exception as e:
|
275 |
st.error(f"Error during {ai_name} move: {e}")
|
276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
if board.is_game_over():
|
278 |
st.write("### Game Over!")
|
279 |
result = board.result()
|