Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +81 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,83 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 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 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import chess
|
| 3 |
+
import chess.svg
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="♟️ Chess Game", layout="centered")
|
| 6 |
+
|
| 7 |
+
st.title("♟️ Chess Game (Streamlit)")
|
| 8 |
+
|
| 9 |
+
# Initialize session state
|
| 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 |
+
# Display board
|
| 17 |
+
st.subheader("Board")
|
| 18 |
+
board_svg = chess.svg.board(board=board, size=500)
|
| 19 |
+
st.image(board_svg)
|
| 20 |
+
|
| 21 |
+
# Show turn
|
| 22 |
+
st.info(f"Turn: {'White' if board.turn else 'Black'}")
|
| 23 |
+
|
| 24 |
+
# Move input
|
| 25 |
+
st.subheader("Make a Move")
|
| 26 |
+
|
| 27 |
+
col1, col2 = st.columns(2)
|
| 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 |
+
# Make move
|
| 38 |
+
if st.button("♟️ Move"):
|
| 39 |
+
try:
|
| 40 |
+
move = chess.Move.from_uci(from_sq + to_sq)
|
| 41 |
+
|
| 42 |
+
if move in board.legal_moves:
|
| 43 |
+
board.push(move)
|
| 44 |
+
st.session_state.move_history.append(move.uci())
|
| 45 |
+
st.success(f"Move: {from_sq} → {to_sq}")
|
| 46 |
+
else:
|
| 47 |
+
st.error("Illegal move!")
|
| 48 |
+
|
| 49 |
+
except:
|
| 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 |
+
# Move history
|
| 66 |
+
st.subheader("📜 Move History")
|
| 67 |
+
if st.session_state.move_history:
|
| 68 |
+
for i, move in enumerate(st.session_state.move_history, 1):
|
| 69 |
+
st.write(f"{i}. {move}")
|
| 70 |
+
else:
|
| 71 |
+
st.write("No moves yet")
|
| 72 |
+
|
| 73 |
+
# Game status
|
| 74 |
+
st.subheader("Game Status")
|
| 75 |
+
|
| 76 |
+
if board.is_checkmate():
|
| 77 |
+
st.error("♟️ Checkmate!")
|
| 78 |
+
elif board.is_stalemate():
|
| 79 |
+
st.warning("Stalemate!")
|
| 80 |
+
elif board.is_check():
|
| 81 |
+
st.warning("Check!")
|
| 82 |
+
else:
|
| 83 |
+
st.success("Game in progress...")
|