Spaces:
Runtime error
Runtime error
import streamlit as st | |
import numpy as np | |
# Function 1: Load game assets | |
def load_game_assets(): | |
x_image = np.array(Image.open('Aaron_Wacker_2D_RPG_topdown_tilesets_09f726d6-dd76-4462-b5c4-89f608db6995.png')) | |
o_image = np.array(Image.open('Aaron_Wacker_2D_RPG_topdown_tilesets_12cd3a05-f8e7-4369-aea5-35e50d8cc09f.png')) | |
blank_image = np.array(Image.open('Aaron_Wacker_2D_RPG_topdown_tilesets_f40f2d4e-8309-4e16-a0bb-25e64d9e39d2.png')) | |
game_assets = {'X': x_image, 'O': o_image, 'BLANK': blank_image} | |
return game_assets | |
# Function 2: Render game board | |
def render_game_board(game_state, game_assets): | |
st.write('Current game state:') | |
for row in game_state: | |
for cell in row: | |
st.image(game_assets[cell], width=100) | |
# Function 3: Handle player input | |
def handle_player_input(): | |
player_input = st.radio("Choose X or O", ('X', 'O')) | |
return player_input | |
# Function 4: Update game state | |
def update_game_state(game_state, player_input, row, col): | |
game_state[row][col] = player_input | |
return game_state | |
# Function 5: Apply game rules | |
def apply_game_rules(game_state, player_input): | |
# Check if player has won horizontally | |
for row in game_state: | |
if row.count(player_input) == 3: | |
st.write(f"Player {player_input} has won!") | |
return True | |
# Check if player has won vertically | |
for col in range(3): | |
if game_state[0][col] == player_input and game_state[1][col] == player_input and game_state[2][col] == player_input: | |
st.write(f"Player {player_input} has won!") | |
return True | |
# Check if player has won diagonally | |
if game_state[0][0] == player_input and game_state[1][1] == player_input and game_state[2][2] == player_input: | |
st.write(f"Player {player_input} has won!") | |
return True | |
elif game_state[0][2] == player_input and game_state[1][1] == player_input and game_state[2][0] == player_input: | |
st.write(f"Player {player_input} has won!") | |
return True | |
# Check if the game is tied | |
if all(all(cell != 'BLANK' for cell in row) for row in game_state): | |
st.write("The game is tied!") | |
return True | |
return False | |
# Function 6: Render game rules | |
def render_game_rules(): | |
st.sidebar.markdown(""" | |
### Tic Tac Toe Rules: | |
- The game is played on a 3x3 grid. | |
- Player 1 is X and Player 2 is O. | |
- Players take turns placing their marks in empty cells. | |
- The first player to get 3 of their marks in a row (horizontally, vertically, or diagonally) wins the game. | |
- If all 9 cells are full and no player has won, the game is a tie. | |
""") | |
# Main function | |
def main(): | |
st.set_page_config(page_title='Tic Tac Toe', page_icon=':game_die:', layout='wide') | |
st.title('Tic Tac Toe') | |
# Load game assets | |
game_assets = load_game_assets() | |
# Initialize game state | |
game_state = [['BLANK', 'BLANK', 'BLANK'], ['BLANK', 'BLANK', 'BLANK'], ['BLANK', 'BLANK', 'BLANK']] | |