Download craps.py and die.py ( See below ). Play the craps game 10 times. Show the output with each win or loss together with statistics. The following is an output example: Enter the number of games:...


Download

craps.py

and

die.py

(See below). Play the craps game 10 times. Show the output with each win or loss together with statistics. The following is an output example:


Enter the number of games: 5


(3, 5) 8


(2, 6) 8



(2, 1) 3



(5, 2) 7



(5, 5) 10


(3, 2) 5


(6, 3) 9


(6, 5) 11


(6, 3) 9


(4, 6) 10



(1, 3) 4


(6, 5) 11


(1, 4) 5


(2, 6) 8


(6, 6) 12


(1, 2) 3


(3, 4) 7



The total number of wins is 3


The total number of losses is 2


The average number of rolls per win is 3.00


The average number of rolls per loss is 4.00


The winning percentage is 0.600




-----------------craps.py------------------------


"""



File: craps.py



This module studies and plays the game of craps.


"""



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))


            sum = v1 + v2


            if sum == 7:


                return False


            elif sum == 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():


    """Plays a number of games and prints statistics."""


    number = int(input("Enter the number of games: "))


    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 / number))



-----------------die.py------------------------



"""



File: die.py



This module defines the Die class.


"""



from random import randint



class Die(object):


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



    def __init__(self):


        """The initial face of the die."""


        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):


        return self._value



    def __str__(self):


        return str(self._value)


Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here