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.
import matplotlib as mpl;
mpl.use('Agg');
import matplotlib.pyplot as plt;
import random;
def playRound(K):
score=0;
while True:
score =score + random.randint(1,6);
if(score>=K and score<=21):
break;
elif(score>21):
score=0;
break;
return score;
def playGame():
array_payoff = [];
for K in range(1,22):
sum_payoff=0;
average_payoff=0;
for N in range(1,10001):
sum_payoff+=playRound(K);
average_payoff=sum_payoff/10000;
array_payoff.append(average_payoff);
return array_payoff;
payoff_for_K=playGame();
print (payoff_for_K);
max = max(payoff_for_K);
print ("\nMaximum value of payoff is : ",max);
print ("\nThe value of K for maximum payoff is : ",payoff_for_K.index(max)+1);
fig = plt.figure(figsize=(10, 8));
ax = fig.add_subplot(111);
ax.plot(range(1,22),payoff_for_K);
ax.set_xlabel('Value of K')
ax.set_ylabel('Average Payoff')
ax.set_title('Graph showing Payoff for K')
fig.savefig('graph.png')
Output : ---------------------------------------------------------------------------------------
[3.5069, 4.0705, 4.7491, 5.5468, 6.4978, 7.5674, 8.8247, 9.702,81, 10.6615, 11.6722, 12.6362, 13.6774, 14.6792, 15.679, 16.69,581, 17.689, 17.525, 16.5143, 14.1735, 10.4418, 5.9829]
Maximum value of payoff is : 17.689
The value of K for maximum payoff is : 16
graph.png