File size: 4,122 Bytes
1a61bb6
ec3e191
 
 
1a61bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
058365b
f1b315c
 
 
 
 
 
 
 
 
 
ec3e191
f1b315c
 
 
 
 
ec3e191
1a61bb6
f1b315c
 
 
 
 
 
 
 
 
 
 
1a61bb6
f1b315c
 
 
1a61bb6
 
 
f1b315c
 
1a61bb6
f1b315c
 
 
 
 
 
 
 
 
 
 
ec3e191
f1b315c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a61bb6
 
f1b315c
1a61bb6
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import streamlit as st

# Set page config to wide mode
st.set_page_config(layout="wide")

# Initialize session state if not already done
if 'board' not in st.session_state:
    st.session_state.board = [
        ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
        ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
        [None] * 8,
        [None] * 8,
        [None] * 8,
        [None] * 8,
        ['β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™'],
        ['β™–', 'β™˜', 'β™—', 'β™•', 'β™”', 'β™—', 'β™˜', 'β™–']
    ]
if 'current_player' not in st.session_state:
    st.session_state.current_player = 'white'
if 'selected_piece' not in st.session_state:
    st.session_state.selected_piece = None

def handle_square_click(row, col):
    if st.session_state.selected_piece is None:
        # Select a piece
        if st.session_state.board[row][col] is not None:
            st.session_state.selected_piece = {'row': row, 'col': col}
            st.rerun()
    else:
        # Move the selected piece
        from_row = st.session_state.selected_piece['row']
        from_col = st.session_state.selected_piece['col']
        
        # Make the move
        st.session_state.board[row][col] = st.session_state.board[from_row][from_col]
        st.session_state.board[from_row][from_col] = None
        st.session_state.selected_piece = None
        st.session_state.current_player = 'black' if st.session_state.current_player == 'white' else 'white'
        st.rerun()

# Create buttons for each square using Streamlit's native components
st.write(f"## Chess Game")
st.write(f"Current Player: {'White' if st.session_state.current_player == 'white' else 'Black'}")

# Create the chessboard using a grid of columns
board_container = st.container()
with board_container:
    # CSS for the chess board
    st.markdown("""
        <style>
        .stButton > button {
            width: 100%;
            height: 0;
            padding-bottom: 100%;
            font-size: 3em;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: transparent;
            border: none;
        }
        .chess-square-white {
            background-color: #e8e8e8 !important;
        }
        .chess-square-black {
            background-color: #b0b0b0 !important;
        }
        .chess-square-selected {
            background-color: #ffd700 !important;
        }
        </style>
    """, unsafe_allow_html=True)

    # Create the board grid
    for row in range(8):
        cols = st.columns(8)
        for col in range(8):
            with cols[col]:
                piece = st.session_state.board[row][col] or ' '
                is_selected = (st.session_state.selected_piece and 
                             st.session_state.selected_piece['row'] == row and 
                             st.session_state.selected_piece['col'] == col)
                
                square_color = 'white' if (row + col) % 2 == 0 else 'black'
                if is_selected:
                    class_name = "chess-square-selected"
                else:
                    class_name = f"chess-square-{square_color}"
                
                # Create a button for each square
                if st.button(
                    piece,
                    key=f"square_{row}_{col}",
                    help=f"Row {row}, Col {col}",
                    type="secondary",
                    use_container_width=True
                ):
                    handle_square_click(row, col)

# Add a reset button
if st.button('Reset Game', key='reset'):
    st.session_state.board = [
        ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
        ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
        [None] * 8,
        [None] * 8,
        [None] * 8,
        [None] * 8,
        ['β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™'],
        ['β™–', 'β™˜', 'β™—', 'β™•', 'β™”', 'β™—', 'β™˜', 'β™–']
    ]
    st.session_state.current_player = 'white'
    st.session_state.selected_piece = None
    st.rerun()