Back to 111 Main Page

Mobius strip

Assignment 19


Write a 2-player card game.

Concepts: Review of all you've learned this term.


You may pick which card game you write. However, it needs to meet these requirements:

  • 2-player. The game must involve only two players, and one of those players must be the computer. This means your program must make all the second player's moves. You may make these moves randomly if necessary.
  • Standard rules. The game should be some semi-standard/traditional, recognizable card game, not some game you just made up. Your game might follow certain variant rules, however. (I'd prefer you use a standard playing card deck, rather than proprietary cards such as an Uno deck.)
  • Significant user choices. Your game should require input from the user, and this input should have an impact on the outcome on the game. The choice may be as simple as picking a number between 1 and 10 though.
  • Game mechanics handled appropriately. Your game needs to set everything up correctly, get the input from the user, etc. Most importantly, it needs to recognize when the game has concluded and declare who won. Ideally, it will then ask the user if they want to play again.

Your user interface may be text-based or a GUI (for extra credit; see below). It might be able to run in either mode.

Internally, your program needs to include the following:

  • At least one loop and one conditional.
  • User input.
  • At least 3 different methods. Constructors and main count as methods for this purpose.
  • At least 2 classes that you created. (This can be as simple as your game and a PlayingCard class.)
  • A collection. That is, you need to use at least one array or ArrayList.
  • No runtime crashes. This means you need to handle any kind of input. Your program should probably loop until it gets the correct info it needs, though it may quit gracefully if necessary.

Your game should also (briefly) explain to the user how to play when it starts.

Possible Games

Some games are harder than others, so, before you pick one, spend some time sketching out the steps you're going to need to implement to get the job done.

Here are some game ideas:

  • Old Maid
  • Crazy Eights
  • Go Fish
  • Blackjack

Check Wikipedia or's list of 2-player card games for more ideas.

What to Submit

Upload your file to Tamarin. As always, remember to include all the extra classes you need to run your program in this file. The public UsernameA19 class should include the main method that runs your program.

We are going to do a studio on this assignment that will work like Studio 3 on A18: you will submit two parts, and your grade will be the sum of the two parts (rather than an average). Therefore, you'll need to submit both an a version your class, and then a b version. (Before submitting, delete all the .class files in your directory, recompile, and run your program one last time to make sure it all works!)

08 Dec: Upload your file to Tamarin. Please do not submit this assignment late. (If you do, you won't get any studio feedback.)

16 Dec: Upload your file to Tamarin.

Grading [15 points]

% - Game choice
2 player game, computer is a player, game with established rules, significant user choices. (-25% of your final grade for each one missing.), out of 7 points:

1 - Compiles
1 - Javadoc
Your code is documented, and it is clear what game (and what rules) you are implementing.
1 - Instructions/UI
It is clear to the user how to play the game.
4 - Required code components
At least one loop and conditional, a collection (array or ArrayList), at least 2 classes, and 3 different methods., out of 8 points:

1 - Coding standards
Code formatted according to the course's coding standards.
4 - Completeness
The game is finished and all parts work. Output is clear and understandable.
2 - Correctness
The game performs correctly (when given correct input).
1 - Robustness
The program does not crash or misbehave if given incorrect input.

Extra Credit: GUI

Create an applet and/or proper GUI application using either AWT or Swing for your card game.

To learn to make a Java GUI, find a good book or tutorial. I recommend starting with the Java Tutorial, particularly the trail on Swing

If you have questions regarding the specifications (see below) or how this assignment is graded, please let me know. I will spend one lab session this week discussing JOptionPanes and the very basics of creating a GUI or applet. However, outside of this single GUI lab session, I will not answer any Swing or applet questions--the point of this EC assignment is that you learn how to do it on your own.

The amount of EC your earn will depend on the sophistication of your GUI. The following are some guidelines/ideas. Your submission must work to get credit! This means you need to include all files needed to run it, including any .html files for applets, etc.

3 points - Application (JOptionPanes)
An application (only) that does not use the console at all but uses JOptionPanes for all input and output.
5 points - Applet (JOptionPanes)
An applet (only) that generates a series of JOptionsPane pop-ups.
8 points - Applet or GUI Application (Text-based)
Runs much like a text-based game, only it is displayed directly within the applet panel or its own JFrame window (using a non-editable TextArea for output and a text field for input, perhaps?)
10 points - Applet or GUI Application (Static display with pop-ups)
Part of the game (such as the player's hand) is displayed continuously in its own component (rather than appearing along with a printed menu). The game can be affected through components (buttons, input fields, etc) in the same panel. These components may produce pop-up/JOptionPanes to get input.
12 points - Applet or GUI Application (Embedded/direct controls)
As for "Static display of pop-ups" description, but no pop-ups are used to get input. Instead, all input is done through the same panel as contains the list display.
+1 points - Close button works
Your application's X close button works.
+2 points - Quit Menu
The application window includes a simple working menu of the File -> Quit variety.
+3 points - Link to working online version of Applet
You get your applet version uploaded and running on your uhunix website.
+2 points - Applet or Application option
The same main class will run either as an application (by running its main method) or can be embedded in a webpage as an applet (because it extends Applet).
+1 points - Console mode
It is also possible to run the game in text mode in the console window. (A command line argument will probably be required to indicate which mode to run in.)

Here is the code I showed you on GUIs in lab:

Extra Credit: Packages and JAR files

+1: Packages. First, put each of your finished classes in its own file. Then place all those classes in a package. You can use your username as the package name. (This will require you to create the appropriate directory and add the package directive to all your files.) You can have additional subpackages if you desire.

+2: JAR File. Then, produce a JAR file containing all your .class and .java files.

+1: Main-Class. You should adjust the JAR file manifest so that you program runs correctly when you execute the JAR file.

To learn more about packages and deployment using JAR files, see the Java Tutorial.

Since Tamarin won't currently accept JAR files, you'll have to email this one to me. Name your file UsernameA19c.jar and email it to me following the normal email submission policies.


Can you give a hint on how to start?
Here's how I'd go about tackling Old Maid:

In main: First, explain the rules to the player.

Create a new Deck of 52 shuffled cards. (If you're not using Deck, then just create an ArrayList of cards instead.)

Deal the cards out into two hands (that is, into two ArrayList<PlayingCards>s). You don't need to alternate between hands as you deal; you can just draw 26 cards for one hand, and then 26 for the other. While dealing, if you draw the Queen of Clubs, discard it. (This technique means you don't need to go find the Queen before or after dealing, and that each hand has an equal chance of being the smaller hand.)

Now go through both hands and remove all pairs--cards with the same value and same color. (This is the hardest part.) Let the user know which pairs were discarded from both hands by this process.

Now the game can start. Show the player their hand. (There's not much they can do with it, though, so you might just want some sort of one-line representation.) Tell them how many cards their opponent has, and let them pick a number between 1 and that number. (This is the card they are drawing from their opponent's hand.) Match and discard the pair this forms.

Then have the computer player randomly pick a card from the player's hand. Again, report what card this was and what discarded pair was formed.

Repeat the previous two blocks--the player drawing and then the computer drawing--until one of the players runs out of cards. The other player should then be holding the Queen of Spades (the Old Maid) and is the loser.

Note: Though possible, this program will be tricky to write if all of this is main. Try to look for things you could break out into methods. For instance, you could write a private static method that takes an ArrayList hand and a PlayingCard to match, and then finds, removes, and returns the matching card (if any) found in the hand.

For games like Old Maid and Go Fish, how do you find all the pairs in a hand?
I would first write a method that takes a hand and a card to match. Something like this:
  private static PlayingCard findMatch(ArrayList<PlayingCard> hand,
                                       PlayingCard toMatch) {
In this method, you would loop through the hand, looking for a card that forms a pair with toMatch. If you find such a card, remove it from the hand and return it. If you can't find any matching card, return null instead. This method can now be used everytime a player draws a card to check if that card matches anything already in their hand.

At the start of the game, you need to find all pairs already in the player's opening hand. To do that, I would loop card-by-card through their hand. Temporarily remove the current card, and call the above method with the rest of the hand and the removed card to see if there's a matching card somewhere else in the hand. If not, put the card back in the hand and remove the next card. Basically, you're going card-by-card through the player's hand to see if there is a matching card somewhere else in the hand.

~ztomasze Index : TA Details: ICS111: A19
Last Edited: 15 Dec 2008
©2008 by Z. Tomaszewski.