Exemplo n.º 1
0
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!"   
Exemplo n.º 2
0
def playManyGames():
    """Plays a number of games and prints statistics."""
    gewd = False
    #Filter input
    while not gewd:
        number = raw_input ( "Enter the number of games you'd like to play: " )
        #Check to see if the input is actually a character/string or negative number(zero is also included to prevent divide by zeros.)
        if number.isalpha() or number == '' or int(number) <= 0:
            print "Incorrect input detected!"
        #Numbers greater than or equal to zero zero
        else:
            #convert the string to an integer
            number = int(number)
            gewd = True
    wins = 0
    losses = 0
    winRolls = 0
    lossRolls = 0
    player = Player()
    for count in xrange(number):
        hasWon = player.play()
        rolls = player.getNumberOfRolls()
        if hasWon:
            wins += 1
            winRolls += rolls
            print wins, winRolls
        else:
            losses += 1
            lossRolls += rolls
            print losses, lossRolls
    #Avoid divide by zeros
    if wins > 0:
        print "The total number of wins is", wins
        print "The average number of rolls per win is %0.2f" % \
              (float(winRolls) / wins )
    else:
        print "There were no wins to report."
    #Avoid divide by zeros
    if losses > 0:
        print "The total number of losses is", losses
        print "The average number of rolls per loss is %0.2f" % \
              (float(lossRolls) / losses)
    else:
        print "There were no losses to report."
    
    print "The winning percentage is %0.3f" % \
          (float(wins) / number)
Exemplo n.º 3
0
def play_and_print():
    # initialize a player
    player = Player()

    # play a new game
    win_ = player.play()
    v1, v2 = player.getLastRoll()

    # texts (information about the roll)
    text = Text(Point(25, 25), "Dice 1: {0}".format(v1))
    text.draw(win)

    text = Text(Point(25, 45), "Dice 2: {0}".format(v2))
    text.draw(win)

    text = Text(Point(25, 65), "Sum: {0}".format(v1 + v2))
    text.draw(win)

    if win_:
        text = Text(Point(25, 85), "Won")
    else:
        text = Text(Point(25, 85), "Lost")
    text.draw(win)
# JTSK-350112
# a2 2.py
# Wossen Hailemariam
# [email protected]

from die import Die
from craps import Player
from craps import playOneGame
from craps import playManyGames

obj = Player()

print(obj.__init__())
print(obj.__str__())

print(obj.getNumberOfRolls())
print(obj.play())