def main():
   """Terminal-based trace of a game of War."""
   game = WarGame()
   game.deal()
   while not game.winner():
      game.step()
      print(game)
   print(game.winner())
def main():
    """Terminal-based trace of a game of War."""
    game = WarGame()
    game.deal()
    while not game.winner():
        game.step()
        print(game)
    print(game.winner())
class WarGUI(EasyFrame):

   def __init__(self):
      """Creates the war game, and sets up the Images and labels
      for the two cards to be displayed, the state label,
      and the command button."""
      EasyFrame.__init__(self, title = "Let's play War!")
      self.setSize(300, 300)
      self.game = WarGame()
      self.game.deal()
      self.playerLabel1 = self.addLabel(row = 0, column = 0,
                                         text = "Player 1")
      self.playerLabel2 = self.addLabel(row = 0, column = 1,
                                         text = "Player 2")
      self.image1 = PhotoImage(file = Card.BACK_NAME)
      self.image2 = PhotoImage(file = Card.BACK_NAME)
      self.cardImageLabel1 = self.addLabel("", row = 1, column = 0)
      self.cardImageLabel1["image"] = self.image1
      self.cardImageLabel2 = self.addLabel("", row = 1, column = 1)
      self.cardImageLabel2["image"] = self.image2
      self.stateLabel = self.addLabel("", row = 2, column = 0,
                                      columnspan = 2)
      self.nextCardButton = self.addButton(row = 3, column = 0,
                                   text = "Next card",
                                   command = self.nextCard)
      self.newGameButton = self.addButton(row = 3, column = 1,
                                   text = "New Game",
                                   command = self.newGame,
                                    state = 'disabled')

   def nextCard(self):
      """Makes a move in the game and updates the view with
      the results."""
      (card1, card2) = self.game.step()
      self.image1 = PhotoImage(file = card1.fileName)
      self.cardImageLabel1["image"] = self.image1
      self.image2 = PhotoImage(file = card2.fileName)
      self.cardImageLabel2["image"] = self.image2
      self.stateLabel["text"] = str(self.game)
      if self.game.winner():
         self.messageBox("Game Is Over!", self.game.winner())
         self.nextCardButton["state"] = 'disabled'
         self.newGameButton["state"] = 'normal'

   def newGame(self):
      """Starts a new game when the button "New Game" is clicked after
      the end of the previous game."""
      self.game = WarGame()
      self.game.deal()
      self.image1 = PhotoImage(file = Card.BACK_NAME)
      self.image2 = PhotoImage(file = Card.BACK_NAME)
      self.cardImageLabel1["image"] = self.image1
      self.cardImageLabel2["image"] = self.image2
      self.stateLabel["text"] = str(self.game)
      self.nextCardButton["state"] = 'normal'
      self.newGameButton["state"] = 'disabled'
Exemple #4
0
class WarGUI(EasyFrame):

   def __init__(self):
      """Creates the war game, and sets up the Images and labels
      for the two cards to be displayed, the state label,
      and the command button."""
      EasyFrame.__init__(self, title = "Let's play War!")
      self.setSize(350, 350)
      self.game = WarGame()
      self.game.deal()
      self.playerLabel1 = self.addLabel(row = 0, column = 0,
                                         text = "Player 1")
      self.playerLabel2 = self.addLabel(row = 0, column = 1,
                                         text = "Player 2")
      self.image1 = PhotoImage(file = Card.BACK_NAME)
      self.image2 = PhotoImage(file = Card.BACK_NAME)
      self.cardImageLabel1 = self.addLabel("", row = 1, column = 0)
      self.cardImageLabel1["image"] = self.image1
      self.cardImageLabel2 = self.addLabel("", row = 1, column = 1)
      self.cardImageLabel2["image"] = self.image2
      self.stateLabel = self.addLabel("", row = 2, column = 0,
                                      columnspan = 2)
      self.nextCardButton = self.addButton(row = 3, column = 0,
                                   text = "Next card",
                                    command = self.nextCard)
      self.newGameButton = self.addButton(row = 4, column = 0,
                                          text = "New Game",
                                          state = "disabled",
                                          command = self.newGame)

   def nextCard(self):
      """Makes a move in the game and updates the view with
      the results."""
      (card1, card2) = self.game.step()
      self.image1 = PhotoImage(file = card1.fileName)
      self.cardImageLabel1["image"] = self.image1
      self.image2 = PhotoImage(file = card2.fileName)
      self.cardImageLabel2["image"] = self.image2
      self.stateLabel["text"] = str(self.game)
      if self.game.winner():
         self.messageBox("Game Is Over!", self.game.winner())
         self.nextCardButton = self.addButton(row = 3, column = 0,
                                   text = "Next card",
                                    state = "disabled",
                                    command = self.nextCard)
         self.newGameButton = self.addButton(row = 4, column = 0,
                                          text = "New Game",
                                          state = "normal",
                                          command = self.newGame)
         

   def newGame(self):
      """Starts a new game of War."""
      self.game = WarGame()
      self.game.deal()
      self.image1 = PhotoImage(file = Card.BACK_NAME)
      self.image2 = PhotoImage(file = Card.BACK_NAME)
      self.cardImageLabel1 = self.addLabel("", row = 1, column = 0)
      self.cardImageLabel1["image"] = self.image1
      self.cardImageLabel2 = self.addLabel("", row = 1, column = 1)
      self.cardImageLabel2["image"] = self.image2
      self.stateLabel["text"] = str()
      self.newGameButton = self.addButton(row = 4, column = 0,
                                          text = "New Game",
                                          state = "disabled",
                                          command = self.newGame)
      self.nextCardButton = self.addButton(row = 3, column = 0,
                                   text = "Next card",
                                    state = "normal",
                                    command = self.nextCard)