100 likes | 190 Views
Computer Science 111. Fundamentals of Programming I Designing War. Application: The Game of War. A simplified version with two players Each player has 3 piles of cards: An unplayed pile A war pile A winnings pile. Playing the Game of War. Deal 26 cards to each player ’ s unplayed pile
E N D
Computer Science 111 Fundamentals of Programming I Designing War
Application: The Game of War • A simplified version with two players • Each player has 3 piles of cards: • An unplayed pile • A war pile • A winnings pile
Playing the Game of War Deal 26 cards to each player’s unplayed pile While both unplayed piles are not empty Each player moves the topmost card from the unplayed pile to the top of her war pile If these cards do not have the same rank Move the cards from the loser’s war pile to the winner’s winnings pile The player with the largest winnings pile wins
Additional Classes • Player contains 3 piles of cards • WarGame allows the user to step through the game • On each step, the cards are drawn and the piles are shifted if there is a winner
Classes and Relationships Deck WarGame 0..52 2 Card Player cards.py: Card, Deck war.py: Player, WarGame, playWar
The WarGame Interface WarGame() # Creates a deck and 2 players deal() # Deals 26 cards to each player step() # Draws the cards and shifts the piles winner() # Returns None if the game is not over, # or the results as a string otherwise __str__ # The current state of the game as a string
Playing the Game WarGame() # Creates a deck and 2 players deal() # Deals 26 cards to each player step() # Draws the cards and shifts the piles winner() # Returns None if the game is not over, # or the results as a string otherwise __str__ # The current state of the game as a string def playWar(): game = WarGame() game.deal() while game.winner() == None: game.step() print(game) print(game.winner())
What Does a Player Do? • A player receives a card during the dealing of each of 26 cards and adds it to her unplayed pile • A player draws a card from her unplayed pile, moves it to her war pile, and returns that card • A player returns the number of cards on her winnings pile • A player moves a set of cards to her winnings pile • A player removes and returns the set of cards in her war pile
The Player Interface Player() # Creates a player with 3 empty piles receive(card) # Adds a card to the player’s unplayed pile draw() # If there are cards in the unplayed pile, moves # the top one to the war pile and returns it; # otherwise, returns None getCardCount() # Returns the number of cards in winnings pile add(cardList) # Moves cards from list into winnings pile remove() # Removes all cards from war pile and returns\ # them in a new list __str__ # The current state of the player as a string # (The card at the top of the war pile)
The Design of step in WarGame step() draw cards from both players if if card2 is None or card1’s rank > card2’s rank add player2’s cards to player1’s cards else if card1 is None or card1’s rank < card2’s rank add player1’s cards to player2’s cards