Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help in python (i wrote these classes below please feel free to change them). also check input code below die py code import random

please help in python (i wrote these classes below please feel free to change them).

also check input code below

image text in transcribedimage text in transcribed

die py code

import random

class Die:

def __init__(self, sides=6):

self.sides = 6 self.value = 0 #roll when defined def roll(self):

self.values = random.randint(1, self.sides) return self.value def __str__(self):

return str(self.value) def __lt__(self, other):

return self.value

return self.value == other.value and self.sides == other.sides def __sub__(self, other):

return self.value - other.value

player py code:

import die

class Player:

def __init__(self):

self.dice[die.Die(), die.Die(), die.Die()] self.points = 0 def get_points(self):

return self.points def roll_dice(self):

self.dice.sort() for d in self.dice:

d.roll_dice() def has_pair(self):

d = "" if self.dice[0] == self.dice[1] or self.dice[0] == self.dice[2] or self.dice[1] == self.dice[2]:

return True d += 1

return d def has_three_of_a_kind(self):

s = "" if self.dice[0] == self.dice[1] == self.dice[2]:

return True s += 3

return s def has_series(self):

b = "" c = "" d = "" e = ""

if self.dice[0] == 1 and self.dice[1] == 2 and self.dice[2] == 3:

return True b += 2

return b

if self.dice[0] == 2 and self.dice[1] == 3 and self.dice[2] == 4:

return True c += 2

return c

if self.dice[0] == 3 and self.dice[1] == 4 and self.dice[2] == 5:

return True d += 2

return d

if self.dice[0] == 4 and self.dice[1] == 5 and self.dice[6]:

return True e += 2

return e def __str__(self):

self.dice.sort() s = "" for i, d in enumerate(self.dice):

s += "D" + str(i + 1) + ": " + str(d) + " "

s += "Sum = " + str(self.dice[0] + self.dice[1] + self.dice[2])

if self.dice[0] == self.dice[1] == self.dice[2]:

s += "You threw three of a kind!!"

return s

check input code:

Functions: get_int(prompt) - returns a valid integer (positive or negative). get_positive_int(prompt) - returns a valid positive (>=0) integer. get_int_range(prompt, low, high) - returns a valid integer within the inclusive range. get_float(prompt) - returns a valid decimal value. get_yes_no(prompt) - returns a valid yeso input.

Usage: in your program, import the check_input module. Then call one of the methods using check_input.

Example Usage:

import check_input

val = check_input.get_int("Enter #:") print(val)

"""

def get_int(prompt): """Repeatedly takes in and validates user's input to ensure that it is an integer. Args: prompt: string to display to the user to prompt them to enter an input. Returns: The valid positive integer input. """ val = 0 valid = False while not valid: try: val = int(input(prompt)) valid = True except ValueError: print("Invalid input - should be an integer.") return val

def get_positive_int(prompt): """Repeatedly takes in and validates user's input to ensure that it is a positive (>= 0) integer. Args: prompt: string to display to the user to prompt them to enter an input. Returns: The valid integer input. """ val = 0 valid = False while not valid: try: val = int(input(prompt)) if val >= 0: valid = True else: print("Invalid input - should not be negative.") except ValueError: print("Invalid input - should be an integer.") return val

def get_int_range(prompt, low, high): """Repeatedly takes in and validates user's input to ensure that it is an integer within the specified range. Args: prompt: string to display to the user to prompt them to enter an input. low: lower bound of range (inclusive) high: upper bound of range (inclusive) Returns: The valid integer input within the specified range. """ val = 0 valid = False while not valid: try: val = int(input(prompt)) if val >= low and val

def get_float(prompt): """Repeatedly takes in and validates user's input to ensure that it is a float. Args: prompt: string to display to the user to prompt them to enter an input. Returns: The valid floating point input. """ val = 0 valid = False while not valid: try: val = float(input(prompt)) valid = True except ValueError: print("Invalid input - should be a decimal value.") return val

def get_yes_no(prompt): """Repeatedly takes in and validates user's input to ensure that it is a yes or a no answer. Args: prompt: string to display to the user to prompt them to enter an input. Returns: True if yes, False if no. """ valid = False while not valid: val = input(prompt).upper() if val == "YES" or val == "Y": return True elif val == "NO" or val == "N": return False else: print("Invalid input - should be a 'Yes' or 'No'.")

Create a dice game that awards the user points for pair, three-of-a-kind, or a series. Use the following class diagram for your program. Die class (die.py) - has two attributes: the number of sides of the die and the value of the rolled die. 1. init (self, sides=6) - assigns the number of sides from the value passed in. Set the returned value of roll(). 2. roll (self) - generate a random number between 1 and the number of sides and assign it to the Die's value. Return the value. 3. __str_ (self) - return the Die's value as a string. 4. _lt__(self, other) - return true if the value of self is less than the value of other. 5. ____ (self, other) - return true if both the values of self and other are equal. 6. _sub_ (self, other) - return the difference between the value of self and the value of other. Player class (player.py) - has two attributes: a list of 3 Die objects and the player's points. 1. init_(self) - constructs and sorts the list of three Die objects and initializes the player's points to 0 . 2. get_points(self) - returns the player's points. 3. roll_dice (self) - calls roll on each of the Die objects in the dice list and sorts the list. 4. has_pair (self) - returns true if two dice in the list have the same value (uses==). Increments points by 1 . 5. has_three_of_a_kind (self) - returns true if all three dice in the list have the same value (uses ==. Increments points by 3 . 6. has_series (self) - returns true if the values of each of the dice in the list are in a sequence (ex. 1,2,3 or 2,3,4 or 3,4,5 or 4,5,6 ) (uses ). Increments points by 2 . 7. _str_(self) - returns a string in the format: "D1=2, D2=4, D3=6". Main file (main.py) - has one function named take_turn that passes in a Player object. The take_turn function should: roll the player's dice, display the dice, check for and display any win types (pair, series, three-of-a-kind), and display the updated score. The main function should construct a player object, and then repeatedly call take_turn until the user chooses to end the game. Display the final points at the end of the game. Use the check_input module's get_yes_no function to prompt the user to continue or end the game. Use docstrings to document each class, method, and function. Example Output (user input in italics): -Yahtzee- D1=1D2=4D3=5D1=3D2=4D3=5Yougotaseriesof3! Aww. Too Bad. Score =3 Score =0 Play again? (Y/N):y Play again? (Y/N):g Invalid input - should be a 'Yes' D1=1 D2=1 D3=1 or 'No'. You got 3 of a kind! Play again? (Y/N):y Score =6 Play again? (Y/N):n D1=3D2=3D3=5 You got a pair! Game Over. Score =1 Final Score =6 Play again? (Y/N):y Notes: 1. You should have 4 different files: die.py, player.py, check_input.py, and main.py. 2. Check all user input using the get_yes_no function in the check_input module. 3. Do not create any extra methods or functions or add any extra parameters. 4. Please do not create any global variables or use the attributes globally. Only access the attributes using the class's methods. 5. Use docstrings to document the class, each of its methods, and the functions in the main file. See the lecture notes and the Coding Standards reference document for examples. 6. Place your names, date, and a brief description of the program in a comment block at the top of your main file. Place brief comments throughout your code. 7. Thoroughly test your program before submitting: a. Make sure that the user input is validated. b. Make sure that the dice values are sorted. c. Make sure that each of the win types are detected correctly. d. Make sure that the user is not awarded for both a pair and a three-of-a-kind simultaneously. e. Make sure that each of the win types awards the correct number f. of points. g. Make sure that the game continues and ends correctly. h. Make sure that the final score is displayed at the end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago