awacke1 commited on
Commit
058365b
Β·
verified Β·
1 Parent(s): 1a61bb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -38
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- import json
3
 
4
  # Initialize session state if not already done
5
  if 'board' not in st.session_state:
@@ -17,30 +17,44 @@ if 'current_player' not in st.session_state:
17
  st.session_state.current_player = 'white'
18
  if 'selected_piece' not in st.session_state:
19
  st.session_state.selected_piece = None
 
 
 
 
 
20
 
21
  # Create the HTML/JavaScript for the chess board
22
  chess_board_html = f"""
23
  <html>
24
  <head>
25
  <style>
 
 
 
 
 
 
 
26
  .chess-board {{
27
  width: 100%;
28
- max-width: 1024px;
29
- height: 1024px;
30
  display: grid;
31
  grid-template-columns: repeat(8, 1fr);
32
  gap: 2px;
33
  padding: 10px;
34
  background: #2f2f2f;
 
35
  }}
36
  .square {{
37
  aspect-ratio: 1;
38
  display: flex;
39
  align-items: center;
40
  justify-content: center;
41
- font-size: 3.5em;
42
  cursor: pointer;
43
  transition: background-color 0.2s;
 
44
  }}
45
  .square:hover {{
46
  opacity: 0.8;
@@ -55,17 +69,19 @@ chess_board_html = f"""
55
  background: #ffd700 !important;
56
  }}
57
  .game-info {{
58
- font-size: 1.5em;
59
  margin: 20px;
60
  text-align: center;
 
61
  }}
62
  </style>
63
  </head>
64
  <body>
65
- <div class="game-info">
66
- Current Player: {'White' if st.session_state.current_player == 'white' else 'Black'}
67
- </div>
68
- <div class="chess-board" id="board">
 
69
  """
70
 
71
  # Generate the squares with pieces
@@ -79,25 +95,29 @@ for row in range(8):
79
  selected_class = ' selected' if is_selected else ''
80
  chess_board_html += f"""
81
  <div class="square {square_color}{selected_class}"
82
- onclick="handleClick({row}, {col})"
83
  data-row="{row}"
84
  data-col="{col}">
85
  {piece}
86
  </div>
87
  """
88
 
89
- # Add JavaScript for handling moves
90
  chess_board_html += """
 
91
  </div>
92
  <script>
93
- function handleClick(row, col) {
 
94
  const data = {
95
- row: row,
96
- col: col
97
  };
 
 
98
  window.parent.postMessage({
99
- type: 'streamlit:message',
100
- data: data
101
  }, '*');
102
  }
103
  </script>
@@ -105,31 +125,50 @@ chess_board_html += """
105
  </html>
106
  """
107
 
108
- # Display the chess board
109
- st.components.v1.html(chess_board_html, height=1200)
 
 
 
 
 
 
 
 
 
110
 
111
- # Handle moves through Streamlit's session state
112
- if st.session_state.get('clicked_square'):
113
- clicked = st.session_state.clicked_square
114
- row, col = clicked['row'], clicked['col']
115
-
116
- if st.session_state.selected_piece:
117
- # Move the piece
118
- selected = st.session_state.selected_piece
119
- st.session_state.board[row][col] = st.session_state.board[selected['row']][selected['col']]
120
- st.session_state.board[selected['row']][selected['col']] = None
121
- st.session_state.selected_piece = None
122
- st.session_state.current_player = 'black' if st.session_state.current_player == 'white' else 'white'
123
- elif st.session_state.board[row][col]:
124
- # Select the piece
125
- st.session_state.selected_piece = {'row': row, 'col': col}
126
-
127
- # Clear the clicked square
128
- st.session_state.clicked_square = None
129
- st.rerun()
 
 
 
 
 
 
 
 
 
 
130
 
131
  # Add a reset button
132
- if st.button('Reset Game'):
133
  st.session_state.board = [
134
  ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
135
  ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
@@ -142,4 +181,5 @@ if st.button('Reset Game'):
142
  ]
143
  st.session_state.current_player = 'white'
144
  st.session_state.selected_piece = None
 
145
  st.rerun()
 
1
  import streamlit as st
2
+ import streamlit.components.v1 as components
3
 
4
  # Initialize session state if not already done
5
  if 'board' not in st.session_state:
 
17
  st.session_state.current_player = 'white'
18
  if 'selected_piece' not in st.session_state:
19
  st.session_state.selected_piece = None
20
+ if 'move_made' not in st.session_state:
21
+ st.session_state.move_made = False
22
+
23
+ # Use the full width of the page
24
+ st.set_page_config(layout="wide")
25
 
26
  # Create the HTML/JavaScript for the chess board
27
  chess_board_html = f"""
28
  <html>
29
  <head>
30
  <style>
31
+ .container {{
32
+ width: 100%;
33
+ padding: 20px;
34
+ display: flex;
35
+ flex-direction: column;
36
+ align-items: center;
37
+ }}
38
  .chess-board {{
39
  width: 100%;
40
+ max-width: 1400px;
41
+ aspect-ratio: 1;
42
  display: grid;
43
  grid-template-columns: repeat(8, 1fr);
44
  gap: 2px;
45
  padding: 10px;
46
  background: #2f2f2f;
47
+ margin: 0 auto;
48
  }}
49
  .square {{
50
  aspect-ratio: 1;
51
  display: flex;
52
  align-items: center;
53
  justify-content: center;
54
+ font-size: 4em;
55
  cursor: pointer;
56
  transition: background-color 0.2s;
57
+ user-select: none;
58
  }}
59
  .square:hover {{
60
  opacity: 0.8;
 
69
  background: #ffd700 !important;
70
  }}
71
  .game-info {{
72
+ font-size: 2em;
73
  margin: 20px;
74
  text-align: center;
75
+ color: #333;
76
  }}
77
  </style>
78
  </head>
79
  <body>
80
+ <div class="container">
81
+ <div class="game-info">
82
+ Current Player: {'White' if st.session_state.current_player == 'white' else 'Black'}
83
+ </div>
84
+ <div class="chess-board" id="board">
85
  """
86
 
87
  # Generate the squares with pieces
 
95
  selected_class = ' selected' if is_selected else ''
96
  chess_board_html += f"""
97
  <div class="square {square_color}{selected_class}"
98
+ onclick="handleSquareClick({row}, {col})"
99
  data-row="{row}"
100
  data-col="{col}">
101
  {piece}
102
  </div>
103
  """
104
 
105
+ # Add JavaScript for handling moves with improved interaction
106
  chess_board_html += """
107
+ </div>
108
  </div>
109
  <script>
110
+ function handleSquareClick(row, col) {
111
+ // Send the click data to Streamlit
112
  const data = {
113
+ 'row': row,
114
+ 'col': col
115
  };
116
+
117
+ const encodedData = encodeURIComponent(JSON.stringify(data));
118
  window.parent.postMessage({
119
+ type: 'streamlit:setComponentValue',
120
+ data: encodedData
121
  }, '*');
122
  }
123
  </script>
 
125
  </html>
126
  """
127
 
128
+ # Create a custom component that returns the clicked square
129
+ def chess_board_component():
130
+ component_value = components.html(
131
+ chess_board_html,
132
+ height=1200,
133
+ key="chess_board"
134
+ )
135
+ return component_value
136
+
137
+ # Handle the component interaction
138
+ clicked_square = chess_board_component()
139
 
140
+ if clicked_square:
141
+ try:
142
+ # Parse the clicked square data
143
+ data = json.loads(clicked_square)
144
+ row, col = data['row'], data['col']
145
+
146
+ # Handle the move logic
147
+ if st.session_state.selected_piece is None:
148
+ # Select a piece
149
+ if st.session_state.board[row][col] is not None:
150
+ st.session_state.selected_piece = {'row': row, 'col': col}
151
+ st.session_state.move_made = True
152
+ else:
153
+ # Move the selected piece
154
+ from_row = st.session_state.selected_piece['row']
155
+ from_col = st.session_state.selected_piece['col']
156
+
157
+ # Make the move
158
+ st.session_state.board[row][col] = st.session_state.board[from_row][from_col]
159
+ st.session_state.board[from_row][from_col] = None
160
+ st.session_state.selected_piece = None
161
+ st.session_state.current_player = 'black' if st.session_state.current_player == 'white' else 'white'
162
+ st.session_state.move_made = True
163
+
164
+ if st.session_state.move_made:
165
+ st.rerun()
166
+
167
+ except Exception as e:
168
+ st.error(f"Error processing move: {str(e)}")
169
 
170
  # Add a reset button
171
+ if st.button('Reset Game', key='reset'):
172
  st.session_state.board = [
173
  ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
174
  ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
 
181
  ]
182
  st.session_state.current_player = 'white'
183
  st.session_state.selected_piece = None
184
+ st.session_state.move_made = False
185
  st.rerun()