111 lab9

Computer Science 111 – Fundamentals of Programming IProgramming Project 9
Due date: 11:59 PM Friday 20 November

Goofus insists on writing everything from scratch. Gallant copies, pastes, and modifies.In this project, you will complete a program for War, a virtual card game.

Overview of Online Card Games

You have probably played several card games, such as Rummy, Crazy Eights, and Hearts. Most computer operating systems include one or more free card games for entertaining diversion. Common examples are Freecell and Solitaire on Windows systems. Most card games use a deck of cards with the following attributes:

  • There are 56 cards in a deck
  • Each card has a suit and a rank
  • There are four suits: Spade, Heart, Diamond, and Club
  • Each suit has a color: black (Spade and Club) or red (Heart and Diamond)
  • The cards can be ordered from highest to lowest by rank, as follows: King, Queen, Jack,
  • 10, 9,…, 2. An Ace has either the highest or the lowest rank, depending on the game
  • The cards can also be ordered by suit, as follows: Spade, Heart, Diamond, Club
  • A deck has one card of each rank and each suit, which equals 52 (13 * 4) total cards
  • A card is face up if you can see its suit and rank; otherwise, it is face down
  • Cards can be dealt or transferred from a deck to players
  • A deck can be shuffled, so that the cards can be dealt in random order

Modern online card games typically have flashy graphics that display images of the cards and allow the user to move them around by manipulating a mouse.

Overview of the Game of War

The following paragraph describes a simplified version of the game of War. For a more complex version, see Hoyle’s Rules of Games (New York: Signet Books, 2001).

There are two players in the game of War. During the course of a game, each player will have two piles of cards, named an unplayed pile and a winnings pile. The game essentially moves forward as cards move from the unplayed piles to the game’s single war pile and then to the winnings piles. The game ends when a player’s unplayed pile has no more cards. At that point, either the player with the largest winnings pile wins the game or there is a tie. Here are the detailed rules for moving cards:

  1. Each player is dealt 26 cards to its unplayed pile.
  2. Repeat steps 3 through 5 until one or both unplayed piles become empty.
  3. Each player plays the topmost card from his unplayed pile by placing it face up on thegame’s war pile.
  4. If the cards have the same rank, repeat step 2.
  5. Otherwise, move the contents of the war pile to the winnings pile of the player who hasthe card of a higher rank from the last two cards played.
  6. The player with the largest winnings pile wins, or the game ends in a tie.

Ideally, the application should have a graphical user interface similar to that of professional- quality games.

You should first develop a data model that can be tested with a terminal-based user interface. Eventually, you can attach a graphical user interface to this model. Each step of the process will result in one or more resources that become the inputs to the next step. The stuff you need to add is highlighted in red.

Step One: Code Review
The directory project9 on the server contains source files with Python classes for playing

cards and the game of War. This directory also contains a subdirectory named DECK, which holds the image files for the playing cards. Copy the project9 directory to your account and study the code in the files cards.py and testcard.py. Run the testcard.py program from IDLE.

Step Two: Define the Deck Class in cards.py

Now add a new class named Deck to your cards.py file. As the name implies, this class represents a deck of cards. Clients should be able to create a new deck, check its size (the number of cards left in it), deal cards from it, ask if it’s empty, shuffle it, and obtain its string representation. Open the file testdeck.py to get clear on how a deck might be used. Be sure to provide a docstring for the class and for each method in it. Then test your new class by runningtestdeck.py from IDLE.

Step Three: Study and Run the GUI Program in deckgui.py
Open the file deckgui.py in IDLE. The code defines a main window class that allows the

user to view images of the cards in a deck by clicking a button. Run the program and click through all of the cards in a deck.

Step Four: Add Two New Buttons to deckgui.py

Add two more command buttons to the main window class in deckgui.py. One button is named Shuffle and the other button is named New deck. Now add methods that are triggered when these two buttons are clicked. The first method, named shuffleDeck, shuffles the existing deck. The second method, named newDeck, creates a new Deck object, assigns it to the window’s deck instance variable, and updates the card images appropriately (the view should look like it did at program startup). Test your program to verify that it produces the expected results for all button clicks.

Step Five: Complete and Test the Code for the Terminal-Based Game of War

A terminal-based game of War can be executed without any user interaction. The mainfunction simply makes moves and displays the state of the game until a player wins or there is a tie. Open the file testwar.py to see how this is done. Note that the code creates a new instance of WarGame and calls the appropriate methods to play the game. Now open the file wargame.pyand examine the code for the WarGame class. Note that this class includes instance variables for the two players, the single war pile, and the game state. Also note the methods that are run on the two Player objects. These methods are defined in the Player class that appears later in the same file. Your job in this step is to complete the methods in the Player class and test your work by running the program testwar.py.

Step Six: Study and Run the GUI Program in wargui.py
Open the file wargui.py in IDLE. The code defines a main window class that allows the

user to play the game of War by clicking a button. Run the program and play the game, until there is a winner or a tie. Study the code in the main window class. Note how the nextCardmethod calls the same methods on the WarGame object as you saw in the terminal-based program.

Step Seven: Add a New Button to wargui.py

Add a button named New game to wargui.py. The state of this button at program startup should be “disabled.” When a game ends, the nextCard method should disable the Next card button and enable the New game button (a state of “normal”). Then define a new method named newGame, which is triggered with the user clicks the New game button. This method creates a new instance of WarGame and essentially restores the view that the user had at program startup. Before this method quits, it disables the New game button and enables the Next cardbutton. You should now be able to play multiple games in one session with wargui.py.

Extra Credit (10 points)

The game of Craps is played with a pair of dice. Each player takes turns rolling the dice. Here are the rules of the game:

  • An initial roll of 7 or 11 wins
  • An initial roll of 2, 3, or 12 loses
  • Any subsequent roll of 7 loses

Any subsequent roll that equals the initial roll wins

The resources in the files die.py, crapsgame.py, testcraps.py, and crapsgui.py in the project9 directory play similar roles to the resources in the corresponding files for the game of War. Your job is to complete the game logic in the file crapsgame.py, so that you can test the game in the terminal-based and GUI-based versions. To do that, study the code in the files die.py,testcraps.py and crapsgui.py to see how the game logic methods are used. Screen shots of the terminal-based version and GUI-based version of the program are shown below.