Spaces:
Sleeping
Sleeping
File size: 1,729 Bytes
df89ba5 6d77e7d df89ba5 6d77e7d df89ba5 6d77e7d df89ba5 6d77e7d df89ba5 6d77e7d df89ba5 6d77e7d df89ba5 6d77e7d |
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 |
import streamlit as st
st.set_page_config(page_title="Tic Tac Toe", page_icon="โ")
st.title("๐ฎ Tic Tac Toe Game")
st.markdown("Play against a friend! Take turns to mark X and O.")
# Initialize game state
if "board" not in st.session_state:
st.session_state.board = [""] * 9
st.session_state.current_player = "X"
st.session_state.winner = None
# Function to check winner
def check_winner(board):
wins = [(0,1,2), (3,4,5), (6,7,8), # rows
(0,3,6), (1,4,7), (2,5,8), # cols
(0,4,8), (2,4,6)] # diagonals
for i,j,k in wins:
if board[i] == board[j] == board[k] != "":
return board[i]
if "" not in board:
return "Draw"
return None
# Handle button clicks
def make_move(i):
if st.session_state.board[i] == "" and st.session_state.winner is None:
st.session_state.board[i] = st.session_state.current_player
st.session_state.winner = check_winner(st.session_state.board)
if st.session_state.winner is None:
st.session_state.current_player = "O" if st.session_state.current_player == "X" else "X"
# Display the board
cols = st.columns(3)
for i in range(9):
with cols[i % 3]:
if st.button(st.session_state.board[i] or " ", key=i):
make_move(i)
# Show game status
if st.session_state.winner:
if st.session_state.winner == "Draw":
st.info("It's a draw!")
else:
st.success(f"๐ Player {st.session_state.winner} wins!")
if st.button("Play Again"):
st.session_state.board = [""] * 9
st.session_state.current_player = "X"
st.session_state.winner = None
else:
st.write(f"Current Turn: **{st.session_state.current_player}**")
|