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.""" 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)
def __init__(self): """Creates the player, and sets up the Images and labels for the two dice to be displayed, the text area for the game state, and the two command buttons.""" EasyFrame.__init__(self, title="Craps Game") self.setSize(220, 320) """Instantiate the model and initial values of the dice""" # self.player self.player = Player() self.v1 = self.player.die1.getValue() self.v2 = self.player.die2.getValue() """Add labels and buttons to the view""" # self.dieLabel1 self.dieLabel1 = self.addLabel("", row=0, column=0, sticky="NSEW") # self.dieLabel2 self.dieLabel2 = self.addLabel("", row=0, column=1, sticky="NSEW") # self.stateArea self.stateArea = self.addTextArea("", row=1, column=0, columnspan=2, width=5, height=15) # self.rollButton self.rollButton = self.addButton(row=2, column=0, text="Roll", command=self.nextRoll) # self.addButton self.newGameButton = self.addButton(row=2, column=1, text="New game", command=self.newGame) self.refreshImages()
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)
class CrapsGUI(EasyFrame): def __init__(self): """Creates the player, and sets up the Images and labels for the two dice to be displayed, the text area for the game state, and the two command buttons.""" EasyFrame.__init__(self, title="Craps Game") self.setSize(220, 320) """Instantiate the model and initial values of the dice""" # self.player self.player = Player() self.v1 = self.player.die1.getValue() self.v2 = self.player.die2.getValue() """Add labels and buttons to the view""" # self.dieLabel1 self.dieLabel1 = self.addLabel("", row=0, column=0, sticky="NSEW") # self.dieLabel2 self.dieLabel2 = self.addLabel("", row=0, column=1, sticky="NSEW") # self.stateArea self.stateArea = self.addTextArea("", row=1, column=0, columnspan=2, width=5, height=15) # self.rollButton self.rollButton = self.addButton(row=2, column=0, text="Roll", command=self.nextRoll) # self.addButton self.newGameButton = self.addButton(row=2, column=1, text="New game", command=self.newGame) self.refreshImages() def nextRoll(self): """Rolls the dice and updates the view with the results.""" (self.v1, self.v2) = self.player.rollDice() self.refreshImages() def newGame(self): """Create a new craps game and updates the view.""" self.player = Player() self.v1 = self.player.die1.getValue() self.v2 = self.player.die2.getValue() self.refreshImages() def refreshImages(self): """Updates the images in the window.""" fileName1 = "DICE/" + str(self.v1) + ".gif" fileName2 = "DICE/" + str(self.v2) + ".gif" self.image1 = PhotoImage(file=fileName1) self.dieLabel1["image"] = self.image1 self.image2 = PhotoImage(file=fileName2) self.dieLabel2["image"] = self.image2
def newGame(self): """Create a new craps game and updates the view.""" self.player = Player() self.v1 = self.player.die1.getValue() self.v2 = self.player.die2.getValue() self.refreshImages()
# 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())