Dice Poker(Class Project)



Again I have another project that the instructions make absolute no sense to me. Here are the instructions:

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.

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.
Define a class named Dice.
Define an init method that has one parameter named "self."
In the init method, create an instance variable named dice that holds an empty list.
Create a loop that will execute its body 5 times.
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.
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.
hand = Dice() # Creates a Dice object
print hand.dice # Prints the instance variable dice (5 random numbers)
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.
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.
Create a for loop that will traverse through the parameter list.
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.
To test that the new method works properly, add the following lines of code to the driver code from earlier:
hand.roll([0,2,3]) # Change the numbers in index positions 0, 2, and 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.
Define a method named score that takes no arguments (must have parameter self).
Create a list named counts that has 7 zeros. Remember that index zero won't be used because dice have numbers 1-6.
Create a loop that traverses through the dice list.
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.
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
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.
Add another elif for Full House. Hint: You must check for 3 in counts and 2 in counts.
Add another elif for Three of a Kind.
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.
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:
Add an else as the last option that returns "Garbage", 0
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.
print hand.score() # Print the type of hand and its value

This is what I have so far:

import random
class Dice():
    def __init__(self):
        dice = []
    for x in range(0, 6):
        self.dice.append(random.randrange(1, 6)
    def roll(hold):

(For some reason this won't format correctly, also could someone just give me the complete way to do this because I am really not understanding this project)


Just giving you the solution won't help you in any way. Please elaborate on what your problem is exactly. What part of the instructions is unclear to you?


I am having issues with the loops and all of the 3rd part