Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

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.

Python, Programming

  • Category:- Python
  • Reference No.:- M92313628

Have any Question?


Related Questions in Python

Lab assignment -background - we have discussed in detail

Lab Assignment - Background - We have discussed, in detail, the function of Stacks and Queues and how they are specifically implemented in Python. To get a better understanding of the utility of these data structures, we ...

Question a software company sells a package that retails

Question : A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program usi ...

Foundations of programming assignment - feduni bankingthis

Foundations of Programming Assignment - FedUni Banking This assignment will test your skills in designing and programming applications to specification. Assignment Overview - You are tasked with creating an application t ...

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Simple python traffic lightswrite a program that simulates

Simple Python (Traffic lights) Write a program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and onl ...

Sieve of eratosthenes in pythonthe goal is to find all the

Sieve of Eratosthenes (in Python) The goal is to find all the prime numbers less than or equal to some natural number maxn. We have a list that tells us if any of the numbers 0..maxn are "marked". It can be an array of b ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As