Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a new Python script called blackjack.py and set it up as a module. Initialize it with the following code: import random def get_card(): return

Create a new Python script called blackjack.py and set it up as a module. Initialize it with the following code:

import random def get_card(): return random.randint(1,13)

This function will return a random value between 1 and 13 (ends included). We are modeling a card as an integer value between 1 and 13 (1 = ace, 210 number cards, {11,12,13} are jack, queen, king). We do not represent the suit since it does not matter in the game of Blackjack.

Note also that we are not representing a deck of cards (i.e., a collection of 52 cards with four of each rank). The code above essentially assumes an infinite deck of cards, which will change the calculated probabilities slightly compared to what you might find online.

Finally, create a test suite: test_blackjack.py and set it up as a unittest test case.

Step 1: Score Function

In blackjack.py, implement a function called score:

def score(cards): ## TODO: fill this in return (total, soft_ace_count) 

The argument to this function is a list of integers representing the cards in a Blackjack hand. This function returns a tuple. The first element of the tuple is the total value of the hand according to the scoring rules of Blackjack (see above; this is not simply the sum of the integers in the cards list). The second element of the tuple is the number of soft aces that remain in the hand after doing any conversions from 111 to keep the hand from going bust.

Some examples of card lists on the left and the corresponding tuple values on the right:

[ 3, 12 ](13, 0) [ 5, 5, 10 ](20, 0) [ 11, 10, 1 ](21, 0) [ 1, 5 ](16, 1) [ 1, 1, 5 ](17, 1) [ 1, 1, 1, 7 ](20, 1) [ 7, 8, 10 ](25, 0)

Add test cases to test_blackjack.py that correspond to the above examples and any other test cases that validate your implementation.

Step 2: Stand Function

In blackjack.py, implement a function called stand:

def stand(stand_on_value, stand_on_soft, cards): total, soft_ace_count = score(cards) ## TODO: fill this in return True or False

The argument to this function is the stand-on value (e.g., 17), 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. This function returns True if the player would stand on the hand, given the policy, and False otherwise. As you can see, the first thing this function does is call the score function. Fill in the rest of stand so that it returns the correct Boolean value.

Add test cases to test_blackjack.py to validate your implementation.

Step 3: Main Function

In blackjack.py, implement a main function that is called when the module is executed as a program (and not imported). The program should take three arguments:

usage: blackjack.py <'soft'|'hard'>

The first argument is an integer specifying the number of simulations to run (should be greater than zero). The second value is an integer between 1 and 20 that indicates the stand-on value. The last argument is either the word soft or hard, indicating which strategy to employ.

At the top of the main function, perform basic validation on the arguments and raise a ValueError exception if anything is incorrect. This is also a good place to convert the arguments from str to the data type you will use in the rest of the program.

The rest of the main function is a loop for num-simulations that initializes a list with two cards (use get_card function) and then keeps adding cards to the list according to the return value of the stand function implemented above. The output of the program is the percentage of simulations for which the hand was bust. For example:

> python3 blackjack.py 10000 12 hard 0.0

the language is python3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions