Due by 11:59pm Tuesday, October 31
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.
Turn in your functions dicetest, roll, doubles, threekind, countSteps, and search from Lab 8.
In addition, turn in ONE of the following programs from Lab 8 (your choice):
For this program, you will need to write the following four functions:
isGameOver(stones) checks to see if the game is over. It takes the current number of stones remaining in the pile as an input argument, and returns True if zero stones remain, or False otherwise.
isGameOver(3) should return False
isGameOver(0) should return True
isGameOver(1) should return False
nextTurn(currentPlayer) takes a string representing the player who is currently taking a turn, and returns the next player.
nextTurn("Alice") should return 'Bob'
nextTurn("Bob") should return 'Alice'
getChoice(currentPlayer, stones) takes the player whose turn it is, and the number of remaining stones as input arguments. It asks the player how many stones they want to take, as shown below. If the player enters an invalid choice, the function prints an error message and asks the player to re-enter the value (as many times as necessary). After the player enters a valid response, the function returns the number of stones the player chose to take. Your function's output should match the example output shown below.
>>> getChoice("Alice", 9) Alice, it is your turn. There are 9 stones left. How many stones will you take? 5 Sorry, you cannot take more than 3 stones. Alice, it is your turn. There are 9 stones left. How many stones will you take? 0 Sorry, you must take at least one stone. Alice, it is your turn. There are 9 stones left. How many stones will you take? 3 (3 is returned) >>> getChoice("Bob", 2) Bob, it is your turn. There are 2 stones left. How many stones will you take? 3 Sorry, you cannot take more than 2 stones. Bob, it is your turn. There are 2 stones left. How many stones will you take? 7 Sorry, you cannot take more than 2 stones. Bob, it is your turn. There are 2 stones left. How many stones will you take? 0 Sorry, you must take at least one stone. Bob, it is your turn. There are 2 stones left. How many stones will you take? 1 (1 is returned)
stones() is the function that plays the game. This function implements the main game loop. It should keep track of the current player and the number of remaining stones. The game loop should quit when isGameOver returns True. On each turn, the current player should choose a legal number of stones using getChoice. Those stones should be removed from the pile of remaining stones, and the current player should be updated using nextTurn. You should test the game by playing multiple times. Make sure that your program reproduces the output below:
>>> stones() Welcome to the Game of Stones! Alice, it is your turn. There are 21 stones left. How many stones will you take? 3 Bob, it is your turn. There are 18 stones left. How many stones will you take? 9 Sorry, you cannot take more than 3 stones. Bob, it is your turn. There are 18 stones left. How many stones will you take? 0 Sorry, you must take at least one stone. Bob, it is your turn. There are 18 stones left. How many stones will you take? 3 Alice, it is your turn. There are 15 stones left. How many stones will you take? 2 Bob, it is your turn. There are 13 stones left. How many stones will you take? 5 Sorry, you cannot take more than 3 stones. Bob, it is your turn. There are 13 stones left. How many stones will you take? 3 Alice, it is your turn. There are 10 stones left. How many stones will you take? 3 Bob, it is your turn. There are 7 stones left. How many stones will you take? 3 Alice, it is your turn. There are 4 stones left. How many stones will you take? 2 Bob, it is your turn. There are 2 stones left. How many stones will you take? 3 Sorry, you cannot take more than 2 stones. Bob, it is your turn. There are 2 stones left. How many stones will you take? 0 Sorry, you must take at least one stone. Bob, it is your turn. There are 2 stones left. How many stones will you take? 1 Alice, it is your turn. There are 1 stones left. How many stones will you take? 2 Sorry, you cannot take more than 1 stones. Alice, it is your turn. There are 1 stones left. How many stones will you take? 1 No more stones remain! Alice took the last stone. Alice LOST. Bob wins. Bob is the AWESOMEST!
You should finish the other problems first before working on these.
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
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...
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']
Save all of your program definitions in a single Python file called assign8.py. Make sure to include your name and the assignment number in a comment at the top of your file. Submit your file electronically using the Homework Upload Site. Please DO NOT email your file to me.
If you have questions about anything, don't hesitate to ask!