-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_management_nogui.py
123 lines (111 loc) · 5.3 KB
/
game_management_nogui.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
import deck as d
import players_no_gui as p
import croupier_no_gui as c
def playerAction(bet, canDouble): # Renamed continue() to playerAction() because continue is a key-word. Also added
prompt = "│Your action [hit, stand" + (", double down" if canDouble else "") + "]: "
noDoubleString = "│Your balance is too low to double down."
action = input(f"{' ':{' '}<80s}" + "\b│\r" + prompt).lower()
if action in ["double", "double down", "d"] and not canDouble:
print(f"{noDoubleString:{' '}<80s}" + "\b│")
action = ""
while action not in ["hit", "h", # Hit = Croupier draws a new card for that player
"stand", "s", # Stand = The player takes no more cards
"double", "double down", "d"]: # Double Down = The player's bet is doubled and the croupier draws an other card for that player
action = input(f"{' ':{' '}<80s}" + "\b│\r" + prompt).lower()
if action in ["double", "double down", "d"] and not canDouble:
print(f"{noDoubleString:{' '}<80s}" + "\b│")
action = ""
if action in ["stand", "s"]:
return False
if action in ["double", "double down", "d"]:
bet[0] *= 2
return True
# @ param
# player: the player object
# playerName: the player's name
# turnNumber: current turn number to be printed
# deck: the deck of cards
def playerTurn(player, playerName, turnNumber, deck, nPlayers):
score = player["score"][-1]
form = f"┤ Round {turnNumber} ; Player: {playerName} ; Score: {score} ├"
print(f"{form:{'─'}^80s}" + "\b┤\r├")
canDouble = (player["bet"][0]*2) <= player["balance"]
con = playerAction(player["bet"], canDouble)
if con:
c = d.drawCard(deck, nPlayers)[0]
drawText = f"│{playerName}'s draw: {c}"
print(f"{drawText:<80s}" + "\b│")
player["hand"].append(c)
player["score"][-1] = d.calcScore(player["hand"])
if player["score"][-1] >= 21:
player["stillPlaying"] = False
return
player["stillPlaying"] = False
# @ param
# player: the player object
# turnNumber: current turn number to be printed
# deck: the deck of cards
def gameTurn(players, turnNumber, deck):
for name, player in players.items():
if player["stillPlaying"]:
playerTurn(player, name, turnNumber, deck, len(players))
def gameOver(players):
flag = True
for _,player in players.items():
flag = flag and not(player["stillPlaying"])
return flag
# @param
# d: the deck of cards
def completeGame(players, deck):
croupier = c.initCroupier()
for name,player in players.items():
player["score"].append(0)
player["stillPlaying"] = True
sanitised = False
while not sanitised:
bal = player["balance"]
bet = input(f"{name}'s bet (balance: ${bal:.2f}): ").split('.') # splitting the input in integer and decimal part
if len(bet) > 2 or len(bet) < 0: # if there are more than two elements in the list, the input was not a valid real number
print("Invalid value")
else: # otherwise, the input sanitisation can continue
flag = True # test if all the inputed characters (excluding the decimal point) were digits
for i in ''.join(bet):
if i not in "0123456789":
flag = False
if flag: # if it's the case, the input is finally sanitised and can be processed
bet = float('.'.join(bet))
if bet > bal or bet < 5: # cannot bet more than you own or less than $5
print("Invalid value")
else:
player["bet"] = [bet]
sanitised = True
else: # otherwise the input was not a valid number
print("Invalid value")
blackJack = p.firstTurn(players, deck)
if blackJack:
for _,player in players.items():
player["stillPlaying"] = False
c.firstTurn(croupier, deck, len(players))
i = 2
while not gameOver(players):
gameTurn(players, i, deck)
i += 1
if not blackJack:
c.play(croupier, deck, len(players)) # when every player has standed or busted, it's the croupier's turn
winners = p.winner(players, croupier, blackJack)
broke = []
for name, player in players.items():
if name in winners:
player["wins"] += 1
player["balance"] += player["bet"][0] + blackJack*0.5*player["bet"][0]
else:
player["balance"] -= player["bet"][0]
if player["balance"] == 0:
broke.append(name)
for name in broke:
del players[name]
if len(winners):
form = f"┤ Game Over ; {', '.join(winners)} won the game {'with a Black Jack ' if blackJack else ''}against the house ├"
else:
form = f"┤ Game Over ; All players lost to the house ├"
print(f"{form:{'─'}^80s}" + "\b╯\r╰")