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 5b2b054

Browse files
Added Blackjack
1 parent 4e0152e commit 5b2b054

File tree

2 files changed

+248
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

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