1 / 10

Computer Science 111

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

risa
Download Presentation

Computer Science 111

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 111 Fundamentals of Programming I Designing War

  2. 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

  3. 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

  4. 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

  5. Classes and Relationships Deck WarGame 0..52 2 Card Player cards.py: Card, Deck war.py: Player, WarGame, playWar

  6. 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

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

  8. 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

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

  10. 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

More Related