-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjackModule.py
More file actions
275 lines (233 loc) · 11.1 KB
/
blackjackModule.py
File metadata and controls
275 lines (233 loc) · 11.1 KB
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
import random
# ██████ ██ █████ ██ ██ ███████ ██████ ██████ ██ █████ ███████ ███████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██ ███████ ████ █████ ██████ ██ ██ ███████ ███████ ███████ █████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ███████ ██ ██ ██ ███████ ██ ██ ██████ ███████ ██ ██ ███████ ███████ ███████ ███████
class gamePlayer:
def __init__(self):
self.hand = []
self.money = 500
self.bet = 0
self.gameInfo = {
"gamesPlayed": 0,
"gamesLost": 0,
"gamesWon": 0,
}
def playerWon(self):
self.gameInfo["gamesWon"]+=1
self.gameInfo["gamesPlayed"]+=1
def playerLost(self):
self.gameInfo["gamesLost"]+=1
self.gameInfo["gamesPlayed"]+=1
# ------------------------------------------------------------------------------------------------------
# ██████ █████ ███ ███ ███████ ██████ ██ █████ ███████ ███████
# ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██
# ██ ███ ███████ ██ ████ ██ █████ ██ ██ ███████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██ ██ ██ ██ ███████ ██████ ███████ ██ ██ ███████ ███████
class blackjackGame:
def __init__(self):
self.player = gamePlayer()
self.loadGame()
self.restartTheGame()
def restartTheGame(self):
"""Раздать всю колоду заново"""
self.player.hand = []
self.dealer_hand = []
self.deckOCards = cardDeck()
self.deckOCards.shuffleCards()
self.pullACard(self.player)
self.pullACard(self.player)
self.pullCardToDealer()
self.pullCardToDealer()
if self.checkHand(self.player.hand)[0] != "continue" or self.checkHand(self.dealer_hand)[0] != "continue":
self.restartTheGame()
def saveGame(self):
"""Сохранить данные сохранения в файл gameSave.json"""
import json
save_data = {
"money": self.player.money,
"gameInfo": self.player.gameInfo,
}
try:
with open("gameSave.json", "w") as file:
json.dump(save_data, file, indent=4)
return True, "Game saved successfully!"
except Exception as e:
return False, f"Error saving game: {e}"
def loadGame(self):
"""Подгрузить данные сохранения из файла gameSave.json"""
import json
import os
if not os.path.exists("gameSave.json"):
return False, "No save file found!"
try:
with open("gameSave.json", "r") as file:
save_data = json.load(file)
self.player.money = save_data.get("money", 500)
self.player.gameInfo = save_data.get("gameInfo", {
"gamesPlayed": 0,
"gamesLost": 0,
"gamesWon": 0,
})
return True, f"Game loaded!\nMoney: ${self.player.money}"
except Exception as e:
return False, f"Error loading game: {e}"
def checkHand(self, hand):
"""Универсальная проверка руки - возвращает статус любой руки"""
total = self.countHand(hand)
if total > 21:
return "busted", total
elif total == 21 and len(hand) == 2:
return "blackjack", total
elif total == 21:
return "twenty_one", total
else:
return "continue", total
def pullACard(self, player):
"""Достаёт карту из колоды и рассказывает если игрок прогорел"""
card = self.deckOCards.cards.pop()
player.hand.append(card)
if self.countHand(player.hand) > 21:
return "busted"
return "continue"
def endGame(self, changePlayer=True):
"""Оканчивает игру и рассказывает кто выиграл или проиграл"""
dealerTotal = self.checkHand(self.dealer_hand)
playerTotal = self.checkHand(self.player.hand)
if playerTotal[0] == "busted":
if changePlayer:
self.player.playerLost()
return "player_busted", "The player busted"
elif dealerTotal[1] > playerTotal[1]:
if changePlayer:
self.player.playerLost()
return "dealer_wins", "Dealer Won!"
elif dealerTotal[0] == "busted":
if changePlayer:
self.player.playerWon()
self.player.money += self.player.bet * 2
return "dealer_busted", "The dealer busted"
elif playerTotal[1] > dealerTotal[1]:
if changePlayer:
self.player.playerWon()
self.player.money += self.player.bet * 2
return "player_wins", "Player won!"
else:
if changePlayer:
self.player.gameInfo["gamesPlayed"] += 1
self.player.money += self.player.bet
return "tie", "Tie"
def pullCardToDealer(self):
"""Взять карту из колоды и добавить в руку дилера"""
card = self.deckOCards.cards.pop()
self.dealer_hand.append(card)
return card
def countHand(self, hand, isDealer=False):
"""Подсчитать общую стоимость руки"""
total = 0
aces = 0
if len(hand) == 2 and isDealer:
card = hand[1]
if card[0]["fakeValue"] == "A":
return 11
return card[1]
for card in hand:
if card[0]["fakeValue"] == "A":
aces += 1
total += 11
else:
total += card[1]
while total > 21 and aces > 0:
total -= 10
aces -= 1
return total
def hit(self):
"""Игрок бьет - берет еще одну карту"""
self.pullACard(self.player)
if self.checkHand(self.player.hand)[0] == "busted":
return "busted"
return "continue"
def dealerTurn(self):
"""Дилер играет"""
while self.countHand(self.dealer_hand) < 17:
self.pullCardToDealer()
return self.endGame()
def canPlayerAffordBet(self, amount):
"""Проверить, достаточно ли у игрока денег для ставки"""
return self.player.money >= amount
def placeBet(self, amount):
"""Поставить ставку, если у игрока достаточно денег"""
if self.canPlayerAffordBet(amount):
self.player.bet = amount
self.player.money -= amount
return True
return False
# ------------------------------------------------------------------------------------------------------
# ██████ █████ ██████ ██████ ███████ ██████ ██ █████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ███████ ██████ ██ ██ ███████ ██ ██ ███████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██ ██ ██ ██ ██████ ███████ ██████ ███████ ██ ██ ███████ ███████
class cardDeck:
def __init__(self):
self.cards = []
suits = ['h', 'd', 'c', 's'] # Hearts, Diamonds, Clubs, Spades
possibleFakeValues = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
for suit in suits:
for value in range(1, 14):
if value == 1:
card_value = 1
else:
card_value = min(value, 10)
self.cards.append([
{
"suit": suit,
"fakeValue": possibleFakeValues[value-1]
},
card_value
])
def shuffleCards(self):
random.shuffle(self.cards)
def takeACard(self):
return self.cards.pop()
def printAHand(self,hand,isDealer=False):
visualHand = ["","","","","","","","",""]
for i, x in enumerate(hand):
if isDealer and i == 0:
currentCard = self.getCardSuite(x,True)
else:
currentCard = self.getCardSuite(x)
tempCardArray = currentCard.split("\n")
counter = 0
for y in tempCardArray:
visualHand[counter] += y+" "
counter+=1
return "\n".join(visualHand)
def getCardSuite(self, card, shouldHideCard=False):
emblem = ">:D"
if shouldHideCard:
asciiSuit = "XX"
rank = "XX"
else:
suit = card[0]["suit"]
rank = card[0]["fakeValue"]
match suit:
case "h":
asciiSuit = "♥"
case "s":
asciiSuit = "♠"
case "c":
asciiSuit = "♣"
case "d":
asciiSuit = "♦"
return f"""._________.
|{asciiSuit+" "+str(rank):<9}|
| |
| |
|{emblem:^9}|
| |
| |
|{str(rank)+" "+asciiSuit:>9}|
˙‾‾‾‾‾‾‾‾‾˙"""