awacke1 commited on
Commit
1822f98
·
verified ·
1 Parent(s): df7afd8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import chess
3
+ import chess.svg
4
+
5
+ def main():
6
+ st.set_page_config(page_title="Streamlit Chess", layout="wide")
7
+ st.title("Streamlit Chess")
8
+
9
+ # Initialize the chess board
10
+ if 'board' not in st.session_state:
11
+ st.session_state.board = chess.Board()
12
+ st.session_state.selected_square = None
13
+
14
+ # Create two columns
15
+ col1, col2 = st.columns([3, 1])
16
+
17
+ with col1:
18
+ # Display the chess board
19
+ svg = chess.svg.board(st.session_state.board, size=700)
20
+ st.image(svg)
21
+
22
+ # Handle clicks on the board
23
+ clicked = st.button("Click to select/move")
24
+ if clicked:
25
+ handle_click()
26
+
27
+ with col2:
28
+ # Display game status
29
+ st.subheader("Game Status")
30
+ st.write(f"Turn: {'White' if st.session_state.board.turn else 'Black'}")
31
+ st.write(f"Check: {'Yes' if st.session_state.board.is_check() else 'No'}")
32
+ st.write(f"Game Over: {'Yes' if st.session_state.board.is_game_over() else 'No'}")
33
+
34
+ if st.session_state.board.is_game_over():
35
+ st.write(f"Result: {st.session_state.board.result()}")
36
+
37
+ # Reset button
38
+ if st.button("Reset Game"):
39
+ st.session_state.board = chess.Board()
40
+ st.session_state.selected_square = None
41
+ st.experimental_rerun()
42
+
43
+ def handle_click():
44
+ # Get the mouse position
45
+ pos = st.experimental_get_query_params().get("pos", [""])[0]
46
+ if not pos:
47
+ return
48
+
49
+ # Convert mouse position to chess square
50
+ x, y = map(int, pos.split(","))
51
+ file = int(x * 8 / 700)
52
+ rank = 7 - int(y * 8 / 700)
53
+ square = chess.square(file, rank)
54
+
55
+ if st.session_state.selected_square is None:
56
+ # Select the piece
57
+ if st.session_state.board.piece_at(square):
58
+ st.session_state.selected_square = square
59
+ else:
60
+ # Try to move the piece
61
+ move = chess.Move(st.session_state.selected_square, square)
62
+ if move in st.session_state.board.legal_moves:
63
+ st.session_state.board.push(move)
64
+ st.session_state.selected_square = None
65
+
66
+ # Force a rerun to update the board
67
+ st.experimental_rerun()
68
+
69
+ if __name__ == "__main__":
70
+ main()