Question

In: Computer Science

Twenty One This game is similar to the famous card game blackjack. We will play a...

Twenty One

This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points.

The objective in each round of the game is to score as close to 21 as possible by rolling a die as many times as you wish and adding all the numbers that appear. When a player's total exceeds 21, he is 'busted' and gets 0 points. If the player chooses to stop rolling before he exceeds 21, then he wins whatever his total is at that point. So for example, if a player rolls 5, 2, 4, and then 6, his total at that point is 17, and he has to decide whether it is worth trying again: he will be busted if he gets 5 or more (since 17+5=22), but will get a better total if he gets 4 or less.

There are many variations on this game, some involving multiple players, or a "banker" or different numbers of dice, or alcohol..... here is a short YT video explaining the basic game.

A computer can play this game with a suitable strategy. For this problem, we will consider a strategy to be simply an integer K which is the value at which you stop rolling (thinking that you are close enough to 21). The number K is fixed for the entire game. For example, if you set K = 19, then in every round, you will keep rolling if your sum to that point is less than 19; if you get a num ≥ 19 you stop. Clearly, any good strategy will be a number at least 15, since 15+6=21 and if you roll again at 15, you will never bust. But we will try all possible strategies.

QUESTION: in python

You should write a function playRound(K) which rolls a single die until you reach or exceed K or get busted, and either return your score (if you reached or exceeded K), or 0 (if you were busted). Then write a function playGame() which calls playRound(K) for N = 10,000 times for each K and returns an array of 21 numbers giving the average payoff for each K = 1, ..., 21.

Your task is to answer the following questions:

  • For each K = 1 .. 21, what is the average payoff per round for a game played with this strategy?

  • What is the best strategy for the game, meaning what value of K wins the most points on average?

Print out the values and an appropriate bar chart for the first question, and simply print out the answer to the second question using a print(...) function.

Solutions

Expert Solution

Here is your required code in PYTHON:

import random

def playRound(k):
points=0
# simulating die rolls until points are less than k
while points<k:
# generating random value from 1 to 6
d=random.randrange(6)+1
points += d
if(points>21):
# for bust
return 0
else:
return points
  
def playGame():
# declaring array of size 21
a = [None] * 21
  
# simulating playRound for values of k = 1,2,3.... 21
for k in range(21):
total = 0
# simulating playRound 10000 times for each value of k
for i in range(10000):
total += playRound(k+1)
# calculating average
average = total/10000
a[k] = average
  
# returning array of averages
return a

#driver code
#fetching result of playgame
result = playGame()

print(result)

max=0.0
bestK=0
# displaying average scores
# calculating best score
print("Average scores for K: \n")
for i in range(21):
if(result[i]>max):
max=result[i]
bestK = i+1
print(i+1, " = ", result[i])

# displaying best results
print("\nBest Strategy is: ")
print("K =", bestK, "with", max, "points.")

Please refer to the screenshot of the code to understand the indentation of the code:

Here is the Output for the sample Test Cases:

Here is the Bar Chart for the results we got after the simulation of the game:


Related Solutions

Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
A card game at a casino costs $10 to play. One card is selected at random...
A card game at a casino costs $10 to play. One card is selected at random from a standard deck. If a heart is selected the player wins $19. If a spade is selected, the player wins $14. If a club or diamond is selected, the player wins nothing. What is the expected value of this game to the player? Round your answer to the nearest cent. Do not include the $ sign in your answer. A homeowner purchases an...
This project involves writing a program to simulate a blackjack card game. You will use a...
This project involves writing a program to simulate a blackjack card game. You will use a simple console-based user interface to implement this game. A simple blackjack card game consists of a player and a dealer. A player is provided with a sum of money with which to play. A player can place a bet between $0 and the amount of money the player has. A player is dealt cards, called a hand. Each card in the hand has a...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
USING JAVA... We want to play a simple card game with 2 players option. The deck...
USING JAVA... We want to play a simple card game with 2 players option. The deck of cards contains 52 cards with 13 cards each in the 4 suits: clubs, diamonds, hearts and spades. Each player begins with 26 cards and one of the players starts the game by putting their first card on the table. Players take turns by putting the top card from their hand, until the current card placed on the table matches the suit of the...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against...
CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against each other. They are drawing poker cards in order to •Make the added rank (score) as close as to 21, but without going over 21.•And make the added score larger than the opponent’s.−Each player starts with two random cards. −Then ask the user if one more card until the user is satisfied with his/her total score. If the user’s total score gets 21, announce...
javascript BlackJack i made a blackjack game, the code is below //this method will return a...
javascript BlackJack i made a blackjack game, the code is below //this method will return a shuffled deck function shuffleDeck() { //this will store array fo 52 objects const decks = [] const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'] const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One'] //run a loop till 52 times for (let i = 0; i < 52; i++) { //push a random item decks.push({ suit: suits[Math.floor(Math.random() *...
javascript BlackJack i made a blackjack game, the code is below //this method will return a...
javascript BlackJack i made a blackjack game, the code is below //this method will return a shuffled deck function shuffleDeck() { //this will store array fo 52 objects const decks = [] const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'] const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One'] //run a loop till 52 times for (let i = 0; i < 52; i++) { //push a random item decks.push({ suit: suits[Math.floor(Math.random() *...
You are to play three games. In the first game, you draw a card, and you...
You are to play three games. In the first game, you draw a card, and you win if the card is a heart. In the second game, you toss two coins, and you win if one head and one tail are shown. In the third game, two dice are rolled and you win if the sum of the dice is 7 or 11. What is the probability that you win all three games? What is the probability that you win...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT