-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardgame.py
290 lines (245 loc) ยท 9.67 KB
/
cardgame.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import os
from time import sleep
from random import shuffle, choice
PLAYER_MONEY = 100
class Player:
def __init__(self, name='Bot'):
self.money = PLAYER_MONEY
self.name = name
self.ai = self.name == 'Bot'
self.score = 0
self.cards = []
def print_cards(self):
if self.ai:
print("Bot's cards: {} ({} points)".
format(' '.join(card.pic for card in self.cards), self.score)
)
else:
print('Your cards: {} ({} points)'.
format(' '.join(card.pic for card in self.cards), self.score)
)
def check_score(self):
self.score = sum([card.value for card in self.cards])
def take_turn(self):
if self.ai:
print('More? (y/n): ', end='', flush=True)
for i in range(3):
sleep(0.5)
print('.', end='', flush=True)
if self.score <= 11:
weight = [(True, 1)]
elif 11 < self.score <= 15:
weight = [(True, 50), (False, 50)]
elif 15 < self.score <= 19:
weight = [(True, 20), (False, 80)]
else:
weight = [(False, 1)]
res = choice([val for val, cnt in weight for _ in range(cnt)])
if res:
print(' y')
sleep(0.5)
else:
print(' n')
sleep(0.5)
else:
ans = input('More? (y/n): ').lower()
if ans == 'y':
res = True
elif ans == 'n':
res = False
else:
print('Incorrect choice.')
return self.take_turn()
return res
def __repr__(self):
return '{}: {} ({} points)'.format(self.name,
' '.join(card.pic for card
in self.cards),
self.score)
class Card:
def __init__(self, value, suit):
self.value = value[1]
self.name = value[0]
self.suit = suit
self.pic = value[2][suit]
def __repr__(self):
return "<Card: {} of {} {}>".format(self.name, self.suit, self.pic)
class Game:
"""
Aim of the โOCHKOโ game is to get a total of 21 points from your cards.
Cards/Points:
Ace 1 or 11 (the player who holds the Ace gets to choose the value
of the card).
Jack 2
Queen 3
King 4
Numbered cards have respective numeric values (6 through 10).
Rules:
1. Player should make a bet from 5 to 25.
2. Player wins as much as they bet. If player bets 10, they win 10 from the
'Bot' (keeping their original bet).
* If a player gets OCHKO (10 + Ace or 2 Aces) from the initial draw,
they win right away and get 2x their bet.
3. Player loses as much as they bet.
4. If the hand is a draw, player keeps the bet neither winning nor losing
money.
5. Two cards are dealt to each player.
6. Player can keep their hand as it is.
7. If a player doesn't have 21 points, they may take additional cards.
8. The winner is the one who gets the most points, or has 21 points.
9. If the total score is more than 21 points - player loses.
"""
def __init__(self, player_name):
self._cards_per_player = 2
self.players = [Player(player_name), Player()]
self.player = self.players[0]
self.ai = self.players[1]
self._card_suits = ['spades', 'hearts', 'diamonds', 'clubs']
self._card_values = [('6', 6, {'spades': '๐ฆ',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐ถ'}),
('7', 7, {'spades': '๐ง',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐ท'}),
('8', 8, {'spades': '๐จ',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐ธ'}),
('9', 9, {'spades': '๐ฉ',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐น'}),
('10', 10, {'spades': '๐ช',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐บ'}),
('Jack', 2, {'spades': '๐ซ',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐ป'}),
('Queen', 3, {'spades': '๐ญ',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐ฝ'}),
('King', 4, {'spades': '๐ฎ',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐พ'}),
('Ace', 11, {'spades': '๐ก',
'clubs': '๐',
'diamonds': '๐',
'hearts': '๐ฑ'}),
]
self.deck = []
def _create_deck(self):
return [Card(value, suit) for value in self._card_values
for suit in self._card_suits]
def distribute_cards(self):
for player in self.players:
for i in range(self._cards_per_player):
player.cards.append(self._get_random_card())
player.check_score()
def _get_random_card(self):
if self.deck:
shuffle(self.deck)
return self.deck.pop()
return None
def add_card(self, player):
card = self._get_random_card()
if card.name == 'Ace' and player.score + card.value > 21:
card.value = 1
player.cards.append(card)
player.check_score()
if player.score > 21:
for card in player.cards:
if card.value == 11:
player.score -= 10
break
def enter_bet(self):
bet = input('Make a bet (5 to 25): ')
try:
bet = int(bet)
except ValueError:
print('Your bet should be an integer, try again.')
return self.enter_bet()
else:
if bet < 5 or bet > 25:
print('Your bet should be from 5 to 25, try again.')
return self.enter_bet()
if bet > self.player.money:
print("You don't have enough money, your max bet is {}.".
format(self.player.money))
return self.enter_bet()
return bet
def reset_cards(self):
for player in self.players:
player.score = 0
player.cards = []
self.deck = self._create_deck()
self.distribute_cards()
def win(self):
ai.win = self.ai.score <= 21
player.win = self.player.score <= 21
if player.win and self.player.score > self.ai.score:
return True
elif (not player.win or (player.win and
self.player.score < self.ai.score)):
return False
else:
return None
def new_round(self):
self.reset_cards()
bet = self.enter_bet()
if self.player.score in [21, 22]:
self.player.print_cards()
print('OCHKO! {} added to your balance.'.format(bet * 2))
self.player.money += bet * 2
else:
for player in self.players:
player.print_cards()
while player.take_turn():
self.add_card(player)
player.print_cards()
if player.score > 21:
print('Too much :(')
break
elif player.score == 21:
print('OCHKO!')
break
print()
res = self.win()
if res:
print('You win! {} added to your balance.'.format(bet))
self.player.money += bet
elif res is None:
print("It's a draw. You keep the money.")
else:
print('You lose! {} removed from your balance.'.format(bet))
self.player.money -= bet
print('Your balance is {}.'.format(self.player.money))
if self.player.money >= 5:
ans = ''
while ans not in ['y', 'n']:
ans = input('\nAnother round? (y/n): ').lower()
if ans == 'y':
os.system('clear')
return self.new_round()
else:
print('You lost all your money.')
def play(self):
print("Welcome to OCHKO, {}. Let's play!".format(self.player.name))
print(self.__doc__)
input('Press Enter to continue...')
os.system('clear')
print('Your balance is {}.'.format(self.player.money))
self.new_round()
print('Bye-bye.\n')
def main():
os.system('clear')
name = input('Enter your name: ')
game = Game(name)
game.play()
if __name__ == '__main__':
main()