Question
its in python, the following is my code: from collections import namedtuple import random import sys #random cards would be given between 1-13 both ends
its in python, the following is my code:
from collections import namedtuple import random import sys #random cards would be given between 1-13 both ends included def get_card(): return random.randint(1,13)
#this function will take cards as an argument and calculate the points of given cards #count Ace that counted as 11 #the return will be a tuple ex: (19,1)
def score(cards): #total is set at 0 at the beginning #for J, Q, K: they are all 10s
total=0 soft_ace_count=0 scores=namedtuple('scores',['total','soft_ace_count']) for x in cards: if x > 10: x=10
total+=x
#the following code will decide 1 =11 or 1 = 1 if total
return scores(total,soft_ace_count)
print(score(cards=[1,1,1,2]))
#return True when stands on hand, given the policy and False otherwise def stand(stand_on_value, stand_on_soft, cards): total, soft_ace_count = score(cards) print(soft_ace_count)
Stand=namedtuple('Stand',['stand','total'])
if total >= stand_on_value: if soft_ace_count == 0:
stands=Stand(True,total) else:
stands=Stand(stand_on_soft,total) else:
stands=Stand(False,total)
return stands
print(stand(17,False,[1,1,5]))
def play_hand(stand_on_value, stand_on_soft):
return total
#turn input into data type and validate the input in command line(raise ValueError if anything is wrong) def main():
args = sys.argv[1:] bustCount = 0 try:
args[0] = int(args[0]) args[1] = int(args[1]) if args[1] 20: raise ValueError if args[2]=='hard': stand_on_soft=False elif args[2]=='soft': stand_on_soft=True else: raise ValueError
if args[0]
for x in range(args[0]): cards = [get_card(),get_card()] while(not stand(args[1],args[2],cards)): cards.append(get_card()) total, soft_ace_count = score(cards) if(total>21): bustCount+=1 print(float(bustCount/args[0]))
if __name__ == '__main__': main()
Step 3: Play Hand Function In blackjack2.py, refactor the game play logic from Week 3 into a function defined as shown below. This function encapsulates the logic of playing a single hand and returns the final score. If the score is 22 or greater (i.e., bust), just return the value 22; otherwise, return the actual final total of the hand. The parameters of the play_hand function are the values read from the command line, as in Week 3. After doing this, adjust the main function code to utilize this function and make sure it works as before (i.e., as it was described for Week 3). def play_hand(stand_on_value, stand_on_soft): ## TODO: fill this in return total Step 4: Refactor Main In blackjack2.py, refactor the functionality so that it takes a single command line argument, which is the number of simulations to run per strategy. > python3 blackjack2.py 100000 The updated function should loop across all "stand on values" from 13-20 and both stand on soft and stand on hard, and run the specified number of simulations for each strategy, keeping track of the number of simulations that result in each hand's total value. For example, when the stand-on value is 13, and stand on soft is true, there will be, say, 100K simulations. Use a defaultdict to count the number of hands that result in each total score between 13 and 22 using the play_hand function defined earlier. Your code should produce output using the csv.writer object that corresponds to the table shown below. The rows correspond to the different strategies (stand on hard 13, stand on soft 13, etc.). The columns correspond to the count of all simulations that resulted in a total of that value. The "Bust" column will have the count for all hands greater than 21. Your code should not have zeros for all the cells. Strategy 13 14 15 16 17 18 19 20 21 Bust H13 S13 H14 $14 H15 S15 H16 S16 H17 $17 H18 H19 S19 H20 s20 Step 3: Play Hand Function In blackjack2.py, refactor the game play logic from Week 3 into a function defined as shown below. This function encapsulates the logic of playing a single hand and returns the final score. If the score is 22 or greater (i.e., bust), just return the value 22; otherwise, return the actual final total of the hand. The parameters of the play_hand function are the values read from the command line, as in Week 3. After doing this, adjust the main function code to utilize this function and make sure it works as before (i.e., as it was described for Week 3). def play_hand(stand_on_value, stand_on_soft): ## TODO: fill this in return total Step 4: Refactor Main In blackjack2.py, refactor the functionality so that it takes a single command line argument, which is the number of simulations to run per strategy. > python3 blackjack2.py 100000 The updated function should loop across all "stand on values" from 13-20 and both stand on soft and stand on hard, and run the specified number of simulations for each strategy, keeping track of the number of simulations that result in each hand's total value. For example, when the stand-on value is 13, and stand on soft is true, there will be, say, 100K simulations. Use a defaultdict to count the number of hands that result in each total score between 13 and 22 using the play_hand function defined earlier. Your code should produce output using the csv.writer object that corresponds to the table shown below. The rows correspond to the different strategies (stand on hard 13, stand on soft 13, etc.). The columns correspond to the count of all simulations that resulted in a total of that value. The "Bust" column will have the count for all hands greater than 21. Your code should not have zeros for all the cells. Strategy 13 14 15 16 17 18 19 20 21 Bust H13 S13 H14 $14 H15 S15 H16 S16 H17 $17 H18 H19 S19 H20 s20Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started