+1 (315) 557-6473 

Python Program to Create Black Jack Strategy Comparison Assignment Solution.


Instructions

Objective
Write a python assignment program to create black jack strategy comparison.

Requirements and Specifications

program to create black jack strategy comparison in python
program to create black jack strategy comparison in python 1

Source Code

from operator import ge

import random

import sys

import csv

from collections import namedtuple

def get_card():

'''This function returns a random value between 1 and 13 '''

return random.randint(1, 13)

def score(hand):

''' This function takes a list of numbers and results in a tuple

where the 1st element is the total value of the blackjack hand.

The second element is the number of soft aces present (if any)

'''

#Begin with 0 as our total - no cards have been dealt out

Score = namedtuple('Score', 'total soft_ace_count')

total = 0

soft_ace_count = 0

#accounts if there are aces present

for card in hand:

if card == 1:

soft_ace_count += 1

total += 11

#For the jacks(11), queens(12), and kings(13) having a value of 10

elif card > 10:

total += 10

else:

total += card

#If aces are drawn can be "soft"

#Ex:value of the Ace can be 1 or 11

while total > 21 and soft_ace_count > 0:

total -= 10

soft_ace_count -= 1

return Score(total, soft_ace_count)

def stand(stand_on_value, stand_on_soft, cards):

''' The function will return a Boolean value indicating

whether the player will stand on

a “soft” hand or just on a “hard” hand,

and a list of integers representing the cards in a Blackjack hand.

True IF the player decides to STAND on their hand.

Otherwise, the function will return False IF the player decides to HIT

'''

Stand = namedtuple('Stand','stand total')

total, soft_ace_count = score(cards)

if total >= stand_on_value:

if soft_ace_count > 0 and total == stand_on_value:

if stand_on_soft == True:

return Stand(True, total) #stand

else:

return Stand(False, total) #hit

else: #total >= stand value and no soft ace

return Stand(True, total) #stand

else:

return Stand(False, total) #hit

def play_hand(stand_on_value, stand_on_soft):

bust_count = 0

; hand = [get_card(), get_card()] #im getting 2 cards!

getscore = score(hand)

while score(hand)[0] < 21:

if stand(stand_on_value, stand_on_soft, hand)[0] == True:

# "STAND"

return score(hand)

else:

hand.append(get_card()) # add a card

if score(hand)[0] > 21:

# "BUST"

bust_count += 1

return score(hand)

elif score(hand)[0] == 21:

# "WIN"

return score(hand)

else:

# "Take it Back Now Y'all"

continue

return score(hand)

def main():

# blackjack.py <'soft'|'hard'>

#process inputs

length_of_argv = len(sys.argv)

if length_of_argv < 2:

raise ValueError()

num_simulations = int(sys.argv[1])

if num_simulations < 1:

raise ValueError()

stand_on_value = 21

firmness = 'soft'

if firmness == 'soft':

stand_on_soft = True

elif firmness == 'hard':

stand_on_soft = False

else:

raise ValueError()

#run simulations

totalbust = 0

for simnum in range(num_simulations):

result = simulate(stand_on_value,stand_on_soft)

if not result:

totalbust += 1

#calculate final output bust percentage & print

percentagebust = totalbust / num_simulations

print(percentagebust)

def simulate(stand_on_value,stand_on_soft):

hand = [get_card(), get_card()] #im getting 2 cards!

"""while not stand(stand_on_value, stand_on_soft, hand): #while hit

hand.append(get_card()) #hit

if score(hand)[0] > 21: #checking results

return False #no bueno

else:

return True #you good"""

score = play_hand(stand_on_value, stand_on_soft)

return score[0] <= 21

if __name__ == '__main__':

main()