Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 94f8988

Browse files
committed
This is a text based blackjack game between one player and an automated dealer. I have used OOP, classes and functions.
1 parent a1cb98a commit 94f8988

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

Basic-Scripts/blackjack_game.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import random #to shuffle the deck
2+
3+
suits = ('hearts','diamonds','spades','clubs')
4+
ranks = ('Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','King','Queen','Jack','Ace')
5+
values = {'Two':2,'Three':3,'Four':4,'Five':5,'Six':6,'Seven':7,'Eight':8,'Nine':9,'Ten':10,'King':10,'Queen':10,'Jack':10,'Ace':11}
6+
7+
playing = True #for controlling while loops
8+
9+
class Card:
10+
11+
def __init__(self,suit,rank):
12+
self.suit=suit
13+
self.rank=rank
14+
15+
def __str__(self):
16+
return f"{self.rank} of {self.suit}"
17+
18+
class Deck: #to store the 52 card objects
19+
20+
def __init__(self):
21+
self.deck = []
22+
for suit in suits:
23+
for rank in ranks:
24+
self.deck.append(Card(suit,rank))
25+
26+
27+
def __str__(self):
28+
resultstr=''
29+
for x in self.deck:
30+
resultstr=resultstr+'\n'+x.__str__() #important
31+
return resultstr
32+
33+
def shuffle(self):
34+
random.shuffle(self.deck)
35+
36+
def deal(self):
37+
singlecard=self.deck.pop() #important
38+
return singlecard
39+
40+
class Hand:
41+
def __init__(self):
42+
self.cards = []
43+
self.value = 0
44+
self.aces = 0
45+
46+
def add_card(self,card):
47+
self.cards.append(card)
48+
self.value=self.value+values[card.rank]
49+
50+
if card.rank=='Ace':
51+
self.aces+=1
52+
53+
def adjust_for_ace(self):
54+
while self.value>21 and self.aces: #aces can have the value 1 or 11
55+
self.value-=10
56+
self.aces-=1
57+
class Chips:
58+
59+
def __init__(self,total=100):
60+
self.total = total
61+
self.bet = 0
62+
63+
def win_bet(self):
64+
self.total+=self.bet
65+
66+
def lose_bet(self):
67+
self.total-=self.bet
68+
69+
def take_bet(chips):
70+
while True:
71+
try:
72+
chips.bet=int(input("Enter bet amount"))
73+
except:
74+
print("Input is incorrect!")
75+
else:
76+
if chips.bet>chips.total:
77+
print(f"you do not have enough chips. you have {chips.total} chips.")
78+
else:
79+
break
80+
81+
def hit(deck,hand):
82+
hitcard=deck.deal()
83+
hand.add_card(hitcard)
84+
hand.adjust_for_ace()
85+
86+
def hit_or_stand(deck,hand):
87+
global playing # to control the while loop
88+
89+
while True:
90+
choice=input('Hit or stand?')
91+
if choice[0].lower()=='h':
92+
hit(deck,hand)
93+
elif choice[0].lower()=='s':
94+
print("Player stands. It is the dealer's chance")
95+
playing=False
96+
else:
97+
print("Wrong input")
98+
continue
99+
break
100+
101+
def show_some(player,dealer): #each time player takes a card
102+
print("Dealer's hidden card: ", dealer.cards[1])
103+
print("Player's cards: ", *player.cards, sep='\n')
104+
105+
def show_all(player,dealer): #at the end of the hand
106+
print("Dealer's cards: ", *dealer.cards, sep='\n')
107+
print("Dealer's value: ",dealer.value)
108+
print("Player's cards: ", *player.cards, sep='\n')
109+
print("Player's value: ",player.value)
110+
111+
def player_busts(player,dealer,chips): #to handle all ending scenarios
112+
print("Player busts.")
113+
chips.lose_bet()
114+
115+
def player_wins(player,dealer,chips):
116+
print("Player wins.")
117+
chips.win_bet()
118+
119+
def dealer_busts(player,dealer,chips):
120+
print("Dealer busts.")
121+
chips.lose_bet()
122+
123+
def dealer_wins(player,dealer,chips):
124+
print("Dealer wins.")
125+
chips.win_bet()
126+
127+
def push(player,dealer,chips):
128+
print("It's a tie/push.")
129+
130+
while True:
131+
print("Welcome to the game!")
132+
133+
# we create & shuffle the deck and deal two cards to each player
134+
deck=Deck()
135+
deck.shuffle()
136+
player_hand=Hand()
137+
dealer_hand=Hand()
138+
player_hand.add_card(deck.deal())
139+
player_hand.add_card(deck.deal())
140+
dealer_hand.add_card(deck.deal())
141+
dealer_hand.add_card(deck.deal())
142+
143+
# Player's chips
144+
player_chips=Chips()
145+
146+
# Prompt the Player for their bet
147+
take_bet(player_chips)
148+
149+
# Show some cards
150+
show_some(player_hand,dealer_hand)
151+
152+
while playing: #variable from our hit_or_stand function
153+
154+
# Prompt for Player to Hit or Stand
155+
hit_or_stand(deck,player_hand)
156+
157+
# Show some cards
158+
show_some(player_hand,dealer_hand)
159+
160+
# If player's hand exceeds 21, player busts
161+
if player_hand.value>21:
162+
player_busts(player_hand,dealer_hand,player_chips)
163+
break
164+
165+
# If Player hasn't busted, Dealer's hand is played until Dealer reaches 17
166+
if player_hand.value<=21:
167+
while dealer_hand.value<17:
168+
hit_or_stand(deck,dealer_hand)
169+
170+
# Show all cards
171+
show_all(player_hand,dealer_hand)
172+
173+
# different ending scenarios
174+
if dealer_hand.value>21:
175+
dealer_busts(player_hand,dealer_hand,player_chips)
176+
elif dealer_hand.value>player_hand.value:
177+
dealer_wins(player_hand,dealer_hand,player_chips)
178+
elif dealer_hand.value<player_hand.value:
179+
player_busts(player_hand,dealer_hand,player_chips)
180+
else:
181+
push(player_hand,dealer_hand)
182+
183+
# Inform Player of their chips total
184+
print("Player's total chips: ", player_chips.total)
185+
186+
# Ask to play again
187+
play_again=input("Do you wish to play again? type yes or no.")
188+
if play_again[0].lower()=='y':
189+
player=True
190+
continue
191+
else:
192+
print("Tha game has finished")
193+
break

0 commit comments

Comments
 (0)