Introduction to Computer Science II

Homework and Lab 1

Due by 3:10pm on Tuesday, January 13

Reading

Read chapter 7, the week 1 PPT.

Lab

Monday 1:30pm-3:00 in room 512 in 14 E Jackson or via Zoom.

Problems

Solve the following by implementing the corresponding functions in homework1.py. When done, submit that file through D2L.


1.    Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll sums up to 2, 3, or 12, the player loses. If the roll is 7 or 11, the player wins. Any other initial roll causes the player to "roll for point" which means that the player keeps rolling the dice until either rolling a 7 or re-rolling the value of the initial roll. If the player re-rolls the initial value before rolling a 7, it's a win for the player. Rolling a 7 first is a loss. Write a program that will simulate multiple games of craps and return the fraction of games that the player won. You should organize your program as follows:
  1. Write a function forPoint(roll) that takes as input an initial roll (i.e. an integer between 2 and 12) and repeatedly generates rolls of a pair of dice until either the value of the roll is 7 (in which case 0 is returned) or the value of the roll is the initial roll (in which case 1 is returned).
  2. Write a function craps() that takes no argument and simulates one game of craps. The program will first roll a pair of dice.  If the value (sum of two dice) of the roll obtained is 7 or 11, then 1 is returned by craps(). If the value obtained is 2, 3 or 12, then 0 is returned by craps(). For all other roll values, forPoint(value) is called and the value returned by it is returned by craps(). In other words, craps() returns 1 if the player won, 0 otherwise.
  3. Write a function testCraps(n) that takeas a positive integer n as input, simulates n games of craps and returns the fraction of games the player won.
Usage:
>>> forPoint(2)
0
>>> forPoint(2)
0
>>> forPoint(2)
1
>>> craps()
1
>>> craps()
0
>>> testCraps(10000)
0.4806
>>> testCraps(10000)
0.4978
>>> testCraps(10000)
0.4901


2.    You will write the function baccarat() that simulates a game of baccarat, another classic card game played in casinos. The game is similar to blackjack in that the player plays agains the house.

Cards in baccarat have values as follows: all cards numbered 2 through 9 are counted at face value, cards 10, J, Q, and K have value 10 and Aces count as 1. The total value of a hand is obtained by adding up the values of the cards in the hand modulo 10. In other words, if the cards in the hand total a two-digit number, only the last digit counts. Example: a hand with two cards having rank 5 and 8 has value 3 since 5 + 8 = 13. The goal of the game is to have a hand whose total value is closest to 9. Ties are possible. Here is how the game is played:
  1. At the beggining, two cards are dealt from the deck to the player and two cards are dealt to the house. The third card drawing rules for the player and the house are entirely rule based and dictate the whole game.
  2. If either the player or the house has a hand with total value 8 or 9 (called a "natural") no one draws a third card and the game is decided based on the current hand values (example 1 below). The below rules are applied only if neither the player or the house has a natural.
  3. If the player has a hand with total value 6 or 7, the player stands (i.e., does not draw a third card); in this case the house draws a third card only if its current hand has total value that is one of 0,1,2,3,4, or 5. The game can now be decided (example 2 below which results in a tie). The below rules are applied only if the player has a hand whose total value is 0,1,2,3,4, or 5.
  4. The player draws a third card. The house will draw a third card if one of the following is true:
    1. The house has a hand with total value 0,1, or 2 (example 4 below).
    2. The house has a hand with total value 3 and the third card just drawn by the player has a value that is not 8 (i.e. its value is one of 1,2,3,4,5,6,7,9,10). (Example 3 below shows the case when the house does not draw a third card because the player's third card has a value of 8.)
    3. The house has a hand with total value 4 and the third card just drawn by the player has a value that is one of 2,3,4,5,6,7 (example 5 below).
    4. The house has a hand with total value 5 and the third card just drawn by the player has a value that is one of 4,5,6,7.
    5. The house has a hand with total value 6 and the third card just drawn by the player has a value that is one of 6,7.
    The game can now be decided.
Note that I am providing you with helper functions newDeck, shuffle, dealCard, value, total, and compareHands. Use them.
Usage:
>>> baccarat()
 House:    9 ♣    J ♠
Player:    A ♡    2 ♡
House wins
>>> baccarat()
 House:    8 ♣    8 ♠
Player:    3 ♡    3 ♢
Tie
>>> baccarat()
 House:    K ♠    3 ♣
Player:    6 ♣    8 ♣
Player draws     8 ♡
House wins
>>> baccarat()
 House:   10 ♠    J ♢
Player:   10 ♢    4 ♠
Player draws     J ♣
House draws     6 ♢
House wins
>>> baccarat()
 House:    9 ♠    5 ♡
Player:    7 ♢    4 ♢
Player draws     4 ♣
House draws     J ♡
Player wins

3.
    [Lab exercise] You will write the necessary assert statements to verify that the deck is a valid deck of cards. That means verifying that the object returned by newDeck() is a list of 52 items with no duplicates and that each item in the list is a string with a valid suit (i.e., one of these unicode characters:  '\u2660', '\u2661', '\u2662', '\u2663') as the last character and a valid rank (i.e., one of these strings: '2','3','4','5','6','7','8','9','10','J','Q','K','A') as a the substring up to but not including the second to last character. (Hint: use a set to verify that the list has no duplicates.)




4.    [Lab exercise] You will write the necessary assert statements to verify that the total function returns an integer in the range from 0 to 9 inclusive. You will do so by generating 10 hands of two cards and verifying the total for each.