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