In this problem, we study the popular card game Blackjack, also known as twenty-
ID: 3794296 • Letter: I
Question
In this problem, we study the popular card game Blackjack, also known as twenty-one. In our simplfied and modified version of the game, the dealer draws cards from an infinite number of well-shuffled decks of 52 cards. Therefore, the probability of getting each of the 13 ranks (Ace, 2, 3, ..., 10, Jack, Queen, King) in each drawing is simply 1/13. Note the suits (spades, hearts, diamonds, and clubs) do not matter for this game. When the game starts, both you and the dealer will get two cards. While both your cards are face-up, only the first card of the dealer is revealed and her/his second card faces down. After receiving your first two cards, you next choose to "hit" (take a third card and end your turn) or "stand" (do not take a third card and end your turn). There is no double or split for our version of the game. The dealer will do the same after you. Both you and the dealer will get at most three cards. (In a real game, the player and the dealer can take multiple turns to hit or stand). You and the dealer get points by adding together the values of your cards. Face cards (Kings, Queens, and Jacks) are counted as ten points. Ace is always counted as 11 points (which is different from the usual twenty-one). All other cards are counted as the numeric value shown on the card. Your objective is to beat the dealer in one of the following ways: Get 21 points on your first two cards (called a "blackjack"); Let the dealer's points exceed 21 (called "bust") before you do; or Reach a final score strictly higher than the dealer without bust. Hand in both your scripts (in text) and the required outputs of your scripts. Find the probability that you get a blackjack (from your first two cards of course). Write a MATLAB script that estimates the probability of getting a blackjack by simulating one million draws of two cards. Output the result. Let X_1 and X_2 be the points of your first and second cards respectively. Compute and use the stem command to plot the pmf for the total points of your first two cards X = X_1 + X_2. Given the points of your first two cards X = x (what values x can take?), compute the probability of bust when you decide to take the third card. Use the stem command to plot this function of x.Explanation / Answer
import simplegui
import random
CARD_SIZE = (73, 98)
CARD_CENTER = (36.5, 49)
card_images = simplegui.load_image
CARD_BACK_SIZE = (71, 96)
CARD_BACK_CENTER = (35.5, 48)
card_back = simplegui.load_image
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:
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:
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.