In: Computer Science
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.
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: