Define an init method that has one parameter named self

Assignment Help Python Programming
Reference no: EM131504899

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.

Reference no: EM131504899

Questions Cloud

Explain respiratory system and circulaatory system : Explain respiratory system and circulaatory system
Subject covered cyber bullying : Complete a 5-7 page final paper that analyzes the future impact of this social problem you have mentioned in the second writing assignment.
Expected return remained constant this announcement : What else must the company have also announced if its stock price and total expected return remained constant following this announcement?
Demonstrate an instance of static charge : Discuss the overall manner in which a magnetic reversal might impact human activities on Earth. Include one example of such impact to support your response.
Define an init method that has one parameter named self : 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.
Derive relationship between quantity and price of x : Derive the relationship between the quantity of X demanded and the price of X if the consumer's indifference map vis-à-vis X and Y has curves concave.
What is the longest bone in the hid limb muscles : What is the longest bone in the hid limb muscles that work together a junction where bones meet what make up most of the human skeleton
Preferred stock dividends : Preferred stock dividends:
A fully efficient market will eliminate : A fully efficient market will eliminate which one of the following?

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd