Program 4

The purpose of this assignment is to gain some experience with event driven programming and building a Graphical User Interface. It is also an opportunity to get some experience working with a larger code base of existing code. This is a common form of development, that is, not starting from scratch.

For this assignment you will build the beginnings of a computer program to play the game of Gin Rummy. See Gin Rummy for an explanation of the game. In this version it will be a sort of Gin Rummy solitare. Your objective of the game will be to complete the hand so that all 10 cards can be melded as sets or runs making as few draws from the deck/discard-pile as possible. The "winning" hand will contain two 3-card sets or runs and one 4-card set or run. A set is cards with the same rank (e.g. all kings or all sixes). A run is a sequence of cards in the same suit (e.g. 3, 4, 5 of diamonds). The ace is to be treated as low coming before two in a run.

To simulate the choice that is made in a normal two person game of Gin Rummy, after you discard, the next card from the top of the deck will automatically be drawn and placed on the discard pile face up. Like having an opponent that never keeps a card and simply draws the next card and discards it. That will give you, the player, the choice between picking up the "discarded" card, or drawing the unknown card from the deck.

Your GUI must allow the player to view their cards, draw a card from the deck, pickup the card from the discard pile, discard a card from their hand, and declare they have gin. In addition, the GUI should prevent the player from making illegal plays. Specifically they should only be able to draw, pickup the discard, or declare gin when they have 10 cards, and they should only be able to discard when they have 11 cards.

If they declare gin when they don't have gin, they should be informed (e.g. with a pop-up) and if they have gin, they should also be informed with a message that includes the number of cards.

Much of the code you will need has been written and can be found in the file program4.zip in Canvas. You should begin by downloading that file and creating an Eclipse project containing all of the .java files. In addition, the directory cards in the zip file should be copied into the bin directory of your Eclipse project. That is where your program will be looking for the card images used in the display.

Each of the .java files has an opening block comment that describes what the class does. Here is a very brief list/description:

  • Card, Deck, Suit, Rank: are used to represent (non-graphically) a deck of cards.

  • Hand: is a collection of cards (non-graphically)

  • CardHolder: creates a GUI component that shows a graphical display of a Card

  • HandDisplay: is a GUI component that shows a hand

  • ImagePainter and GIFImage are two helper classes for the CardHolder class and could be used in other programs for displaying images

  • CardPicker is the start of an event handler for picking a card out of a hand.

  • HandController is an event handler that shows how to add or remove cards from a HandDisplay.

  • HandDisplayTest shows how a HandDisplay can be used in a GUI to show a hand where cards are added or removed.

How to begin?

  1. Download and unzip the prog4.zip file from Canvas. As suggested above, first build an eclipse project with all of the .java files in the src directory and the cards folder in the bin directory of that project. When you use File/Import to select the files for import (from the location where you unzipped the zip file), using select all should also bring in the cards folder. When I did it, it placed a copy of cards in both bin and src directories which is fine.

  2. Run HandDisplayTest to confirm everything is in place and working.

  3. Make a new class GinRummy that is a copy of HandDisplayTest. You could just modify HandDisplayTest but you might want to keep the unmodified original around just in case.

  4. Most of your changes will be in GinRummy, CardPicker, and HandController. 

  5. Decide on what you would like the interface to look like? Will there be buttons? How many? How will they be arranged?

  6. Make the changes to GinRummy so that it displays your selected components without them actually doing anything. (50 points)

  7. Make it possible to draw a card from the deck. Start with HandController. (5 points)

  8. Make it possible to discard a card from your hand. Start from CardPicker. (5 points)

  9. Make it possible to flip a card over from the deck onto the discard pile. (5 points)

  10. Make it possible for the player to declare they have gin and give an appropriate response. E.g. either congratulations or keep trying. To make it easy to test this, comment out the line in GinRummy where the Deck gets shuffled. It will then be easy to have a hand with gin. (5 points)

  11. Enforce the rules of the game so that the player can only draw or declare gin when they have 10 cards and can only discard when they have 11. (10 points)

  12. After the player discards, automatically flip a card from the Deck onto the discard pile. (5 points)

  13. Finally, if they successfully declare gin, report how many cards they had to pickup to get there. (5 points)

Note: You can lose up to 10 points for failing to follow good programming style. Pay particular attention to selecting descriptive variable names, making sure your code is properly formatted, and that you provide comments as needed.

What to turn in?

  • When you are ready to turn in your program, make sure there is a block comment in GinRummy.java that indicates your name and your partner's name if you had one, even for a little while. Make it clear if this is an individual submission or a pair submission.

  • Create a zip file that contains all of your .java files and the cards folder.

  • Please name it prog4.zip.
  • I strongly recommend you move this zip file to an empty folder and unzip it to confirm it has all of the expected contents. Students sometimes do not zip it correctly and key files are missing.
  • In the location where you unzipped the file, compile and run the program to confirm it works.