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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -124
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import streamlit as st
2
- import json
3
 
4
  # Set page config to wide mode
5
  st.set_page_config(layout="wide")
@@ -20,140 +19,86 @@ if 'current_player' 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"""
52
- <html>
53
- <head>
54
- <style>
55
- .container {{
56
- width: 100%;
57
- padding: 20px;
58
- display: flex;
59
- flex-direction: column;
60
- align-items: center;
61
- }}
62
- .chess-board {{
63
  width: 100%;
64
- max-width: 1400px;
65
- aspect-ratio: 1;
66
- display: grid;
67
- grid-template-columns: repeat(8, 1fr);
68
- gap: 2px;
69
- padding: 10px;
70
- background: #2f2f2f;
71
- margin: 0 auto;
72
- }}
73
- .square {{
74
- aspect-ratio: 1;
75
  display: flex;
76
  align-items: center;
77
  justify-content: center;
78
- font-size: 4em;
79
- cursor: pointer;
80
- transition: background-color 0.2s;
81
- user-select: none;
82
- }}
83
- .square:hover {{
84
- opacity: 0.8;
85
- }}
86
- .white {{
87
- background: #e8e8e8;
88
- }}
89
- .black {{
90
- background: #b0b0b0;
91
- }}
92
- .selected {{
93
- background: #ffd700 !important;
94
- }}
95
- .game-info {{
96
- font-size: 2em;
97
- margin: 20px;
98
- text-align: center;
99
- color: #333;
100
- }}
101
- </style>
102
- </head>
103
- <body>
104
- <div class="container">
105
- <div class="game-info">
106
- Current Player: {'White' if st.session_state.current_player == 'white' else 'Black'}
107
- </div>
108
- <div class="chess-board" id="board">
109
- """
110
-
111
- # Generate the squares with pieces
112
- for row in range(8):
113
- for col in range(8):
114
- piece = st.session_state.board[row][col] or ''
115
- is_selected = (st.session_state.selected_piece and
116
- st.session_state.selected_piece['row'] == row and
117
- st.session_state.selected_piece['col'] == col)
118
- square_color = 'white' if (row + col) % 2 == 0 else 'black'
119
- selected_class = ' selected' if is_selected else ''
120
- chess_board_html += f"""
121
- <div class="square {square_color}{selected_class}"
122
- onclick="handleSquareClick({row}, {col})"
123
- data-row="{row}"
124
- data-col="{col}">
125
- {piece}
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,5 +111,4 @@ if st.button('Reset Game'):
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()
 
1
  import streamlit as st
 
2
 
3
  # Set page config to wide mode
4
  st.set_page_config(layout="wide")
 
19
  st.session_state.current_player = 'white'
20
  if 'selected_piece' not in st.session_state:
21
  st.session_state.selected_piece = None
 
 
22
 
23
+ def handle_square_click(row, col):
24
+ if st.session_state.selected_piece is None:
25
+ # Select a piece
26
+ if st.session_state.board[row][col] is not None:
27
+ st.session_state.selected_piece = {'row': row, 'col': col}
28
+ st.rerun()
29
+ else:
30
+ # Move the selected piece
31
+ from_row = st.session_state.selected_piece['row']
32
+ from_col = st.session_state.selected_piece['col']
33
 
34
+ # Make the move
35
+ st.session_state.board[row][col] = st.session_state.board[from_row][from_col]
36
+ st.session_state.board[from_row][from_col] = None
37
+ st.session_state.selected_piece = None
38
+ st.session_state.current_player = 'black' if st.session_state.current_player == 'white' else 'white'
 
 
 
 
 
 
 
 
 
 
 
 
39
  st.rerun()
40
 
41
+ # Create buttons for each square using Streamlit's native components
42
+ st.write(f"## Chess Game")
43
+ st.write(f"Current Player: {'White' if st.session_state.current_player == 'white' else 'Black'}")
44
+
45
+ # Create the chessboard using a grid of columns
46
+ board_container = st.container()
47
+ with board_container:
48
+ # CSS for the chess board
49
+ st.markdown("""
50
+ <style>
51
+ .stButton > button {
 
 
52
  width: 100%;
53
+ height: 0;
54
+ padding-bottom: 100%;
55
+ font-size: 3em;
 
 
 
 
 
 
 
 
56
  display: flex;
57
  align-items: center;
58
  justify-content: center;
59
+ background-color: transparent;
60
+ border: none;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
+ .chess-square-white {
63
+ background-color: #e8e8e8 !important;
64
+ }
65
+ .chess-square-black {
66
+ background-color: #b0b0b0 !important;
67
+ }
68
+ .chess-square-selected {
69
+ background-color: #ffd700 !important;
70
+ }
71
+ </style>
72
+ """, unsafe_allow_html=True)
73
 
74
+ # Create the board grid
75
+ for row in range(8):
76
+ cols = st.columns(8)
77
+ for col in range(8):
78
+ with cols[col]:
79
+ piece = st.session_state.board[row][col] or ' '
80
+ is_selected = (st.session_state.selected_piece and
81
+ st.session_state.selected_piece['row'] == row and
82
+ st.session_state.selected_piece['col'] == col)
83
+
84
+ square_color = 'white' if (row + col) % 2 == 0 else 'black'
85
+ if is_selected:
86
+ class_name = "chess-square-selected"
87
+ else:
88
+ class_name = f"chess-square-{square_color}"
89
+
90
+ # Create a button for each square
91
+ if st.button(
92
+ piece,
93
+ key=f"square_{row}_{col}",
94
+ help=f"Row {row}, Col {col}",
95
+ type="secondary",
96
+ use_container_width=True
97
+ ):
98
+ handle_square_click(row, col)
99
 
100
  # Add a reset button
101
+ if st.button('Reset Game', key='reset'):
102
  st.session_state.board = [
103
  ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
104
  ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
 
111
  ]
112
  st.session_state.current_player = 'white'
113
  st.session_state.selected_piece = None
 
114
  st.rerun()