The play method in the Player class of the craps game plays an entire game without interaction with the user. Revise the Player class so that its user can make individual rolls of the dice and view the results after each roll. The Player class no longer accumulates a list of rolls, but saves the string representation of each roll after it is made. Add new methods rollDice, getNumberOfRolls, isWinner, and isLoser to the Player class. The last three methods allow the user to obtain the number of rolls and to determine whether there is a winner or a loser. The last two methods are associated with new Boolean instance variables (winner and loser respectively). Two other instance variables track the number of rolls and the string representation of the most recent roll (rollsCount and roll). Another instance variable (atStartup) tracks whether or not the first roll has occurred. At instantiation, the roll, rollsCount, atStartup, winner, and loser variables are set to their appropriate initial values. All game logic is now in the rollDice method. This method rolls the dice once, updates the state of the Player object, and returns a tuple of the values of the dice for that roll. Include in the module the playOneGame and playManyGames functions, suitably updated for the new interface to the Player class.   -----------------------------------------------------------------------------------   """ File: craps.py Project 9.6 This module studies and plays the game of craps. Refactors code from case study so that the user can have the Player object roll the dice and view the result. """ from die import Die class Player(object):     def __init__(self):         """Has a pair of dice and an empty rolls list."""         self.die1 = Die()         self.die2 = Die()         self.rolls = []     def __str__(self):         """Returns a string representation of the list of rolls."""         result = ""         for (v1, v2) in self.rolls:             result = result + str((v1, v2)) + " " +\                      str(v1 + v2) + "\n"         return result     def getNumberOfRolls(self):         """Returns the number of the rolls."""         return len(self.rolls)     def play(self):         """Plays a game, saves the rolls for that game,          and returns True for a win and False for a loss."""         self.rolls = []         self.die1.roll()         self.die2.roll()         (v1, v2) = (self.die1.getValue(),                     self.die2.getValue())         self.rolls.append((v1, v2))         initialSum = v1 + v2         if initialSum in (2, 3, 12):             return False         elif initialSum in (7, 11):             return True         while (True):             self.die1.roll()             self.die2.roll()             (v1, v2) = (self.die1.getValue(),                         self.die2.getValue())             self.rolls.append((v1, v2))             laterSum = v1 + v2             if laterSum == 7:                 return False             elif laterSum == initialSum:                 return True def playOneGame():     """Plays a single game and prints the results."""     player = Player()     youWin = player.play()     print(player)     if youWin:         print("You win!")     else:         print("You lose!") def playManyGames(number):     """Plays a number of games and prints statistics."""     wins = 0     losses = 0     winRolls = 0     lossRolls = 0     player = Player()     for count in range(number):         hasWon = player.play()         rolls = player.getNumberOfRolls()         if hasWon:             wins += 1             winRolls += rolls         else:             losses += 1             lossRolls += rolls     print("The total number of wins is", wins)     print("The total number of losses is", losses)     print("The average number of rolls per win is %0.2f" % \           (winRolls / wins))     print("The average number of rolls per loss is %0.2f" % \           (lossRolls / losses))     print("The winning percentage is %0.3f" % (wins*100 / number)+"%") def main():     """Plays a number of games and prints statistics."""     number = int(input("Enter the number of games: "))     playManyGames(number) if __name__ == "__main__":     main()   -----------------------------------------------------------------------------------   """ File: die.py This module defines the Die class. """ from random import randint class Die:     """This class represents a six-sided die."""     def __init__(self):         """Creates a new die with a value of 1."""         self.value = 1     def roll(self):         """Resets the die's value to a random number         between 1 and 6."""         self.value = randint(1, 6)     def getValue(self):         """Returns the value of the die's top face."""         return self.value     def __str__(self):         """Returns the string rep of the die."""         return str(self.getValue())

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

The play method in the Player class of the craps game plays an entire game without interaction with the user. Revise the Player class so that its user can make individual rolls of the dice and view the results after each roll. The Player class no longer accumulates a list of rolls, but saves the string representation of each roll after it is made.

Add new methods rollDice, getNumberOfRolls, isWinner, and isLoser to the Player class. The last three methods allow the user to obtain the number of rolls and to determine whether there is a winner or a loser. The last two methods are associated with new Boolean instance variables (winner and loser respectively). Two other instance variables track the number of rolls and the string representation of the most recent roll (rollsCount and roll). Another instance variable (atStartup) tracks whether or not the first roll has occurred.

At instantiation, the roll, rollsCount, atStartup, winner, and loser variables are set to their appropriate initial values. All game logic is now in the rollDice method. This method rolls the dice once, updates the state of the Player object, and returns a tuple of the values of the dice for that roll. Include in the module the playOneGame and playManyGames functions, suitably updated for the new interface to the Player class.

 

-----------------------------------------------------------------------------------

 

"""
File: craps.py
Project 9.6
This module studies and plays the game of craps.
Refactors code from case study so that the user
can have the Player object roll the dice and view
the result.
"""

from die import Die

class Player(object):

    def __init__(self):
        """Has a pair of dice and an empty rolls list."""
        self.die1 = Die()
        self.die2 = Die()
        self.rolls = []

    def __str__(self):
        """Returns a string representation of the list of rolls."""
        result = ""
        for (v1, v2) in self.rolls:
            result = result + str((v1, v2)) + " " +\
                     str(v1 + v2) + "\n"
        return result

    def getNumberOfRolls(self):
        """Returns the number of the rolls."""
        return len(self.rolls)

    def play(self):
        """Plays a game, saves the rolls for that game, 
        and returns True for a win and False for a loss."""
        self.rolls = []
        self.die1.roll()
        self.die2.roll()
        (v1, v2) = (self.die1.getValue(),
                    self.die2.getValue())
        self.rolls.append((v1, v2))
        initialSum = v1 + v2
        if initialSum in (2, 3, 12):
            return False
        elif initialSum in (7, 11):
            return True
        while (True):
            self.die1.roll()
            self.die2.roll()
            (v1, v2) = (self.die1.getValue(),
                        self.die2.getValue())
            self.rolls.append((v1, v2))
            laterSum = v1 + v2
            if laterSum == 7:
                return False
            elif laterSum == initialSum:
                return True

def playOneGame():
    """Plays a single game and prints the results."""
    player = Player()
    youWin = player.play()
    print(player)
    if youWin:
        print("You win!")
    else:
        print("You lose!")

def playManyGames(number):
    """Plays a number of games and prints statistics."""
    wins = 0
    losses = 0
    winRolls = 0
    lossRolls = 0
    player = Player()
    for count in range(number):
        hasWon = player.play()
        rolls = player.getNumberOfRolls()
        if hasWon:
            wins += 1
            winRolls += rolls
        else:
            losses += 1
            lossRolls += rolls
    print("The total number of wins is", wins)
    print("The total number of losses is", losses)
    print("The average number of rolls per win is %0.2f" % \
          (winRolls / wins))
    print("The average number of rolls per loss is %0.2f" % \
          (lossRolls / losses))
    print("The winning percentage is %0.3f" % (wins*100 / number)+"%")

def main():
    """Plays a number of games and prints statistics."""
    number = int(input("Enter the number of games: "))
    playManyGames(number)

if __name__ == "__main__":
    main()
 
-----------------------------------------------------------------------------------
 
"""
File: die.py

This module defines the Die class.
"""

from random import randint

class Die:
    """This class represents a six-sided die."""

    def __init__(self):
        """Creates a new die with a value of 1."""
        self.value = 1

    def roll(self):
        """Resets the die's value to a random number
        between 1 and 6."""
        self.value = randint(1, 6)

    def getValue(self):
        """Returns the value of the die's top face."""
        return self.value

    def __str__(self):
        """Returns the string rep of the die."""
        return str(self.getValue())
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education