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 program to receive a series of numbers

Write a program to receive a series of numbers (including decimal) from the user until enter key is pressed. Process the input data and display number count, sum and average. Use proper data type and format.

  Create an html web document that displays the results

Search for and download a photograph of a jackrabbit. Create an HTML Web document that displays the results and plays the blended WAV document that you created in Topic 5. Add a headline "Mythological Jackalope Discovered!"

  Q1if we knew all the ecological social and competitive

q1if we knew all the ecological social and competitive forces that regulate populations and in reality we couldnt what

  Develop and test a python program

develop and test a python program to track student marks in the various assignments in a given unit of study. The time allocated for this project is 10 weeks.

  Create a custom python module

Write a program named program51.py that defines a value-returning function named cuber that returns both the surface area and volume of a cube. A cube is a rectangular prism with all sides equal.

  Whatever is done in excel you have to do it in python

Whatever is done in excel you have to do it in python. Like you have to categorize the tweets related to app and other. It should be done by using pandas

  Assignment work in python

Write a function interleave two pictures take the first 20 pixels from the first picture and then 20 pixels from the second picture and then the next 20 pixels from the first picture and then the next 20 pixels from the second picture and so on till ..

  Python program that reads in a series of positive integers

Write a Python program that reads in a series of positive integers and writes out theproduct of all the integers less than 25 and the sum of all the integers greater than or equal to 25. Use 0 as a sentinel value

  Find a python script

Find a python script

  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.

  Write a function trans(m) which returns the transpose

Write a function trans(M) which returns the transpose of an n-by-n matrix M. The matrix M is represented by a list of n lists, each of length n. Transposing M means that each M[i][j] is swapped (once!) with M[j][i].

  Write a python program to calculate the average temperature

Write a Python program to calculate the average temperature using a for loop. Finding the frequency distribution of the temperatures.

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