Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I really need help on this. Create a new class called Die in a file called die.py Code for die.py import random # die.py

Hello I really need help on this. Create a new class called Die in a file called die.py

Code for die.py

import random

# die.py class to represent a single die

class Die:

def__init__(self,sides):

self.__numSides = sides

self.__value = self.roll()

def roll( self ):

self.__value = random.randint( 1, self.__numSides )

return self.__value

def getValue( self ):

return self.__value

def setValue( self, val ):

if val > 0 and value <= self.__numSides:

self.__value = val

return True # successfully set the value

return False; # failed the test

def __str__( self ):

return D + str( self.__numSides ) + = + str( self.__value )

Overload operators

# Overload + operator to sum two dice

def __add__ ( self, other ):

return self.__value + other.__value

# Overload > operator to compare two dice

def __gt__ ( self, other ):

return self.__value > other.__value

# Overload < operator to compare two dice

def __lt__ ( self, other ):

return self.__value < other.__value

# Overload == operator

def __eq__ ( self, other ):

return self.__value == other.__value

then add an equals method by overloading the == operator. Then create a second file for your main program.

The player rolls a set of 3 dice. If two of the dice are the same then the player receives 1 point for a pair. If all of the dice are the same, then the player receives 3 points for 3-of-a-kind (note: if a player gets a 3-of-a-kind they shouldnt also get points for having a pair). If the dice values are in a series, then the player receives 2 points (ex. 1-2-3, 3-2-1, 2-1-3, or any other order of these values would be considered a series).

Create a list of three Die objects and a variable for the players points in your main program. Make three functions that pass in the list of dice to check for each of the three different win types that use the Dies overloaded ==, >, and/or < operators, return the number of points if they met the win condition. Add a display function that passes in the list of dice and displays their values by calling the Dies string method. Create a function thats called takeTurn that passes in the list of dice, rolls each of the dice, displays their values by calling the display function you wrote, determines if the player met any of the win conditions using the functions you wrote, and then returns the points back to the main function where it is added to their point total.

Display a running sum of their points and ask the player if they want to continue playing after every roll, display their final points when they decide to quit. Write a function to take in the users input and check it for valid inputs (Y/N or y/n).

Example Output:

Yacht-z

Rolling Dice... 1D6 = 2, 2D6 = 4, 3D6 = 3

You got a series of 3!

Score = 2 points.

Play again? (Y/N) Y

Rolling Dice... 1D6 = 3, 2D6 = 1, 3D6 = 1

You got a pair! Score = 3 points.

Play again? (Y/N) Y

Rolling Dice... 1D6 = 4, 2D6 = 4, 3D6 = 4

You got 3 of a kind!

Score = 6 points.

Play again? (Y/N) Y

Rolling Dice... 1D6 = 3, 2D6 = 6, 3D6 = 1

Awww. Too Bad.

Score = 6 points.

Play again? (Y/N) N

Final Score = 6 points

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

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

More Books

Students also viewed these Databases questions