Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
# Function 1: Load game assets
|
5 |
+
def load_game_assets():
|
6 |
+
x_image = np.array(Image.open('x.png'))
|
7 |
+
o_image = np.array(Image.open('o.png'))
|
8 |
+
blank_image = np.array(Image.open('blank.png'))
|
9 |
+
game_assets = {'X': x_image, 'O': o_image, 'BLANK': blank_image}
|
10 |
+
return game_assets
|
11 |
+
|
12 |
+
# Function 2: Render game board
|
13 |
+
def render_game_board(game_state, game_assets):
|
14 |
+
st.write('Current game state:')
|
15 |
+
for row in game_state:
|
16 |
+
for cell in row:
|
17 |
+
st.image(game_assets[cell], width=100)
|
18 |
+
|
19 |
+
# Function 3: Handle player input
|
20 |
+
def handle_player_input():
|
21 |
+
player_input = st.radio("Choose X or O", ('X', 'O'))
|
22 |
+
return player_input
|
23 |
+
|
24 |
+
# Function 4: Update game state
|
25 |
+
def update_game_state(game_state, player_input, row, col):
|
26 |
+
game_state[row][col] = player_input
|
27 |
+
return game_state
|
28 |
+
|
29 |
+
# Function 5: Apply game rules
|
30 |
+
def apply_game_rules(game_state, player_input):
|
31 |
+
# Check if player has won horizontally
|
32 |
+
for row in game_state:
|
33 |
+
if row.count(player_input) == 3:
|
34 |
+
st.write(f"Player {player_input} has won!")
|
35 |
+
return True
|
36 |
+
|
37 |
+
# Check if player has won vertically
|
38 |
+
for col in range(3):
|
39 |
+
if game_state[0][col] == player_input and game_state[1][col] == player_input and game_state[2][col] == player_input:
|
40 |
+
st.write(f"Player {player_input} has won!")
|
41 |
+
return True
|
42 |
+
|
43 |
+
# Check if player has won diagonally
|
44 |
+
if game_state[0][0] == player_input and game_state[1][1] == player_input and game_state[2][2] == player_input:
|
45 |
+
st.write(f"Player {player_input} has won!")
|
46 |
+
return True
|
47 |
+
elif game_state[0][2] == player_input and game_state[1][1] == player_input and game_state[2][0] == player_input:
|
48 |
+
st.write(f"Player {player_input} has won!")
|
49 |
+
return True
|
50 |
+
|
51 |
+
# Check if the game is tied
|
52 |
+
if all(all(cell != 'BLANK' for cell in row) for row in game_state):
|
53 |
+
st.write("The game is tied!")
|
54 |
+
return True
|
55 |
+
|
56 |
+
return False
|
57 |
+
|
58 |
+
# Function 6: Render game rules
|
59 |
+
def render_game_rules():
|
60 |
+
st.sidebar.markdown("""
|
61 |
+
### Tic Tac Toe Rules:
|
62 |
+
- The game is played on a 3x3 grid.
|
63 |
+
- Player 1 is X and Player 2 is O.
|
64 |
+
- Players take turns placing their marks in empty cells.
|
65 |
+
- The first player to get 3 of their marks in a row (horizontally, vertically, or diagonally) wins the game.
|
66 |
+
- If all 9 cells are full and no player has won, the game is a tie.
|
67 |
+
""")
|
68 |
+
|
69 |
+
# Main function
|
70 |
+
def main():
|
71 |
+
st.set_page_config(page_title='Tic Tac Toe', page_icon=':game_die:', layout='wide')
|
72 |
+
st.title('Tic Tac Toe')
|
73 |
+
|
74 |
+
# Load game assets
|
75 |
+
game_assets = load_game_assets()
|
76 |
+
|
77 |
+
# Initialize game state
|
78 |
+
game_state = [['BLANK', 'BLANK', 'BLANK'], ['BLANK', 'BLANK', 'BLANK'], ['BLANK', 'BLANK', 'BLANK']]
|
79 |
+
|
80 |
+
|