Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60b3532

Browse files
Merge pull request avinashkranjan#591 from mehabhalodiya/Blackjack
Added Blackjack
2 parents 3356531 + c158583 commit 60b3532

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed

‎Blackjack/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Blackjack
2+
3+
Blackjack (also known as 21) is a multiplayer card game, with fairly simple rules.
4+
5+
For this script, I am implementing a simplified version where a user can play against the computer who acts as dealer.
6+
7+
Two cards are dealt to each player. The dealer shows one card face up, and the other is face down. The player gets to see both of his or her cards and the total of them is added.
8+
9+
Aces are worth 1 or 11 points, and all other cards are worth their face value. The goal of the game is to get as close to 21 ("blackjack") without going over (called "busting.")
10+
11+
The human player goes first, making his or her decisions based on the single dealer card showing.
12+
13+
</br>
14+
15+
The player has two choices: Hit or Stand.
16+
- Hit means to take another card.
17+
- Stand means that the player wishes no more cards, and ends the turn, allowing for the dealer to play.
18+
19+
The dealer must hit if their card total is less than 17, and must stand if it is 17 or higher.
20+
21+
Whichever player gets closest to 21 without exceeding it, wins.
22+
23+
</br>
24+
25+
## Example Run
26+
27+
![demo](https://user-images.githubusercontent.com/73488906/111056858-9854d280-84a8-11eb-9cbe-c5858764c6bc.png)
28+
![demo](https://user-images.githubusercontent.com/73488906/111056838-50ce4680-84a8-11eb-84f4-62c8364c82ef.png)

‎Blackjack/blackjack.py‎

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
# IMPORT MODULES AND DEFINE VARIABLES
2+
3+
import random
4+
5+
playing = True
6+
7+
8+
# CLASSES
9+
10+
class Card: # Creates all the cards
11+
12+
def __init__(self, suit, rank):
13+
self.suit = suit
14+
self.rank = rank
15+
16+
def __str__(self):
17+
return self.rank + ' of ' + self.suit
18+
19+
20+
class Deck: # creates a deck of cards
21+
22+
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
23+
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
24+
25+
def __init__(self):
26+
self.deck = [] # haven't created a deck yet
27+
for suit in Deck.suits:
28+
for rank in Deck.ranks:
29+
self.deck.append(Card(suit, rank))
30+
31+
def __str__(self):
32+
deck_comp = ''
33+
for card in self.deck:
34+
deck_comp += '\n ' + card.__str__()
35+
return 'The deck has: ' + deck_comp
36+
37+
def shuffle(self): # shuffle all the cards in the deck
38+
random.shuffle(self.deck)
39+
40+
def deal(self): # pick out a card from the deck
41+
single_card = self.deck.pop()
42+
return single_card
43+
44+
45+
class Hand: # show all the cards that the dealer and player have
46+
47+
values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8,
48+
'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
49+
50+
def __init__(self):
51+
self.cards = []
52+
self.value = 0
53+
self.aces = 0 # keep track of aces
54+
55+
def add_card(self, card): # add a card to the player's or dealer's hand
56+
self.cards.append(card)
57+
self.value += Hand.values[card.rank]
58+
if card.rank == 'Ace':
59+
self.aces += 1
60+
61+
def adjust_for_ace(self):
62+
while self.value > 21 and self.aces:
63+
self.value -= 10
64+
self.aces -= 1
65+
66+
67+
class Chips: # keep track of chips
68+
69+
def __init__(self):
70+
self.total = 100
71+
self.bet = 0
72+
73+
def win_bet(self):
74+
self.total += self.bet
75+
76+
def lose_bet(self):
77+
self.total -= self.bet
78+
79+
80+
# FUNCTIONS
81+
82+
def take_bet(chips): # ask for user's bet
83+
84+
while True:
85+
try:
86+
chips.bet = int(input("How many chips would you like to bet? "))
87+
except ValueError:
88+
print("Sorry! Please can you type in a number: ")
89+
else:
90+
if chips.bet > chips.total:
91+
print("You bet can't exceed 100!")
92+
else:
93+
break
94+
95+
96+
def hit(deck, hand):
97+
hand.add_card(deck.deal())
98+
hand.adjust_for_ace()
99+
100+
101+
def hit_or_stand(deck, hand): # hit or stand
102+
global playing
103+
104+
while True:
105+
ask = input("\nWould you like to hit or stand? Please enter 'h' or 's': ")
106+
107+
if ask[0].lower() == 'h':
108+
hit(deck, hand)
109+
elif ask[0].lower() == 's':
110+
print("Player stands, Dealer is playing.")
111+
playing = False
112+
else:
113+
print("Sorry! I did not understand that! Please try again!")
114+
continue
115+
break
116+
117+
118+
def show_some(player, dealer):
119+
print("\nDealer's Hand: ")
120+
print(" <card hidden>")
121+
print("", dealer.cards[1])
122+
print("\nPlayer's Hand: ", *player.cards, sep='\n ')
123+
124+
125+
def show_all(player, dealer):
126+
print("\nDealer's Hand: ", *dealer.cards, sep='\n ')
127+
print("Dealer's Hand =", dealer.value)
128+
print("\nPlayer's Hand: ", *player.cards, sep='\n ')
129+
print("Player's Hand =", player.value)
130+
131+
132+
# Game endings
133+
134+
def player_busts(player, dealer, chips):
135+
print("PLAYER BUSTS!")
136+
chips.lose_bet()
137+
138+
139+
def player_wins(player, dealer, chips):
140+
print("PLAYER WINS!")
141+
chips.win_bet()
142+
143+
144+
def dealer_busts(player, dealer, chips):
145+
print("DEALER BUSTS!")
146+
chips.win_bet()
147+
148+
149+
def dealer_wins(player, dealer, chips):
150+
print("DEALER WINS!")
151+
chips.lose_bet()
152+
153+
154+
def push(player, dealer):
155+
print("Its a push! Player and Dealer tie!")
156+
157+
158+
# Gameplay!
159+
160+
while True:
161+
print("Welcome to BlackJack!")
162+
163+
# create an shuffle deck
164+
deck = Deck()
165+
deck.shuffle()
166+
167+
player_hand = Hand()
168+
player_hand.add_card(deck.deal())
169+
player_hand.add_card(deck.deal())
170+
171+
dealer_hand = Hand()
172+
dealer_hand.add_card(deck.deal())
173+
dealer_hand.add_card(deck.deal())
174+
175+
# set up the player's chips
176+
player_chips = Chips()
177+
178+
# ask player for bet
179+
take_bet(player_chips)
180+
181+
# show cards
182+
show_some(player_hand, dealer_hand)
183+
184+
while playing:
185+
186+
hit_or_stand(deck, player_hand)
187+
show_some(player_hand, dealer_hand)
188+
189+
if player_hand.value > 21:
190+
player_busts(player_hand, dealer_hand, player_chips)
191+
break
192+
193+
if player_hand.value <= 21:
194+
195+
while dealer_hand.value < 17:
196+
hit(deck, dealer_hand)
197+
198+
show_all(player_hand, dealer_hand)
199+
200+
if dealer_hand.value > 21:
201+
dealer_busts(player_hand, dealer_hand, player_chips)
202+
203+
elif dealer_hand.value > player_hand.value:
204+
dealer_wins(player_hand, dealer_hand, player_chips)
205+
206+
elif dealer_hand.value < player_hand.value:
207+
player_wins(player_hand, dealer_hand, player_chips)
208+
209+
if player_hand.value > 21:
210+
player_busts(player_hand, dealer_hand, player_chips)
211+
212+
print("\nPlayer's winnings stand at", player_chips.total)
213+
214+
new_game = input("\nWould you like to play again? Enter 'y' or 'n': ")
215+
if new_game[0].lower() == 'y':
216+
playing = True
217+
continue
218+
else:
219+
print("\nThanks for playing!")
220+
break
221+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /