pradeep4321 commited on
Commit
af09c74
ยท
verified ยท
1 Parent(s): 110eba8

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +106 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,108 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import chess
3
+ import chess.svg
4
 
5
+ st.set_page_config(page_title="Chess Game", layout="wide")
6
+
7
+ # ================= CSS (compact layout) =================
8
+ st.markdown("""
9
+ <style>
10
+ .block-container {
11
+ padding-top: 1rem;
12
+ padding-bottom: 1rem;
13
+ }
14
+
15
+ /* Reduce spacing between columns */
16
+ div[data-testid="column"] {
17
+ padding: 0 !important;
18
+ }
19
+
20
+ div[data-testid="stHorizontalBlock"] {
21
+ gap: 5px !important;
22
+ }
23
+ </style>
24
+ """, unsafe_allow_html=True)
25
+
26
+ st.title("โ™Ÿ๏ธ Chess Game")
27
+
28
+ # ================= INIT =================
29
+ if "board" not in st.session_state:
30
+ st.session_state.board = chess.Board()
31
+ st.session_state.selected = None
32
+ st.session_state.message = ""
33
+
34
+ board = st.session_state.board
35
+
36
+ # ================= HANDLE CLICK =================
37
+ def select_square(square):
38
+ if st.session_state.selected is None:
39
+ piece = board.piece_at(square)
40
+ if piece and piece.color == board.turn:
41
+ st.session_state.selected = square
42
+ else:
43
+ move = chess.Move(st.session_state.selected, square)
44
+
45
+ if move in board.legal_moves:
46
+ board.push(move)
47
+ st.session_state.message = "โœ… Move played"
48
+ else:
49
+ st.session_state.message = "โŒ Illegal move"
50
+
51
+ st.session_state.selected = None
52
+
53
+ # ================= LAYOUT =================
54
+ col_board, col_controls = st.columns([2.3, 1])
55
+
56
+ # ================= BOARD =================
57
+ with col_board:
58
+ st.subheader(f"Turn: {'White' if board.turn else 'Black'}")
59
+
60
+ selected_square = st.session_state.selected
61
+
62
+ svg_board = chess.svg.board(
63
+ board=board,
64
+ size=720, # ๐Ÿ”ฅ big board for better UX
65
+ lastmove=board.peek() if board.move_stack else None,
66
+ squares=[selected_square] if selected_square else []
67
+ )
68
+
69
+ st.image(svg_board)
70
+
71
+ # ================= CONTROLS =================
72
+ with col_controls:
73
+ st.subheader("๐ŸŽฎ Controls")
74
+
75
+ for row in range(7, -1, -1):
76
+ cols = st.columns(8)
77
+ for col in range(8):
78
+ square = chess.square(col, row)
79
+ name = chess.square_name(square)
80
+
81
+ if cols[col].button(name, key=name):
82
+ select_square(square)
83
+
84
+ if st.session_state.message:
85
+ st.success(st.session_state.message)
86
+ st.session_state.message = ""
87
+
88
+ st.markdown("---")
89
+
90
+ if st.button("โ†ฉ๏ธ Undo"):
91
+ if board.move_stack:
92
+ board.pop()
93
+
94
+ if st.button("๐Ÿ”„ Reset"):
95
+ st.session_state.board = chess.Board()
96
+ st.session_state.selected = None
97
+
98
+ # ================= STATUS =================
99
+ st.subheader("Game Status")
100
+
101
+ if board.is_checkmate():
102
+ st.error("โ™Ÿ๏ธ Checkmate!")
103
+ elif board.is_stalemate():
104
+ st.warning("Stalemate!")
105
+ elif board.is_check():
106
+ st.warning("Check!")
107
+ else:
108
+ st.success("Game in progress...")