awacke1 commited on
Commit
784add3
·
1 Parent(s): a787a9e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import streamlit as st
3
+
4
+ # Define the game rules
5
+ NUM_ROUNDS = 26
6
+ CARD_VALUES = {
7
+ 'A': 14,
8
+ 'K': 13,
9
+ 'Q': 12,
10
+ 'J': 11,
11
+ '10': 10,
12
+ '9': 9,
13
+ '8': 8,
14
+ '7': 7,
15
+ '6': 6,
16
+ '5': 5,
17
+ '4': 4,
18
+ '3': 3,
19
+ '2': 2,
20
+ }
21
+
22
+ # Define the game mechanics
23
+ def shuffle_deck():
24
+ """Returns a shuffled deck of cards."""
25
+ deck = [(value, suit) for value in CARD_VALUES for suit in ['♠', '♡', '♢', '♣']]
26
+ random.shuffle(deck)
27
+ return deck
28
+
29
+ def draw_card(deck):
30
+ """Draws a card from the top of the deck and removes it from the deck."""
31
+ if len(deck) == 0:
32
+ return None
33
+ return deck.pop(0)
34
+
35
+ def compare_cards(card1, card2):
36
+ """Compares the values of two cards and returns the winner."""
37
+ value1 = CARD_VALUES[card1[0]]
38
+ value2 = CARD_VALUES[card2[0]]
39
+ if value1 > value2:
40
+ return 'player'
41
+ elif value2 > value1:
42
+ return 'ai'
43
+ else:
44
+ return 'tie'
45
+
46
+ def determine_winner(player_card, ai_card):
47
+ """Determines the winner of the round based on the values of the cards."""
48
+ if player_card is None:
49
+ return 'ai'
50
+ elif ai_card is None:
51
+ return 'player'
52
+ else:
53
+ return compare_cards(player_card, ai_card)
54
+
55
+ # Define the game UI
56
+ def game_ui():
57
+ """Displays the game UI and updates the game state."""
58
+ player_cards = game_state['player_cards']
59
+ ai_cards = game_state['ai_cards']
60
+ player_card = player_cards[-1] if len(player_cards) > 0 else None
61
+ ai_card = ai_cards[-1] if len(ai_cards) > 0 else None
62
+
63
+ st.write('# Peace and Love')
64
+ st.write('---')
65
+
66
+ st.write('**Player**')
67
+ st.write('Cards: ', ' '.join([f"{card[0]}{card[1]}" for card in player_cards]))
68
+ st.write('Score: ', game_state['player_score'])
69
+ st.write('---')
70
+
71
+ st.write('**Dealer**')
72
+ st.write('Cards: ', ' '.join([f"🂠" if len(ai_cards) == 1 else f"{card[0]}{card[1]}" for card in ai_cards]))
73
+ st.write('Score: ', game_state['ai_score'])
74
+ st.write('---')
75
+
76
+ if st.button('Draw'):
77
+ st.write('---')
78
+ st.write('You drew:', f"{player_card[0]}{player_card[1]}")
79
+ st.write('Dealer drew:', f"🂠" if len(ai_cards) == 1 else f"{ai_card[0]}{ai_card[1]}")
80
+ winner = determine_winner(player_card, ai_card)
81
+ if winner == 'player':
82
+ st.write('You won this round!')
83
+ game_state['player_cards'].extend([player_card, ai_card])
84
+ game_state['player_score'] += 2
85
+ elif winner == 'ai':
86
+ st.write('Dealer won this round!')
87
+ game_state['ai_cards'].extend([player_card, ai_card])
88
+ game_state['ai_score'] += 2
89
+ else:
90
+ st.write('Tie!')
91
+ game_state['player_cards'].append(player_card)
92
+ game_state['ai_cards'].append(ai_card)
93
+
94
+ game_state['rounds_played'] += 1
95
+
96
+ st.sidebar.write('---')
97
+ if st.sidebar.button('New Game'):
98
+ # Reset game state
99
+ game_state['player_cards'] = []
100
+ game_state['ai_cards'] = []
101
+ game_state['player_score'] = 0
102
+ game_state['ai_score'] = 0
103
+ game_state['rounds_played'] = 0
104
+ deck = shuffle_deck()
105
+ game_state['player_cards'] = deck[:26]
106
+ game_state['ai_cards'] = deck[26:]
107
+
108
+ if st.sidebar.button('Save'):
109
+ with open('game_state.txt', 'w') as f:
110
+ f.write(str(game_state))
111
+
112
+ if st.sidebar.button('Reload'):
113
+ with open('game_state.txt', 'r') as f:
114
+ game_state = eval(f.read())
115
+
116
+ # Define the game state and load from file if available
117
+ game_state = {
118
+ 'player_cards': [],
119
+ 'ai_cards': [],
120
+ 'player_score': 0,
121
+ 'ai_score': 0,
122
+ 'rounds_played': 0,
123
+ }
124
+ if st.sidebar.button('Reload'):
125
+ with open('game_state.txt', 'r') as f:
126
+ game_state = eval(f.read())
127
+
128
+ # Play the game
129
+ if len(game_state['player_cards']) == 0 and len(game_state['ai_cards']) == 0:
130
+ deck = shuffle_deck()
131
+ game_state['player_cards'] = deck[:26]
132
+ game_state['ai_cards'] = deck[26:]
133
+
134
+ while game_state['rounds_played'] < NUM_ROUNDS and len(game_state['player_cards']) + len(game_state['ai_cards']) == 52:
135
+ game_ui()
136
+
137
+ if st.sidebar.button('Save'):
138
+ with open('game_state.txt', 'w') as f:
139
+ f.write(str(game_state))
140
+
141
+ player_card = draw_card(game_state['player_cards'])
142
+ ai_card = draw_card(game_state['ai_cards'])
143
+ winner = determine_winner(player_card, ai_card)
144
+
145
+ if winner == 'player':
146
+ game_state['player_cards'].extend([player_card, ai_card])
147
+ game_state['player_score'] += 2
148
+ elif winner == 'ai':
149
+ game_state['ai_cards'].extend([player_card, ai_card])
150
+ game_state['ai_score'] += 2
151
+ else:
152
+ game_state['player_cards'].append(player_card)
153
+ game_state['ai_cards'].append(ai_card)
154
+
155
+ game_state['rounds_played'] += 1