Spaces:
Runtime error
Runtime error
import os | |
import random | |
import streamlit as st | |
# Define the game rules | |
NUM_ROUNDS = 26 | |
CARD_VALUES = { | |
'A': 14, | |
'K': 13, | |
'Q': 12, | |
'J': 11, | |
'10': 10, | |
'9': 9, | |
'8': 8, | |
'7': 7, | |
'6': 6, | |
'5': 5, | |
'4': 4, | |
'3': 3, | |
'2': 2, | |
} | |
# Define the game mechanics | |
def shuffle_deck(): | |
"""Returns a shuffled deck of cards.""" | |
deck = [(value, suit) for value in CARD_VALUES for suit in ['β ', 'β‘', 'β’', 'β£']] | |
random.shuffle(deck) | |
return deck | |
def draw_card(deck): | |
"""Draws a card from the top of the deck and removes it from the deck.""" | |
if len(deck) == 0: | |
return None | |
return deck.pop(0) | |
def compare_cards(card1, card2): | |
"""Compares the values of two cards and returns the winner.""" | |
value1 = CARD_VALUES[card1[0]] | |
value2 = CARD_VALUES[card2[0]] | |
if value1 > value2: | |
return 'player' | |
elif value2 > value1: | |
return 'ai' | |
else: | |
return 'tie' | |
def determine_winner(player_card, ai_card): | |
"""Determines the winner of the round based on the values of the cards.""" | |
if player_card is None: | |
return 'ai' | |
elif ai_card is None: | |
return 'player' | |
else: | |
return compare_cards(player_card, ai_card) | |
# Define the game UI | |
def game_ui(): | |
"""Displays the game UI and updates the game state.""" | |
player_cards = game_state['player_cards'] | |
ai_cards = game_state['ai_cards'] | |
player_card = player_cards[-1] if len(player_cards) > 0 else None | |
ai_card = ai_cards[-1] if len(ai_cards) > 0 else None | |
st.write('# Peace and Love') | |
st.write('---') | |
st.write('**Player**') | |
st.write('Cards: ', ' '.join([f"{card[0]}{card[1]}" for card in player_cards])) | |
st.write('Score: ', game_state['player_score']) | |
st.write('---') | |
st.write('**Dealer**') | |
st.write('Cards: ', ' '.join([f"π " if len(ai_cards) == 1 else f"{card[0]}{card[1]}" for card in ai_cards])) | |
st.write('Score: ', game_state['ai_score']) | |
st.write('---') | |
if st.button('Draw'): | |
st.write('---') | |
st.write('You drew:', f"{player_card[0]}{player_card[1]}") | |
st.write('Dealer drew:', f"π " if len(ai_cards) == 1 else f"{ai_card[0]}{ai_card[1]}") | |
winner = determine_winner(player_card, ai_card) | |
if winner == 'player': | |
st.write('You won this round!') | |
game_state['player_cards'].extend([player_card, ai_card]) | |
game_state['player_score'] += 2 | |
elif winner == 'ai': | |
st.write('Dealer won this round!') | |
game_state['ai_cards'].extend([player_card, ai_card]) | |
game_state['ai_score'] += 2 | |
else: | |
st.write('Tie!') | |
game_state['player_cards'].append(player_card) | |
game_state['ai_cards'].append(ai_card) | |
game_state['rounds_played'] += 1 | |
# Save game state to file | |
with open('game_state.txt', 'w') as f: | |
if not os.path.exists('game_state.txt'): | |
f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n') | |
f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n') | |
st.sidebar.write('---') | |
if st.sidebar.button('New Game'): | |
# Reset game state | |
game_state['player_cards'] = [] | |
game_state['ai_cards'] = [] | |
game_state['player_score'] = 0 | |
game_state['ai_score'] = 0 | |
game_state['rounds_played'] = 0 | |
deck = shuffle_deck() | |
game_state['player_cards'] = deck[:26] | |
game_state['ai_cards'] = deck[26:] | |
# Save game state to file | |
with open('game_state.txt', 'w') as f: | |
f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n') | |
f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n') | |
if st.sidebar.button('Save'): | |
# Save game state to file | |
with open('game_state.txt', 'w') as f: | |
if not os.path.exists('game_state.txt'): | |
f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n') | |
f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n') | |
if st.sidebar.button('Reload'): | |
# Reload game state from file | |
game_state = {'player_cards': [], 'ai_cards': [], 'player_score': 0, 'ai_score': 0, 'rounds_played': 0} | |
with open('game_state.txt', 'r') as f: | |
headers = f.readline().strip().split(',') | |
data = f.readlines() | |
if len(data) > 0: | |
last_line = data[-1].strip().split(',') | |
for i in range(len(headers)): | |
game_state[headers[i]] = eval(last_line[i]) | |
# Show game history | |
st.write('# Game History') | |
if not st.checkbox('Show game history'): | |
return | |
with open('game_state.txt', 'r') as f: | |
lines = f.readlines() | |
headers = [header.strip() for header in lines[0].strip().split(',')] | |
data = [[cell.strip() for cell in line.strip().split(',')] for line in lines[1:]] | |
st.write(st.dataframe(data, columns=headers)) | |
# Play the game | |
game_state = {'player_cards': [], 'ai_cards': [], 'player_score': 0, 'ai_score': 0, 'rounds_played': 0} | |
while game_state['rounds_played'] < NUM_ROUNDS and len(game_state['player_cards']) + len(game_state['ai_cards']) == 52: | |
game_ui() | |
player_card = draw_card(game_state['player_cards']) | |
ai_card = draw_card(game_state['ai_cards']) | |
winner = determine_winner(player_card, ai_card) | |
if winner == 'player': | |
game_state['player_cards'].extend([player_card, ai_card]) | |
game_state['player_score'] += 2 | |
elif winner == ' | |