Not logged in. Login

Assignment 3

For this assignment, you will have to use the Python Standard Library Reference. As part of this course, you are expected to be able to use documentation such as this.

Part I: Warmup

  1. In a file guess.py, implement the guessing game described in question 4 of assignment 1. Use a function from the random module to generate the random number. [You're free to deviate from your pseudocode from assignment 1 as necessary to implement a correct game.]

  2. In lists.py, start by writing a function that takes one floating point number x as its argument and returns \(\sqrt{|x|} + 5x^3\). The math module has a square root function.

    Using a loop, read 5 floating point values from the user into a list. Apply the function you wrote to each value in the list and print the results in reverse order.

    Enter 5 values:
    2
    3
    4
    5
    6
    1082.44948974 627.236067977 322.0 136.732050808 41.4142135624
    

    Or another run of the program:

    Enter 5 values:
    1.2
    -2
    10
    0
    2.41
    71.5400224696 0.0 5003.16227766 -38.5857864376 9.73544511501
    

Part II: Programming

For this assignment, your job is to create a program that plays (a simplified version of) blackjack. Name your program blackjack.py.

The Rules

For simplicity, we won't be implementing the full set of blackjack rules, but will get the core of the game working:

  1. A regular deck of 52 cards is used. The cards are every combination of 13 “ranks” (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace) and 4 “suits” (spades, clubs, diamonds, hearts). The deck is shuffled (put into a random order) before the game begins.

  2. In our game, each player starts with $1000. At the start of the game, ask how many people wish to play.

  3. Before each turn, each player makes a bet. They can risk any amount of their money on each turn.

  4. Two cards are dealt (given) to each player and placed face-up on the table. The dealer also receives two cards, but one is kept face-down (the “hole card”). The rest of the cards form the “shoe”.

  5. The object of the game is to get cards that have value as close as possible to 21 without going over (“busting”). The cards in a players hand have point values as follows.

    Card Rank Value
    2–10 the card's face value (2–10)
    J, Q, K 10
    A 1 or 11, whichever is better
  6. Each player gets a turn to take additional cards from the shoe. A player can take another card (“hit”) as many times as they like. When they stop taking new cards (“stand”), their turn is over. The cards in the player's hand will not change after their turn is over.

  7. Once the players have taken their turns, the dealer reveals his/her hole card. The dealer also has the chance to hit and stand, but they have a more rigid rule to follow. The dealer must hit (take another card) if their hand contains 16 points or less, and stand (stop taking cards) if they have 17 points or more.

  8. Once all of the cards have been played, each player either wins or loses depending on what is in their hand and the dealer's hand.

    Player Busts Player Doesn't Bust
    Dealer Busts player loses player wins
    Dealer points < Player points player loses player wins
    Dealer points == Player points n/a push
    Dealer points > Player points n/a player loses
  9. If the player wins, they get their bet back plus an equal amount from the dealer. If they lose, they lose the amount of money they bet.

  10. A “push” is a tie. In that case, the player takes his/her money back and doesn't win or lose anything. [In true blackjack, there is an additional set of outcomes that occur when either the player or dealer has “blackjack”: 21 points with two cards. We won't worry about those rules here.]

  11. After the turn is played and the bets are settled, they play continues with another round (go back to step 3).

  12. In a true game of blackjack, players can join or leave the game between hands. We won't worry about that. For this game, all of the players will play three hands, and then the game will stop.

Implementing

To help you along, a cards module has been provided. You'll have to download the cards module. Save this file in the same directory as your blackjack.py file and you should be able to import cards.

You must use Card objects as provided to represent the cards in the deck and players' hands. The deck and hands should be lists of Card objects. There are several functions in the cards module that will help you complete the assignment; they are described in the module documentation.

Several hints have been provided to help you find your way through this assignment. The hints don't say much about breaking your code into functions: deciding how to do that is part of the assignment.

Output

Here is a short sample of what the program should look like when it runs.

How many players are there? 2

Player #1:
You have $1000.
How much do you want to bet? 100
Player #2:
You have $1000.
How much do you want to bet? 200

Hands:
  Dealer:    ** 4H
  Player #1: 2D JH
  Player #2: 9D TS

Player #1
Cards: 2D JH, total: 12
Hit (h) or stand (s)? h
Cards: 2D JH 8H, total: 20
Hit (h) or stand (s)? s

Player #2
Cards: 9D TS, total: 19
Hit (h) or stand (s)? s

Dealer draws 4S.
Dealer's hand: QC 4H 4S, total: 18
Player #1 wins $100.
Player #2 wins $200.
Press enter to continue.

Player #1:
You have $1100.
How much do you want to bet? 100
Player #2:
You have $1200.
How much do you want to bet? 200

Hands:
  Dealer:    ** 8D
  Player #1: JC 6C
  Player #2: 6H KC

Player #1
Cards: JC 6C, total: 16
Hit (h) or stand (s)? s

Player #2
Cards: 6H KC, total: 16
Hit (h) or stand (s)? h
Cards: 6H KC KD, total: 26
Bust!

Dealer draws 5C.
Dealer's hand: 4D 8D 5C, total: 17
Player #1 loses $100.
Player #2 loses $200.
Press enter to continue.

Player #1:
You have $1000.
How much do you want to bet? 500
Player #2:
You have $1000.
How much do you want to bet? 200

Hands:
  Dealer:    ** 6S
  Player #1: 7S JS
  Player #2: 5D 5S

Player #1
Cards: 7S JS, total: 17
Hit (h) or stand (s)? s

Player #2
Cards: 5D 5S, total: 10
Hit (h) or stand (s)? h
Cards: 5D 5S 9H, total: 19
Hit (h) or stand (s)? s

Dealer draws QH.
Dealer's hand: 3C 6S QH, total: 19
Player #1 loses $500.
Player #2 pushes.
Press enter to continue.

Player 1: $500
Player 2: $1000

There is another page of sample runs that you can look at as well.

When you play the game, it should look like the given examples (i.e. all the messages, prompts, etc. should be the same).

Notes

Some things to keep in mind:

  • Remember that every time a player takes a card from the deck, it is removed from the deck.
  • An ace can count for either 11 or 1 in a hand. When calculating the value of a hand, make sure you use the best one.
  • The user should be able to enter their choice in lower- or uppercase. So, entering s and S should do the same thing. Hint: convert their input to uppercase immediately with the string's upper method.
  • You won't want to play the full game all of the time while testing. Press control-C to stop the program.

Break up the program into subtasks placed into functions as appropriate. You should do that whenever you think it's useful.

Your code should be easy to read and understand. Style will be marked more closely on this assignment.

Submitting

Submit all of your work through CourSys.

Updated Thu June 27 2024, 14:30 by ggbaker.