-
Notifications
You must be signed in to change notification settings - Fork 0
/
black_jack new
153 lines (123 loc) · 4.1 KB
/
black_jack new
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
import random
class Dealer:
def __init__(self):
self.deck = [
'♥A', '♥7', '♥8', '♥9', '♥10', '♥J', '♥Q', '♥K',
'♦A', '♦7', '♦8', '♦9', '♦10', '♦J', '♦Q', '♦K',
'♣A', '♣7', '♣8', '♣9', '♣10', '♣J', '♣Q', '♣K',
'♠A', '♠7', '♠8', '♠9', '♠10', '♠J', '♠Q', '♠K',
]
self.players = []
def shuffle(self):
# deck = self.deck.copy()
random.shuffle(self.deck)
# return deck
def deal(self, n):
arm = []
deck = self.deck
# print(deck)
if len(deck) == 0:
return []
else:
for num in range(n):
if len(deck) == 0:
break
num = deck.pop(0)
arm.append(num)
return arm
def addPlayer(self, player):
self.players.append(player)
def removePlayer(self, player):
self.players.remove(player)
def startRound(self):
players = self.players.copy()
while len(players) != 0:
delete = []
print('--- rozdavam ---')
for player in players:
if len(player.hand) == 0:
card = self.deal(1)
player.hand += card
print(player.name, ' ma nyni karty ', player.hand, ' v hodnote ', player.getHandValue())
else:
res = player.needsCard()
if res == True:
card_new = self.deal(1)
player.hand += card_new
if player.getHandValue() > 21:
delete.append(player)
print(player.name, ' ma nyni karty ', player.hand, ' v hodnote ', player.getHandValue())
else:
delete.append(player)
if len(players) == 1:
print('---', player.name, ' nechce ---')
for i in delete:
players.remove(i)
print('--- kolo skoncilo ---')
print(self.announceWinner())
def announceWinner(self):
winner = 0
for i in self.players:
print(i.name, str(i.getHandValue()))
class Player:
def __init__(self, name, strategy):
self.name = name
self.strategy = strategy
self.hand = []
def getHandValue(self):
points = 0
for card in self.hand:
if card[1] == 'J' or card[1] == 'K' or card[1] == 'Q':
points += 1
elif card[1] == '7':
points += 7
elif card[1] == '8':
points += 8
elif card[1] == '9':
points += 9
elif card[1] == '1':
points += 10
elif card[1] == 'A':
points += 11
return points
def acceptCard(self, cards):
self.hand += cards
print(self.hand)
def needsCard(self):
if self.strategy == 'Cautious':
if self.getHandValue() >= 10:
return False
else:
return True
elif self.strategy == 'Bold':
if self.getHandValue() >= 15:
return False
else:
return True
elif self.strategy == 'Human':
res = input(self.name + ' ma nyni karty: ' + str(self.hand) + ' v hodnote ' +
str(self.getHandValue()) + ', chce dalsi(A/N)?: ')
if res == 'A':
return True
elif res == 'N':
return False
# TEST DEAL
# dealer = Dealer()
# dealer.shuffle()
# myHand = dealer.deal(31)
# print(myHand)
# dealer.shuffle()
# myHand = dealer.deal(3)
# print(myHand)
# TEST GAME
newDealer = Dealer()
player1 = Player('Čeněk Člověčí', 'Human')
player2 = Player('Vilda Vopatrný', 'Cautious')
player3 = Player('Olda Odvážný', 'Bold')
player4 = Player('Ofgot', 'Cautious')
newDealer.addPlayer(player1)
newDealer.addPlayer(player2)
newDealer.addPlayer(player3)
newDealer.addPlayer(player4)
newDealer.shuffle()
newDealer.startRound()