This repository was archived by the owner on Aug 17, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathblackjack.py
More file actions
442 lines (353 loc) · 19.8 KB
/
Copy pathblackjack.py
File metadata and controls
442 lines (353 loc) · 19.8 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from blackjackModule import blackjackGame
from textual.app import App
from textual import on
from textual.widgets import Static, Button, Header, Input
from textual.theme import Theme
from textual.screen import Screen
from textual.containers import Horizontal, Container
from textual.reactive import reactive
pokerTheme = Theme(name="poker", primary="#FFD700", secondary="#228B22", accent="#DC143C", foreground="#FFFFFF", background="#0F5132", success="#32CD32", warning="#FFA500", error="#FF0000", surface="#1B5E20", panel="#2E7D32", dark=True,)
game = blackjackGame()
# ███████ ███ ██ ██████ ███████ ██████ ██████ ███████ ███████ ███ ██
# ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
# █████ ██ ██ ██ ██ ██ ███████ ██ ██████ █████ █████ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ███████ ██ ████ ██████ ███████ ██████ ██ ██ ███████ ███████ ██ ████
class EndScreen(Screen):
CSS_PATH = "tcss/EndScreen.tcss"
def compose(self):
yield Header()
yield CustomFooter()
with Container(classes="endingContainer", id="endingContainer"):
yield EndingScreenTitle(classes="endingTextContainer")
yield PlayerCards()
# revealHidden=True: the round is over, so flip the dealer's hole card
yield DealerCards(revealHidden=True)
yield Button("Play Again!", id="playAgainButton")
yield Button("Main Menu", id="backToMainMenu")
def on_mount(self):
self.sub_title = "Game Over!"
self.query_one("#endingContainer").border_title = "Game Over"
@on(Button.Pressed, "#playAgainButton")
def playAgain(self):
game.restartTheGame()
mainApp.switch_screen(BetScreen())
@on(Button.Pressed, "#backToMainMenu")
def backToMainMenu(self):
game.restartTheGame()
mainApp.pop_screen()
class EndingScreenTitle(Static):
endingTitle = reactive(game.endGame(changePlayer=False)[1])
def compose(self):
with Horizontal(id="hand-container"):
self.stats_display = Static(self.cardsText(), classes="endingText")
yield self.stats_display
def cardsText(self):
return f"{game.endGame(changePlayer=False)[1]}"
def on_mount(self):
self.set_interval(0.1, self.refresh_stats)
self.refresh_stats()
def refresh_stats(self):
"""Refresh stats from game object"""
old_endingTitle = self.endingTitle
new_endingTitle = game.endGame(changePlayer=False)[1]
if (old_endingTitle != new_endingTitle):
self.endingTitle = new_endingTitle
def watch_endingTitle(self):
self.update_stats_display()
def update_stats_display(self):
if hasattr(self, 'stats_display'):
self.stats_display.update(self.cardsText())
# ██████ █████ ███ ███ ███████ ███████ ██████ ██████ ███████ ███████ ███ ██
# ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ████ ██
# ██ ███ ███████ ██ ████ ██ █████ ███████ ██ ██████ █████ █████ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██ ██ ██ ██ ███████ ███████ ██████ ██ ██ ███████ ███████ ██ ████
class GameScreen(Screen):
CSS_PATH = "tcss/GameScreen.tcss"
def compose(self):
yield Header()
yield CustomFooter()
yield PlayerCards(classes="cardsDisplay")
yield DealerCards(classes="cardsDisplay")
yield Button("Hit", id="hitButton")
yield Button("Stand", id="standButton")
yield Static()
yield Static()
def on_mount(self):
self.sub_title = "In game"
@on(Button.Pressed, "#hitButton")
def hitACard(self):
if game.hit() == "busted":
# Settle the round (deduct the loss / log the stat) the same
# way dealerTurn() does on Stand, THEN switch screens.
game.endGame()
mainApp.switch_screen(EndScreen())
@on(Button.Pressed, "#standButton")
def stand(self):
game.dealerTurn()
mainApp.switch_screen(EndScreen())
# ██████ ██ █████ ██ ██ ███████ ██████ ██████ █████ ██████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██ ███████ ████ █████ ██████ ██ ███████ ██████ ██ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ███████ ██ ██ ██ ███████ ██ ██ ██████ ██ ██ ██ ██ ██████ ███████
class PlayerCards(Static):
playerHand = reactive(game.player.hand)
playerCards = reactive(game.deckOCards.printAHand(game.player.hand, False))
def compose(self):
with Horizontal(id="hand-container"):
self.stats_display = Static(self.cardsText(), classes="playerCardsText")
yield self.stats_display
def cardsText(self):
return f"Player's Hand: [{game.countHand(game.player.hand,False)}]\n{self.playerCards}"
def on_mount(self):
self.set_interval(0.1, self.refresh_stats)
self.refresh_stats()
def refresh_stats(self):
"""Refresh stats from game object"""
old_hand = self.playerHand
old_cards = self.playerCards
new_hand = game.player.hand
new_cards = game.deckOCards.printAHand(game.player.hand, False)
if (old_hand != new_hand or
old_cards != new_cards):
self.playerHand = new_hand
self.playerCards = new_cards
def watch_playerHand(self):
self.update_stats_display()
def watch_playerCards(self):
self.update_stats_display()
def update_stats_display(self):
if hasattr(self, 'stats_display'):
self.stats_display.update(self.cardsText())
# ██████ ███████ █████ ██ ███████ ██████ ██████ █████ ██████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ █████ ███████ ██ █████ ██████ ██ ███████ ██████ ██ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██ ██ ███████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██████ ███████
class DealerCards(Static):
"""revealHidden=False (default): hole card stays hidden, used on GameScreen.
revealHidden=True: hole card is shown, used on EndScreen once the round is over."""
dealerHand = reactive(game.dealer_hand)
def __init__(self, revealHidden: bool = False, **kwargs):
super().__init__(**kwargs)
self.revealHidden = revealHidden
self.dealerCards = self._computeCardsText()
def compose(self):
with Horizontal(id="hand-container"):
self.stats_display = Static(self.cardsText(), classes="dealerCardsText")
yield self.stats_display
def _computeCardsText(self):
hideHoleCard = not self.revealHidden
total = game.countHand(game.dealer_hand, hideHoleCard)
cards = game.deckOCards.printAHand(game.dealer_hand, hideHoleCard)
return f"Dealer's Hand: [{total}]\n{cards}"
def cardsText(self):
return self.dealerCards
def on_mount(self):
self.set_interval(0.1, self.refresh_stats)
self.refresh_stats()
def refresh_stats(self):
"""Refresh stats from game object"""
old_hand = self.dealerHand
old_cards = self.dealerCards
new_hand = game.dealer_hand
new_cards = self._computeCardsText()
if (old_hand != new_hand or
old_cards != new_cards):
self.dealerHand = new_hand
self.dealerCards = new_cards
self.update_stats_display()
def update_stats_display(self):
if hasattr(self, 'stats_display'):
self.stats_display.update(self.cardsText())
# ██████ ███████ ████████ ███████ ██████ ██████ ███████ ███████ ███ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
# ██████ █████ ██ ███████ ██ ██████ █████ █████ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██ ███████ ██████ ██ ██ ███████ ███████ ██ ████
class BetScreen(Screen):
BINDINGS = [
("ctrl+b", "backToMainMenuFromBetScreen", "Back to Main Menu"),
]
CSS_PATH="tcss/BetScreen.tcss"
def action_backToMainMenuFromBetScreen(self):
mainApp.pop_screen()
def on_mount(self):
self.sub_title = "Betting screen"
self.add_class("bet-screen")
def compose(self):
yield Header()
yield CustomFooter()
yield Static("Please, input your bet!")
yield Input( type="integer", placeholder="100", id="betTextField" )
yield Static("Press enter to place the bet!")
yield Button("Cancel", id="cancelBetButton")
@on(Button.Pressed, "#cancelBetButton")
def cancelBet(self):
self.app.pop_screen()
@on(Input.Submitted, '#betTextField')
def placeABet(self, event: Input.Submitted):
bet_value = event.value
if not bet_value:
self.notify(
"Please enter a bet amount.",
severity="error"
)
return
validation_result = event.validation_result
if validation_result is not None and not validation_result.is_valid:
errors = "\n".join(validation_result.failure_descriptions)
self.notify(f"Invalid bet:\n{errors}", severity="error")
return
try:
bet_amount = int(bet_value)
if bet_amount <= 0:
self.notify("Bet must be greater than 0", severity="error")
return
elif bet_amount > game.player.money:
self.notify(f"Insufficient funds! You only have ${game.player.money}", severity="error")
return
else:
game.player.bet = bet_amount
game.player.money -= game.player.bet
self.notify(f"Bet placed: ${bet_amount}", severity="information")
self.app.switch_screen(GameScreen())
except ValueError:
self.notify("Please enter a valid number", severity="error")
return
# ███ ███ █████ ██ ███ ██ ███ ███ ███████ ███ ██ ██ ██ ███████ ██████ ██████ ████████ ███████ ██████
# ████ ████ ██ ██ ██ ████ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ████ ██ ███████ ██ ██ ██ ██ ██ ████ ██ █████ ██ ██ ██ ██ ██ █████ ██ ██ ██ ██ ██ █████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ████ ██ ██ ███████ ██ ████ ██████ ██ ██████ ██████ ██ ███████ ██ ██
class CustomFooter(Static):
# NOTE: CSS_PATH only works on App/Screen subclasses in Textual, NOT
# on plain widgets like this Static. That's why tcss/CustomFooter.tcss
# was never actually loading. Widgets use DEFAULT_CSS (an inline
# string) instead - you can delete tcss/CustomFooter.tcss now.
DEFAULT_CSS = """
CustomFooter {
dock: bottom;
height: 1;
background: $panel;
color: $foreground;
#footer-container {
height: 1;
align: center middle;
}
#textDisplay {
color: #fff;
text-style: bold;
margin: 0 2;
width: auto;
}
}
"""
playerMoney = reactive(game.player.money)
playerWins = reactive(game.player.gameInfo["gamesWon"])
playerLosses = reactive(game.player.gameInfo["gamesLost"])
def compose(self):
with Horizontal(id="footer-container"):
self.stats_display = Static(self.footer_text(), id="textDisplay")
yield self.stats_display
def footer_text(self):
return f"Available Money: ${self.playerMoney} | Games Won: {self.playerWins} | Games Lost: {self.playerLosses}"
def updateFooterStats(self):
self.playerMoney = game.player.money
self.playerWins = game.player.gameInfo["gamesWon"]
self.playerLosses = game.player.gameInfo["gamesLost"]
self.update_stats_display()
def on_mount(self):
self.set_interval(0.1, self.refresh_stats)
self.refresh_stats()
def refresh_stats(self):
"""Refresh stats from game object"""
old_money = self.playerMoney
old_wins = self.playerWins
old_losses = self.playerLosses
new_money = game.player.money
new_wins = game.player.gameInfo["gamesWon"]
new_losses = game.player.gameInfo["gamesLost"]
if (old_money != new_money or
old_wins != new_wins or
old_losses != new_losses):
self.playerMoney = new_money
self.playerWins = new_wins
self.playerLosses = new_losses
def watch_playerMoney(self):
self.update_stats_display()
def watch_playerWins(self):
self.update_stats_display()
def watch_playerLosses(self):
self.update_stats_display()
def update_stats_display(self):
if hasattr(self, 'stats_display'):
self.stats_display.update(self.footer_text())
# ███ ███ █████ ██ ███ ██ ███ ███ ███████ ███ ██ ██ ██
# ████ ████ ██ ██ ██ ████ ██ ████ ████ ██ ████ ██ ██ ██
# ██ ████ ██ ███████ ██ ██ ██ ██ ██ ████ ██ █████ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ████ ██ ██ ███████ ██ ████ ██████
class MainMenu(Screen):
CSS_PATH = "tcss/MainMenu.tcss"
def compose(self):
yield Header()
yield CustomFooter()
with Container(classes="titleContainer"):
yield Static("BlackJack", classes="title")
yield Static("By Futuraura :3", classes="title")
yield Button("Start", id="startTheGame")
yield Button("Save the game", id="saveTheGame")
yield Button("Load the game", id="loadTheGame")
def on_mount(self):
self.sub_title = "Main Menu"
@on(Button.Pressed, "#startTheGame")
def startTheGame(self):
mainApp.push_screen(BetScreen())
@on(Button.Pressed, "#saveTheGame")
def saveTheGame(self):
success, message = game.saveGame()
if success:
mainApp.notify(
message,
title="Save Game",
severity="information"
)
else:
mainApp.notify(
message,
title="Save Game Error",
severity="error"
)
@on(Button.Pressed, "#loadTheGame")
def loadTheGame(self):
success, message = game.loadGame()
if success:
mainApp.notify(
message,
title="Load Game",
severity="information"
)
else:
mainApp.notify(
message,
title="Load Game Error",
severity="error"
)
# ████████ ██ ██ ███████ █████ ██████ ██████ ██████ ██ █████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ███████ █████ ███████ ██████ ██████ ██ ██ ███████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ███████ ██ ██ ██ ██ ██████ ███████ ██ ██ ███████ ███████
class blackjackApp(App):
ENABLE_COMMAND_PALETTE = True
def on_mount(self):
self.register_theme(pokerTheme)
self.theme = "poker"
self.title = "Blackjack Game"
self.push_screen(MainMenu())
if __name__ == "__main__":
mainApp = blackjackApp()
mainApp.run()