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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -58
app.py CHANGED
@@ -1,5 +1,8 @@
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,11 +20,32 @@ 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
- 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"""
@@ -102,73 +126,34 @@ for row in range(8):
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>
124
  </body>
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,5 +166,5 @@ if st.button('Reset Game', key='reset'):
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()
 
1
  import streamlit as st
2
+ import json
3
+
4
+ # Set page config to wide mode
5
+ st.set_page_config(layout="wide")
6
 
7
  # Initialize session state if not already done
8
  if 'board' not in st.session_state:
 
20
  st.session_state.current_player = 'white'
21
  if 'selected_piece' not in st.session_state:
22
  st.session_state.selected_piece = None
23
+ if 'last_move' not in st.session_state:
24
+ st.session_state.last_move = None
25
 
26
+ def handle_move():
27
+ if 'last_move' in st.session_state and st.session_state.last_move:
28
+ move_data = json.loads(st.session_state.last_move)
29
+ row, col = move_data['row'], move_data['col']
30
+
31
+ if st.session_state.selected_piece is None:
32
+ # Select a piece
33
+ if st.session_state.board[row][col] is not None:
34
+ st.session_state.selected_piece = {'row': row, 'col': col}
35
+ else:
36
+ # Move the selected piece
37
+ from_row = st.session_state.selected_piece['row']
38
+ from_col = st.session_state.selected_piece['col']
39
+
40
+ # Make the move
41
+ st.session_state.board[row][col] = st.session_state.board[from_row][from_col]
42
+ st.session_state.board[from_row][from_col] = None
43
+ st.session_state.selected_piece = None
44
+ st.session_state.current_player = 'black' if st.session_state.current_player == 'white' else 'white'
45
+
46
+ # Clear the last move
47
+ st.session_state.last_move = None
48
+ st.rerun()
49
 
50
  # Create the HTML/JavaScript for the chess board
51
  chess_board_html = f"""
 
126
  </div>
127
  """
128
 
129
+ # Add JavaScript for handling moves with Streamlit state management
130
  chess_board_html += """
131
  </div>
132
  </div>
133
  <script>
134
  function handleSquareClick(row, col) {
 
135
  const data = {
136
+ row: row,
137
+ col: col
138
  };
139
+ window.parent.Streamlit.setComponentValue(JSON.stringify(data));
 
 
 
 
 
140
  }
141
+
142
+ // Initialize Streamlit component
143
+ window.parent.Streamlit.setComponentReady();
144
  </script>
145
  </body>
146
  </html>
147
  """
148
 
149
+ # Display the chess board and handle moves
150
+ component_value = st.components.v1.html(chess_board_html, height=1200)
151
+ if component_value:
152
+ st.session_state.last_move = component_value
153
+ handle_move()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  # Add a reset button
156
+ if st.button('Reset Game'):
157
  st.session_state.board = [
158
  ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
159
  ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
 
166
  ]
167
  st.session_state.current_player = 'white'
168
  st.session_state.selected_piece = None
169
+ st.session_state.last_move = None
170
  st.rerun()