pradeep4321 commited on
Commit
79b998f
Β·
verified Β·
1 Parent(s): d8ae45f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +54 -46
src/streamlit_app.py CHANGED
@@ -4,73 +4,81 @@ import chess.svg
4
 
5
  st.set_page_config(page_title="β™ŸοΈ Chess Game", layout="centered")
6
 
7
- st.title("β™ŸοΈ Chess Game (Streamlit)")
8
 
9
- # Initialize session state
10
  if "board" not in st.session_state:
11
  st.session_state.board = chess.Board()
12
  st.session_state.move_history = []
 
13
 
14
  board = st.session_state.board
15
 
16
- # Display board
17
- st.subheader("Board")
18
- board_svg = chess.svg.board(board=board, size=500)
19
- st.image(board_svg)
20
 
21
- # Show turn
22
- st.info(f"Turn: {'White' if board.turn else 'Black'}")
 
 
 
23
 
24
- # Move input
25
- st.subheader("Make a Move")
 
26
 
27
- col1, col2 = st.columns(2)
28
 
29
- squares = [chess.square_name(sq) for sq in chess.SQUARES]
30
 
31
- with col1:
32
  from_sq = st.selectbox("From", squares, key="from")
33
-
34
- with col2:
35
  to_sq = st.selectbox("To", squares, key="to")
36
 
37
- # Make move
38
- if st.button("β™ŸοΈ Move"):
39
- try:
40
- move = chess.Move.from_uci(from_sq + to_sq)
41
-
42
- if move in board.legal_moves:
43
- board.push(move)
44
- st.session_state.move_history.append(move.uci())
45
- st.success(f"Move: {from_sq} β†’ {to_sq}")
46
- else:
47
- st.error("Illegal move!")
48
-
49
- except:
50
- st.error("Invalid move format!")
51
-
52
- # Undo move
53
- if st.button("↩️ Undo Last Move"):
54
- if board.move_stack:
55
- board.pop()
56
- st.session_state.move_history.pop()
57
- st.warning("Last move undone")
58
-
59
- # Restart game
60
- if st.button("πŸ”„ Restart Game"):
61
- st.session_state.board = chess.Board()
62
- st.session_state.move_history = []
63
- st.success("Game restarted!")
64
 
65
- # Move history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  st.subheader("πŸ“œ Move History")
 
67
  if st.session_state.move_history:
68
- for i, move in enumerate(st.session_state.move_history, 1):
69
- st.write(f"{i}. {move}")
70
  else:
71
  st.write("No moves yet")
72
 
73
- # Game status
74
  st.subheader("Game Status")
75
 
76
  if board.is_checkmate():
 
4
 
5
  st.set_page_config(page_title="β™ŸοΈ Chess Game", layout="centered")
6
 
7
+ st.title("β™ŸοΈ Chess Game")
8
 
9
+ # --- Initialize session ---
10
  if "board" not in st.session_state:
11
  st.session_state.board = chess.Board()
12
  st.session_state.move_history = []
13
+ st.session_state.last_move_msg = ""
14
 
15
  board = st.session_state.board
16
 
17
+ # --- Layout ---
18
+ col_board, col_controls = st.columns([2, 1])
 
 
19
 
20
+ # ================= BOARD =================
21
+ with col_board:
22
+ st.subheader("Board")
23
+ board_svg = chess.svg.board(board=board, size=500)
24
+ st.image(board_svg)
25
 
26
+ # ================= CONTROLS =================
27
+ with col_controls:
28
+ st.subheader("Controls")
29
 
30
+ st.info(f"Turn: {'White' if board.turn else 'Black'}")
31
 
32
+ squares = [chess.square_name(sq) for sq in chess.SQUARES]
33
 
 
34
  from_sq = st.selectbox("From", squares, key="from")
 
 
35
  to_sq = st.selectbox("To", squares, key="to")
36
 
37
+ if st.button("β™ŸοΈ Move"):
38
+ try:
39
+ move = chess.Move.from_uci(from_sq + to_sq)
40
+
41
+ if move in board.legal_moves:
42
+ board.push(move)
43
+ st.session_state.move_history.append(move.uci())
44
+
45
+ # βœ… Store message once
46
+ st.session_state.last_move_msg = f"Moved: {from_sq} β†’ {to_sq}"
47
+
48
+ else:
49
+ st.session_state.last_move_msg = "❌ Illegal move!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ except:
52
+ st.session_state.last_move_msg = "❌ Invalid input!"
53
+
54
+ # βœ… Show message ONCE (not repeating)
55
+ if st.session_state.last_move_msg:
56
+ st.success(st.session_state.last_move_msg)
57
+ st.session_state.last_move_msg = "" # clear after showing
58
+
59
+ # --- Buttons ---
60
+ col_btn1, col_btn2 = st.columns(2)
61
+
62
+ with col_btn1:
63
+ if st.button("↩️ Undo"):
64
+ if board.move_stack:
65
+ board.pop()
66
+ st.session_state.move_history.pop()
67
+
68
+ with col_btn2:
69
+ if st.button("πŸ”„ Reset"):
70
+ st.session_state.board = chess.Board()
71
+ st.session_state.move_history = []
72
+
73
+ # ================= HISTORY =================
74
  st.subheader("πŸ“œ Move History")
75
+
76
  if st.session_state.move_history:
77
+ st.write(", ".join(st.session_state.move_history))
 
78
  else:
79
  st.write("No moves yet")
80
 
81
+ # ================= STATUS =================
82
  st.subheader("Game Status")
83
 
84
  if board.is_checkmate():