?python?blackjack you will build a simple game of blackjack where the computer p
ID: 3763965 • Letter: #
Question
?python?blackjack
you will build a simple game of blackjack where the computer plays as the dealer. In this simplified version of blackjack, the active player is dealt a card whose value ranges between 2 and 11. For simplicity, we will ignore the dual role that aces play (either as 1 or 11) and suits. Because of this, some impossible situations may occur, such as 5 2-value cards being dealt in a given hand. Likewise, this game does not use true odds as we won’t be modifying the odds of getting a card based on previous cards dealt.
Program Flow
Your program should follow the general outline below. Program Start
1. Output an introduction Starting with the player, do the following
2. Deal the player a card.
3. Add the card’s value to the player’s total hand.
4. Output the value of the card just dealt and the player’s current hand total (score)
5. Ask the player if he would like to hit or stand
6. If the player hits, go back to #1
After the player busts (goes over 21) or stands
1. If the player has not busted, begin computer’s play
2. Deal the computer a card 3. Add the value of the card to the computer’s hand total (score)
4. If a card causes the computer to bust (go over 21), output a message. Go to step #7.
5. If the computer’s hand total is less than 17, it must take another card (hit). Go to step #2.
6. If the computer’s hand is 17 or more, the computer stands.
7. Stop computer play
When both player and computer are done
1. If the player has not busted (gone over 21) and the computer busted, the player wins
2. If the player has not busted, the computer has not busted, and the player’s hand total is greater than the computer, then the player wins
3. If the player has not busted, the computer has not busted, and the player’s hand total is less than the computer’s then the computer wins
4. If the player has busted, then the computer wins.
5. Ask the player if he would like to play again.
Recommended Variables
It might be helpful for your program to track the following items using variables: ?
Whether or not the player would like to play again ?
Whether or not the player would like to hit or stand ?
The player’s current score ?
The computer’s current score ?
Whether or not the player has busted ?
The value of the next card drawn
Required Functions
In this function, you must define and use the following functions:
get_next_card_value() ?
Randomly returns a value between 2 and 11 ?
(bonus, see grading) In addition to a value, your program returns and displays a valid suit
(bonus, see grading) In addition to a value and suit, your program uses true odds o
E.g. when an Ace of Diamonds is dealt, it is guaranteed not to be dealt again for the remainder of the game
(bonus, see grading) In addition to the above two bonuses, your program allows the user to play with multiple decks and maintains true odds. Example, when playing with two decks, it is possible to draw up to two Ace of Diamonds per game, but no more.
play_game() ?
Begins play of the game. Will call other functions as necessary.
main() ?
Outputs welcome message ?
Calls the play_game function.
Sample Output
Explanation / Answer
import simplegui
import random
# load card sprite - 949x392 - source: jfitz.com
CARD_SIZE = (73, 98)
CARD_CENTER = (36.5, 49)
card_images = simplegui.load_image("c://cards.png")
CARD_BACK_SIZE = (71, 96)
CARD_BACK_CENTER = (35.5, 48)
card_back = simplegui.load_image("c://card_back.png")
# initialize global variables
in_play = False
message = ""
outcome = ""
score = 0
popped = []
player = []
dealer = []
deck = []
SUITS = ('C', 'S', 'H', 'D')
RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
class Card:
def __init__(self, suit, rank):
if (suit in SUITS) and (rank in RANKS):
self.suit = suit
self.rank = rank
else:
self.suit = None
self.rank = None
def __str__(self):
return self.suit + self.rank
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def draw(self, canvas, pos):
card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank),
CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE)
class Hand:
def __init__(self):
self.player_hand = []
def __str__(self):
s = ''
for c in self.player_hand:
s = s + str(c) + ' '
return s
def add_card(self, card):
self.player_hand.append(card)
return self.player_hand
def get_value(self):
value = 0
for card in self.player_hand:
rank = card.get_rank()
value = value + VALUES[rank]
for card in self.player_hand:
rank = card.get_rank()
if rank == 'A' and value <= 11:
value += 10
return value
def draw(self, canvas, p):
pos = p
for card in self.player_hand:
card.draw(canvas, p)
pos[0] = pos[0] + 90
if in_play == True:
canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, [115.5,184], CARD_BACK_SIZE)
class Deck:
def __init__(self):
popped = []
self.cards = [Card(suit, rank) for suit in SUITS for rank in RANKS]
self.shuffle()
def __str__(self):
s = ''
for c in self.cards:
s = s + str(c) + ' '
return s
def shuffle(self):
random.shuffle(self.cards)
def deal_card(self):
popped = self.cards.pop(0)
return popped
def deal():
global in_play, player, dealer, deck, message, score, outcome
if in_play == True:
# if player clicks Deal button during a hand, player loses hand in progress
message = "Here is the new hand"
score -= 1
deck = Deck()
player = Hand()
dealer = Hand()
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
if in_play == False:
# starts a new hand
deck = Deck()
player = Hand()
dealer = Hand()
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
message = "New Hand. Hit or Stand?"
in_play = True
outcome = ""
def hit():
global in_play, score, message
if in_play == True:
player.add_card(deck.deal_card())
message = "Hit or Stand?"
if player.get_value() > 21:
in_play = False
message = "Player busted! You Lose! Play again?"
score -= 1
outcome = "Dealer: " + str(dealer.get_value()) + " Player: " + str(player.get_value())
def stand():
global in_play, score, message, outcome
if in_play == False:
message = "The hand is already over. Deal again."
else:
while dealer.get_value() < 17:
dealer.add_card(deck.deal_card())
if dealer.get_value() > 21:
message = "Dealer busted. You win! Play again?"
score += 1
in_play = False
elif dealer.get_value() > player.get_value():
message = "Dealer wins! Play again?"
score -= 1
in_play = False
elif dealer.get_value() == player.get_value():
message = "Tie! Dealer wins! Play again?"
score -= 1
in_play = False
elif dealer.get_value() < player.get_value():
message = "You win! Play again?"
score += 1
in_play = False
outcome = "Dealer: " + str(dealer.get_value()) + " Player: " + str(player.get_value())
def exit():
frame.stop()
def draw(canvas):
canvas.draw_text("Blackjack", [270,50], 48, "Yellow")
canvas.draw_text("Score : " + str(score), [80,520], 36, "Black")
canvas.draw_text("Dealer :", [80,110], 30, "Black")
canvas.draw_text("Player :", [80,300], 30, "Black")
canvas.draw_text(message, [200,480], 26, "Black")
canvas.draw_text(outcome, [80,560], 28, "White")
dealer.draw(canvas, [80,135])
player.draw(canvas, [80,325])
frame = simplegui.create_frame("Blackjack", 700, 600)
frame.set_canvas_background("Green")
frame.add_button("Deal", deal, 200)
frame.add_button("Hit", hit, 200)
frame.add_button("Stand", stand, 200)
frame.add_button("Exit", exit, 200)
frame.set_draw_handler(draw)
deal()
frame.start()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.