Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +54 -46
src/streamlit_app.py
CHANGED
|
@@ -4,73 +4,81 @@ import chess.svg
|
|
| 4 |
|
| 5 |
st.set_page_config(page_title="βοΈ Chess Game", layout="centered")
|
| 6 |
|
| 7 |
-
st.title("βοΈ Chess Game
|
| 8 |
|
| 9 |
-
# Initialize session
|
| 10 |
if "board" not in st.session_state:
|
| 11 |
st.session_state.board = chess.Board()
|
| 12 |
st.session_state.move_history = []
|
|
|
|
| 13 |
|
| 14 |
board = st.session_state.board
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
st.
|
| 18 |
-
board_svg = chess.svg.board(board=board, size=500)
|
| 19 |
-
st.image(board_svg)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
squares = [chess.square_name(sq) for sq in chess.SQUARES]
|
| 30 |
|
| 31 |
-
with col1:
|
| 32 |
from_sq = st.selectbox("From", squares, key="from")
|
| 33 |
-
|
| 34 |
-
with col2:
|
| 35 |
to_sq = st.selectbox("To", squares, key="to")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.error("Invalid move format!")
|
| 51 |
-
|
| 52 |
-
# Undo move
|
| 53 |
-
if st.button("β©οΈ Undo Last Move"):
|
| 54 |
-
if board.move_stack:
|
| 55 |
-
board.pop()
|
| 56 |
-
st.session_state.move_history.pop()
|
| 57 |
-
st.warning("Last move undone")
|
| 58 |
-
|
| 59 |
-
# Restart game
|
| 60 |
-
if st.button("π Restart Game"):
|
| 61 |
-
st.session_state.board = chess.Board()
|
| 62 |
-
st.session_state.move_history = []
|
| 63 |
-
st.success("Game restarted!")
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
st.subheader("π Move History")
|
|
|
|
| 67 |
if st.session_state.move_history:
|
| 68 |
-
|
| 69 |
-
st.write(f"{i}. {move}")
|
| 70 |
else:
|
| 71 |
st.write("No moves yet")
|
| 72 |
|
| 73 |
-
#
|
| 74 |
st.subheader("Game Status")
|
| 75 |
|
| 76 |
if board.is_checkmate():
|
|
|
|
| 4 |
|
| 5 |
st.set_page_config(page_title="βοΈ Chess Game", layout="centered")
|
| 6 |
|
| 7 |
+
st.title("βοΈ Chess Game")
|
| 8 |
|
| 9 |
+
# --- Initialize session ---
|
| 10 |
if "board" not in st.session_state:
|
| 11 |
st.session_state.board = chess.Board()
|
| 12 |
st.session_state.move_history = []
|
| 13 |
+
st.session_state.last_move_msg = ""
|
| 14 |
|
| 15 |
board = st.session_state.board
|
| 16 |
|
| 17 |
+
# --- Layout ---
|
| 18 |
+
col_board, col_controls = st.columns([2, 1])
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# ================= BOARD =================
|
| 21 |
+
with col_board:
|
| 22 |
+
st.subheader("Board")
|
| 23 |
+
board_svg = chess.svg.board(board=board, size=500)
|
| 24 |
+
st.image(board_svg)
|
| 25 |
|
| 26 |
+
# ================= CONTROLS =================
|
| 27 |
+
with col_controls:
|
| 28 |
+
st.subheader("Controls")
|
| 29 |
|
| 30 |
+
st.info(f"Turn: {'White' if board.turn else 'Black'}")
|
| 31 |
|
| 32 |
+
squares = [chess.square_name(sq) for sq in chess.SQUARES]
|
| 33 |
|
|
|
|
| 34 |
from_sq = st.selectbox("From", squares, key="from")
|
|
|
|
|
|
|
| 35 |
to_sq = st.selectbox("To", squares, key="to")
|
| 36 |
|
| 37 |
+
if st.button("βοΈ Move"):
|
| 38 |
+
try:
|
| 39 |
+
move = chess.Move.from_uci(from_sq + to_sq)
|
| 40 |
+
|
| 41 |
+
if move in board.legal_moves:
|
| 42 |
+
board.push(move)
|
| 43 |
+
st.session_state.move_history.append(move.uci())
|
| 44 |
+
|
| 45 |
+
# β
Store message once
|
| 46 |
+
st.session_state.last_move_msg = f"Moved: {from_sq} β {to_sq}"
|
| 47 |
+
|
| 48 |
+
else:
|
| 49 |
+
st.session_state.last_move_msg = "β Illegal move!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
except:
|
| 52 |
+
st.session_state.last_move_msg = "β Invalid input!"
|
| 53 |
+
|
| 54 |
+
# β
Show message ONCE (not repeating)
|
| 55 |
+
if st.session_state.last_move_msg:
|
| 56 |
+
st.success(st.session_state.last_move_msg)
|
| 57 |
+
st.session_state.last_move_msg = "" # clear after showing
|
| 58 |
+
|
| 59 |
+
# --- Buttons ---
|
| 60 |
+
col_btn1, col_btn2 = st.columns(2)
|
| 61 |
+
|
| 62 |
+
with col_btn1:
|
| 63 |
+
if st.button("β©οΈ Undo"):
|
| 64 |
+
if board.move_stack:
|
| 65 |
+
board.pop()
|
| 66 |
+
st.session_state.move_history.pop()
|
| 67 |
+
|
| 68 |
+
with col_btn2:
|
| 69 |
+
if st.button("π Reset"):
|
| 70 |
+
st.session_state.board = chess.Board()
|
| 71 |
+
st.session_state.move_history = []
|
| 72 |
+
|
| 73 |
+
# ================= HISTORY =================
|
| 74 |
st.subheader("π Move History")
|
| 75 |
+
|
| 76 |
if st.session_state.move_history:
|
| 77 |
+
st.write(", ".join(st.session_state.move_history))
|
|
|
|
| 78 |
else:
|
| 79 |
st.write("No moves yet")
|
| 80 |
|
| 81 |
+
# ================= STATUS =================
|
| 82 |
st.subheader("Game Status")
|
| 83 |
|
| 84 |
if board.is_checkmate():
|