Define an init method that has one parameter named self in


Assignment: Dice Poker

Our goal is to write a game program that allows a user to play video poker using dice. The program will display a hand consisting of five dice. The basic set of rules is as follows:

? The player starts with $100.
? Each round costs $10 to play. This amount is subtracted from the user's money at the start of the round.
? The player initially rolls a completely random hand.
? The player gets two chances to enhance the hand by rerolling some or all of the dice.
? At the end of the hand, the player's money is updated according to the following payout schedule:

? Two Pairs = $5
? Three of a Kind = $8
? Full House (A Pair and a Three of a Kind) = $12
? Four of a Kind = $15
? Straight (1-5 or 2-6) $20
? Five of a Kind = $30

For this assignment you will build the Dice class that will handle all the aspects of the dice such as rolling the dice, keeping track of the values, and returning the score for the dice. Follow the steps below to complete this part of the assignment. The rest of the game will be completed in the next assignment.

1. The Dice class implements a collection of dice, which are just changing numbers. The obvious representation is to use a list of five ints. Our contructor needs to create a list and assign some initial values.

1. Define a class named Dice.

2. Define an init method that has one parameter named "self."

3. In the init method, create an instance variable named dice that holds an empty list.

4. Create a loop that will execute its body 5 times.

5. In the body of the loop append a random number from 1 to 6 to the list. Remember that to refer to an instance variable you begin with "self" and then a dot. Also remember that you must import the random library to use random functions.

6. To test that your code so far, add the following temporary driver code to the bottom of your file and run the program. Make sure you don't indent because this code is not part of the Dice class.

1. hand = Dice() # Creates a Dice object
2. print hand.dice # Prints the instance variable dice (5 random numbers)

2. We need a method to roll selected dice. We can specify which dice to roll by passing a list of indexes. For example, roll([0, 2, 3]) would roll the dice in positions 0, 3, and 4 of the dice list. We just need a loop that goes through the parameter list and generates a new random value for each listed position.

1. Define an instance method named "roll" that takes one argument (besides self). Give the parameter an appropriate name to hold the list of indexes we need to roll.

2. Create a for loop that will traverse through the parameter list.

3. Inside the loop, change the dice for each index value in the list to a random number from 1 to 6. Remember that to refer to the instance variable you must precede it with self and a dot.

4. To test that the new method works properly, add the following lines of code to the driver code from earlier:

1. hand.roll([0,2,3]) # Change the numbers in index positions 0, 2, and 3.

2. print hand.dice # Prints the instance variable dice (5 random numbers)

3. Finally, we need a method to that will determine the worth of the current dice. We need to examine the values and determine whether we have any of the patterns that lead to a payoff. Let's return a string labeling what the hand is and an int that gives the payoff amount. We need to check for each possible hand in a sensible order to guarentee the correct payout. For example, a full house also contains a three of a kind. We need to check for the full house before checking for three of a kind, since the full house is more valuable. One simple way of checking the hand is to generate a list of the counts of each value. That is, counts[i] will be the number of times that the value i occures in dice. If the dice are: [3,2,5,2,3] then the count list would be [0,0,2,2,0,1,0]. Notice that counts[0] will always be zero since dice values are in the range 1-6. Checking for various hands can then be done by looking for various values in counts. For example, if counts contains a 3 and a 2, the hand contains a triple and a pair; hence, it is a full house.

1. Define a method named score that takes no arguments (must have parameter self).

2. Create a list named counts that has 7 zeros. Remember that index zero won't be used because dice have numbers 1-6.

3. Create a loop that traverses through the dice list.

4. Inside the loop add one to the appropiate index value in the counts list for each value in the dice list. When the loop is done, the counts list should have the number of times each value occurs in the dice list.

5. Create an if statement the checks for the highest score, Five of a Kind. If any of the values in the counts list is 5, then return the string "Five of a Kind" and the value 30 since Five of a Kind pays $30. Here is the first line of the if statement: if 5 in counts: Here is the body of the if statement: return "Five of a Kind", 30

6. Add to the if statement by adding the next option using elif to check if there are any values of 4 in the counts list. If so, return the appropriate string and payout from the information at the beginning of this assignment.

7. Add another elif for Full House. Hint: You must check for 3 in counts and 2 in counts.

8. Add another elif for Three of a Kind.

9. Add another elif for a Straight. This is a little tricky so I will give you the setup for the elif and explain it: elif not (2 in counts) and (counts[1] == 0 or counts[6] == 0): Since we have already checked for 5, 4, and 3 of a kind, checking that there are no pairs guarantees that the dice show five distinct values. If there is no 6, then the values must be 1-5; likewise, no 1 means the values must be 2-6.

10. Add another elif for Two Pairs. This has a little trick too so I will give you the setup for the elif: elif counts.count(2) == 2:

11. Add an else as the last option that returns "Garbage", 0

12. To test that the code you created works properly, add the following code to the previous driver code. You also may want to create a loop around all the driver code so multiple examples are printed. You may need to run many times to finally get a Straight and Five of a Kind. Remember that in the final game, the user will be able to improve their hand two times before the value of the hand is calculated.

1. print hand.score() # Print the type of hand and its value.

Request for Solution File

Ask an Expert for Answer!!
Python Programming: Define an init method that has one parameter named self in
Reference No:- TGS02300457

Expected delivery within 24 Hours