Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3-Classes Code for die.py from lecture notes import random # die.py class to represent a single die class Die: Constructor sets the initial values

Python 3-Classes

image text in transcribed

Code for die.py from lecture notes

import random

# die.py class to represent a single die

class Die:

Constructor sets the initial values of the data members (attributes of) the object.

def__init__(self,sides):

self.__numSides = sides

self.__value = self.roll()

Methods actions (or operations) the object can perform.

# Rolls the die and returns the result

def roll( self ):

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

return self.__value

Accessor retrieves the value of one of the data members.

# Returns the value of the die

def getValue( self ):

return self.__value

Mutator changes the value of one of the data members.

# Allows the die to be set to a selected value

def setValue( self, val ):

if val > 0 and value

self.__value = val

return True # successfully set the value

return False; # failed the test

To String returns the string representation of the object.

# Returns the string representation of the die

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

def __lt__ ( self, other ):

return self.__value

# Overload == operator

def __eq__ ( self, other ):

return self.__value == other.__value

Objects- don't know if this is needed for the project but just in case I'll add it here, if not just ignore it

import die

# game.py two players roll a die to see who rolls higher

p1 = die.Die( 6 )

p2 = die.Die( 6 )

p1.roll()

p2.roll()

print( p1 )

print( p2 )

if p1 > p2:

print( Player 1 Wins )

elif p1

print( Player 2 Wins )

else:

print( Tie )

print( The sum of the dice is:, p1 + p2 )

Yacht-z - Create a dice game that uses the Die class that was given in the lecture notes Create a new class called Die in a file called 'die.py', add the code from the lecture notes, and then add an equals method by overloading theoperator. 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 shouldn't 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 player's 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 Die's overloaded >, and/or

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_2

Step: 3

blur-text-image_3

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

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions

Question

(c) What is the complete defining relationship?

Answered: 1 week ago