-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (128 loc) · 4.13 KB
/
main.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
import random
card_faces = ["Spades", "Hearts", "Diamonds", "Clubs"]
cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "King", "Queen", "Jack", "Ace"]
deck = []
def create_deck(card_faces, cards):
deck = []
for face in card_faces:
for card in cards:
deck.append(f"{card} of {face}")
return deck
# Dealing Cards
def deal(deck, hand):
if not deck:
raise ValueError("Deck is Empty.")
drawn_card = deck.pop()
hand.append(drawn_card)
# Calculating
def hand_value(hand):
value = 0
aces = 0
for card in hand:
rank = card.split()[0]
if rank in ["King", "Queen", "Jack"]:
value += 10
elif rank == "Ace":
value += 11
aces += 1
else:
value += int(rank)
while value > 21 and aces > 0:
value -= 10
aces -= 1
return value
def launch():
wallet = 0
wins = 0
losses = 0
running = True
welcome_message = f"""Welcome to this simple Blackjack Game!
You currently have: {wallet}! (If you run out you can take a loan :D)
You have won: {wins} times & lost: {losses} times.
Select one of the optinons below to proceed.
"""
print(welcome_message)
while running:
print("(1): Play a Game")
print("(2): Take a loan")
print("(3): Show Stats")
print("(4): Reset Score")
print("(5): Exit")
choice = input("Select a number: ")
if choice == "1":
bet = int(input("How much do you wanna bet? "))
if bet >= 1 and bet <= wallet:
print(f"You have bet: {bet}")
player_hand = []
house_hand = []
deck = create_deck(card_faces.copy(), cards.copy())
random.shuffle(deck)
print("Shuffling Deck...")
deal(deck, player_hand)
deal(deck, player_hand)
deal(deck, house_hand)
deal(deck, house_hand)
busted = False
print("dealing...")
while hand_value(player_hand) <21:
print(f"Your hand is: {player_hand}, {hand_value(player_hand)}")
decision = input("(1)Hit or (2)Stay: ")
if decision == "1":
deal(deck, player_hand)
if decision == "2":
break
if hand_value(player_hand) > 21:
busted = True
while hand_value(house_hand) < 17:
deal(deck, house_hand)
if busted:
print(f"You bust, you had {hand_value(player_hand)}, house had {hand_value(house_hand)}! The House has won this round!")
losses += 1
wallet -= bet
print(f"You now have: {wallet} in your wallet")
elif hand_value(house_hand) > 21:
print(f"House Busts, you had {hand_value(player_hand)}, house had {hand_value(house_hand)}!")
wins += 1
wallet += bet
print(f"You now have: {wallet} in your wallet")
elif hand_value(player_hand) > hand_value(house_hand) and hand_value(player_hand) <= 21:
print(f"You've won, you had {hand_value(player_hand)}, house had {hand_value(house_hand)}!")
wins += 1
wallet += bet
print(f"You now have: {wallet} in your wallet")
elif hand_value(player_hand) == hand_value(house_hand):
print("Tie")
else:
print(f"House wins, you had {hand_value(player_hand)}, house had {hand_value(house_hand)}")
losses += 1
wallet -= bet
print(f"You now have: {wallet} in your wallet")
else:
print("Sorry there was a probleming starting the game. Are you poor?")
elif choice == "2":
if wallet >= 100:
print("You're too rich to take a loan!")
else:
wallet = 250
print(f"Your loan was approved, you now have: {wallet}")
elif choice == "3":
stats = f"""Your stats are as follows:
Wins: {wins}
Losses: {losses}
Current Balance: {wallet}
"""
print(stats)
elif choice == "4":
wins = 0
losses = 0
reset_stats = f"""Your stats are now as follows:
Wins: {wins}
Losses: {losses}
"""
print(reset_stats)
elif choice == "5":
running = False
else:
print(f"Thanks for playing, your final stats were. Wins: {wins}, Losses: {losses}")
if __name__ == "__main__":
launch()