CS 1063  Project 3:  Concentration

The Concentration Class

Objectives

This is one of three major programming projects this semester.  You may NOT collaborate on this project.  While you may ask for assistance in debugging, this project should be ENTIRELY your own work.

Other objectives include:

Hand-in Requirements

All projects and laboratories will be submitted electronically through Blackboard.  Zip up your entire lab directory to submit as the source.  (Right click on the lab folder and follow the 7-Zip > Add to "project3.zip" or follow Send To > Compressed (zipped) Folder.)  The lab folder should include the following:

Introduction

The goal of this project is to implement a program that allows the user to play a version of Concentration.  In this version, the goal of the user is to match a color on the top row with a color on the bottom row.  The initial 700x300 window should have two rows and six columns:

Empty Concentration board

In each turn, the user selects a square from the top row, then a square from the bottom row.  The colors of the squares will be revealed.  For example, the user might select the second square on the top row and the fourth square on the bottom row.  The window now looks like:

Concentration board after two squares are selected

In this case, the colors do not match, so after a second or so, the green square and the red square will be hidden with the original gray color.  If the colors had matched, they would have remained revealed.

Here is a board after three colors have been matched.  Note that each color square in the top row has a matching color square in the bottom row.

Concentration board after matching three colors

Here is the board after all the colors have been matched.

Concentration board after matching all colors

Two arrays of length six are needed to store the colors of the two rows.  Two more arrays of length six are needed to remember whether each square is hidden or revealed.

A program that partially implements this game for three columns is provided for you.  Lab 9 is used to extend this program to six columns  This project is used to implement the additional features described below.

Initial Zip File

You can start your project by downloading project3.zip.  This file contains DrawingPanel2.java and an initial version of Concentration.java.

DrawingPanel2.java

DrawingPanel2.java has additional methods for closing the window and getting information about the state of the mouse.  Here are the methods available to you.

Method Name Description
getGraphics( ) returns a Graphics object that the program can draw on
setBackground(color)     sets the background color
sleep(milliseconds)  pause for a given number of milliseconds (1000 milliseconds equals one second)
close( )  closes the window
getMouseX( )  returns the X position of the mouse
getMouseY( )  returns the Y position of the mouse
getClickX( )  returns the X position of the last mouse click
getClickY( )  returns the Y position of the last mouse click
mousePressed( )  returns true if any mouse button is pressed

If you run the initial version of Concentration.java, you will see information about the mouse in the status bar of the window.   If you look more closely at the code in Concentration.java, you will find out that the code uses these methods to repeatedly ask about the status of the mouse.  This is a bad way to program window behavior because a lot of time is wasted in loops waiting for something to happen (called busy waiting or spinning).  Instead, the program should be event-driven, but we don't have enough time to discuss Chapter 14 of your textbook.

Concentration.java

The initial version of Concentration.java implements the following pseudocode:

  1. Initialize the window so that the colors of the squares are hidden.
  2. Initialize the two arrays that store the hidden Colors of the top row and bottom row.
  3. Loop forever:
    1. Wait until the user clicks on the top row.
    2. Reveal the color of the square that the user clicked on.
    3. Wait until the user clicks on the bottom row.
    4. Reveal the color of the square that the user clicked on.
    5. If the colors do not match, hide the colors of the squares.

You should study the code in Concentration.java to gain an understanding of how the code implements the above behavior.  Describe what is stored in each variable.  For the getClickRow and getClickColumn methods, determine the correspondence between the return values and locations of the mouse clicks.  Figure out how the program knows the color of a particular square.

Change to Six Colors

For Lab 9, you need to identify all the code which assumes that the game has three Colors and three columns and modify the code to handle six Colors and six columns.  You should initialize the Color arrays to correspond to the following colors.

Concentration board after matching all colors

The following sections discuss features that should be implemented for Project 3.

Randomizing the Colors

The game is not very interesting if the order of the Colors are the same every time.  It would be better if the order of the Colors are different every time.  That is, the Colors should be put in a random order.

A simple way to implement the randomizeColors method is to follow the following pseudocode:

  1. Repeat several times:
    1. Generate two random numbers r1 and r2 between 0 and length - 1 of the Color array.
    2. Use the swap method to swap the values at indexes r1 and r2.

The swap method is already implemented for you in Concentration.java.

This pseudocode is not correct if you want an equal chance of generating each possible order.  A more sophisticated algorithm is:

  1. Loop for i from 0 up to length - 1 of the Color array.
    1. Generate a random number r between i and length - 1 of the Color array.
    2. Use the swap method to swap the values at indexes i and r.

Detecting the End of the Game

Instead of an infinite loop, the game should end when the user has matched all the colors.  One approach is to count how many times the user has found a matching pair of colors.  After matching six pairs, the game should be over.  Run the following code after the game is over.  You will probably need to adjust some of the numbers so the text appears correctly.

g.setFont(new Font("SansSerif", Font.BOLD, 100));
g.setColor(Color.BLACK);
g.drawString("The game is over!", 0, 300);

Fixing the Clicking on Revealed Squares Bug

The game currently allows the user to select a square after it has been revealed.  For example, suppose the game has reached this board.

Concentration board after matching three colors

A misbehaving user might now select the green square on top and the magenta square on the bottom.  Because the colors do not match, the program as it is currently written will hide the two squares, resulting in this bad board.

Concentration board after the effect of a bug

The program should be modified to ensure that a revealed square cannot be selected.  To do this, the program needs to remember which squares have been revealed.  One way of implementing this idea is to have two additional boolean arrays.  For example, in the above board where there are three matches, one array should represent what has been revealed in the top row.

index     0     1     2     3     4     5  
value   false true  false true  true  false

The other array should represent what has been revealed in the bottom row.

index     0     1     2     3     4     5  
value   true  true  false false true  false

Hints: Look at p. 427 in your textbook for an example of how to construct a boolean array.  These arrays need to be updated after squares are revealed by the program and when the squares are hidden by the program.  Additional code that checks these arrays is needed to ensure that col0 and col1 in the program correspond to hidden squares.

Rubric

Your program should compile without any errors.  A program with more than one or two compile errors will likely get a zero for the whole assignment.

The following criteria will also be used to determine the grade for this assignment: