15-110 FALL 2009 (CORTINA)

PROGRAM 7 - due Tuesday, November 3, 2009 by 11:59PM

Electronic handin will be available after 5PM on Friday.

The game of Mot consists of an infinite collection of cards, where each card has a color and a number, as shown below:

The colors can be red, green, yellow or blue and the numbers range from 1 to 9, inclusive. There are duplicate cards in the game, and the cards are in random order.

The player gets 5 cards dealt initially. The first card is called the "discard pile". The goal is to get all of your remaining cards on the discard pile. You can move one of your other cards to the discard pile if it matches the discard pile's color or number (or both). For example, in the game shown below, we can put the red 6 or the blue 4 on the discard pile.


DISCARD
PILE

Note that once a card is placed on top of the discard pile, you cannot see the cards underneath anymore. So you only have to keep track of the top card of the discard pile. If you cannot move any of your other cards to the discard pile, you must deal one new card. The game ends when you move all of your cards to the discard pile or if you have (CORRECTION:) nine cards showing (including the top of the discard pile) and you cannot move any of them on to the discard pile.

Assignment

You will complete two classes for this assignment. One class will model a single card for this game. The other will contain the code to play the game using instances of your card class.

SET-UP: Download a copy of the Program7.zip file. Unzip the file and move the folder MotGame into your workspace for Eclipse. Then, using Eclipse, start a new Java project. Select "Create a new project in workspace" and enter the project name MotGame. You should see a warning at the bottom of the dialog box that says "The wizard will automatically configure the JRE and the project layout based on the existing source." This is ok. This means that Eclipse found a folder with source code in it already so it will start the project with that code.

  1. Write a Java class named Card that models a card in this game. It should contain a field for the color and a field for the number of the card. For colors, choose from the strings "red", "green", "blue", "yellow".

    Include the following methods in your Card class:

  2. In the same project, complete the Java class named MotGame that contains a main method that plays the game.

    The main method creates an array that can hold up to 9 cards and sets up a Grid object that will display the cards. (The grid is 1 X 9, since there is one row with a maximum of nine cards.)

    First, initialize the first five cells of the card array with new cards. Then call the method drawCards to display the cards in your array. The drawCards method requires two arguments, a reference to the grid that draws the cards (game) and a reference to the array with the card in it (cardArray). The drawCard method will draw the cards on your screen, assuming you wrote the Card class correctly, and will also display them in text, assuming you wrote the toString method of your Card class correctly.

    Next, ask the user which card he/she wants to move on to the discard pile. The user should enter an integer between 1 and 8 inclusive. (0 represents the discard pile.) Then do the following:

    After the user inputs a valid number and you process it, call the drawCards method again to redisplay the current cards.

    Repeat this process until the player has only one card (in position 0) and wins, or the player has nine cards (one in every position) and is forced to deal another card and loses.

    Once the game ends, print the phrase "WINNER" or "LOSER" in the console.

Program Requirements

You should have a variable in your game that keeps track of the number of cards displayed. For example, at the start of the game, this is 5. If you store the cards in your array starting from position 0 without leaving any "gaps" (i.e. null references), as you should, then this variable will also tell you the location of the next available position in the array. For example, if you have 5 cards showing, these would be stored in positions 0 through 4 in the array, and position 5 is the first empty cell.

You may use helper methods (additional static methods) in your MotGame class to make things more manageable if you wish.

Compiling and Testing / Sample Output

In addition to displaying the cards with the drawCards method, your game should print the cards as text in the console window for debugging purposes. For example, your program might start out like this:

Welcome to MOT!
Your current cards:
0: red 4
1: blue 5
2: red 6
3: blue 4
4: yellow 9
Pick a card to move to position 0 or enter 0 to get a new card: 3
Your current cards:
0: blue 4
1: blue 5
2: red 6
3: yellow 9
Pick a card to move to position 0 or enter 0 to get a new card: 1
Your current cards:
0: blue 5
1: yellow 9
2: red 6
Pick a card to move to position 0 or enter 0 to get a new card: 1
INVALID POSITION
Pick a card to move to position 0 or enter 0 to get a new card: 0
Your current cards:
0: blue 5
1: yellow 9
2: red 6
0: blue 5
1: yellow 9
2: red 6
3: green 5

etc.

Documentation & Programming Style

Your code should be well documented and should demonstrate proper Java formatting style. Be sure to name your variables appropriately and indent properly.

Hand-in Instructions

See the course website for instructions on how to hand in your program. Please zip your project folder that is created in Eclipse. This makes it easier for us to grade your work.

Remember that the work you submit must be your own. Also, late hand-ins are not accepted. Please plan ahead and submit early to avoid server overload at the deadline. The deadline is based on the server's clock, not your clock. Please do not email your code to your instructor or course assistant as your official hand-in; these will not be graded.