Introduction to Computer Science: The Way of the Program — Homework 8

Due by 11:59pm Tuesday, October 31

Reading

AutoTester Program

An autotester program is available for Exercise 3 below (the Game of Stones). Download hw8tester.py and put it in the same folder as your assign8.py file, and put the line from hw8tester import * at the top of your file. Also make sure that autotester_base.py is in the same folder.


Programming Exercises

  1. Turn in your functions dicetest, roll, doubles, threekind, countSteps, and search from Lab 8.

  2. In addition, turn in ONE of the following programs from Lab 8 (your choice):

  3. The Game of Stones. Alice and Bob are life-long rivals and have agreed to meet and decide once and for all who is the best ever. To test their wits, determination, and awesomeness, they have agreed to play The Game of Stones. This game begins with a pile of 21 stones. Alice and Bob take turns. Each turn, a player may take either one, two or three stones. They must take at least one stone, and cannot take more than three stones. The player who takes the final stone loses the game and will never be able to program in Python ever again. Which player has the wits and determination to win? Who will be awesome enough to conquer the Game of Stones?

    For this program, you will need to write the following four functions:


EXTRA CREDIT PROBLEMS (OPTIONAL)

You should finish the other problems first before working on these.

  1. The dice game of Pig is played as follows. On each turn, a player repeatedly rolls a single six-sided die, adding each value rolled to their score for that turn, until they decide to stop rolling (which is called holding). If they decide to hold before rolling a 1, their total score for that turn is added to their overall cumulative score, and it becomes the next player's turn. However, if the player rolls a 1 before holding, their turn immediately ends and they receive 0 points for that turn. The first player to score 100 or more points wins. For example, the first player, Alice, begins a turn with a roll of 5. Alice could hold and score 5 points, but chooses to roll again. Alice rolls a 2, and could hold with a turn total of 7 points, but chooses to roll again. Alice rolls a 1, and must end her turn without scoring. The next player, Bob, rolls the sequence 4-5-3-5-5, after which he chooses to hold, and adds his turn total of 22 points to his score. (See the Wikipedia entry on Pig for more info.)

    Write a function called pigturn() to simulate a single turn of the game. The function should roll the die repeatedly, adding each value to the player's score for that turn and asking the player if they want to hold, until either a 1 is rolled, in which case the function immediately returns 0, or until the player decides to hold, in which case the player's accumulated score is returned. Some sample interactions are shown below. Your function should produce similar output.

    >>> pigturn()
    You rolled a 6 (6 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 3 (9 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 5 (14 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 3 (17 points so far)
    Type 'h' to hold or 'r' to roll again: h
    17
    >>> pigturn()
    You rolled a 5 (5 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 6 (11 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 1, so you get 0 points this turn
    0
    >>> pigturn()
    You rolled a 1, so you get 0 points this turn
    0
    
  2. Next, write a main program called pig() that simulates the following variant of Pig for a single player. The goal is for the player to accumulate 100 points using at most 10 turns. Your program should repeatedly call pigturn() as a helper function and keep track of the player's overall score and number of turns left. If the player's score reaches 100 or the number of turns left becomes 0, the program should end the game and print an appropriate message. Make sure that if the player reaches 100 points on the last turn, this is counted as a win and not a loss. Hint: use a while-loop with the compound condition while totalScore < 100 and turnsLeft > 0. Your program should produce output similar to the following:

    >>> pig()
    You rolled a 2 (2 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 6 (8 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 3 (11 points so far)
    Type 'h' to hold or 'r' to roll again: h
    You now have 11 points with 9 turns left
    
    You rolled a 3 (3 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 6 (9 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 6 (15 points so far)
    Type 'h' to hold or 'r' to roll again: h
    You now have 26 points with 8 turns left
    
    You rolled a 5 (5 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 2 (7 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 1, so you get 0 points this turn
    You now have 26 points with 7 turns left
    
    You rolled a 5 (5 points so far)
    Type 'h' to hold or 'r' to roll again: r
    You rolled a 1, so you get 0 points this turn
    You now have 26 points with 6 turns left
    
    You rolled a 3 (3 points so far)
    Type 'h' to hold or 'r' to roll again: h
    You now have 29 points with 5 turns left
    
    ...etc...
    
  3. In Poker, a "full house" is a hand with five cards, consisting of three-of-a-kind and two-of-a-kind. For example, three Kings and two 7's, or three 3's and two Aces. Write a program called fullhouse() that performs a simulation to estimate the probability of obtaining a full house when dealt five cards from a fresh, randomly-shuffled deck, and then reports the probability as a percentage to four decimal places. Hints: To represent the deck, create a list of 52 numbers and strings representing the ranks (2, 10, "Jack", "Ace", etc.) of each of the 52 cards in the deck, and then randomly select and remove cards from the deck. A function to remove duplicates from a list may be useful for checking whether a list of cards represents a full house, since a full house contains exactly two distinct card ranks (but be careful not to count a hand with four-of-a-kind and one other card as a full house). The random library function random.shuffle(list) can be used to randomly rearrange the elements of a list. For example:

    >>> cards = [2, 3, 4, 5, "Jack", "Queen", "King"]
    >>> print(cards)
    [2, 3, 4, 5, 'Jack', 'Queen', 'King']
    >>> random.shuffle(cards)
    >>> print(cards)
    [2, 'Jack', 4, 5, 'King', 3, 'Queen']
    

Turning in Your Homework