Most of you probably did Lab 5 something like the following: This is probably how most of you are starting.

Let's talk redesign.

We can use modularity in two ways here:

  1. use of loops
  2. use of classes

I am going to do both...loops and classes.

First lets look at the use of loops ... we will edit this code later for more completeness

Rather than how it is set up in the above code, consider a loop and an array:

Setting up the Game - Do everything one step at a time...ONE thing at a time. What would you do if you were, say, drawing the game. Think of each step as you go along. Specifically, don't try to do everything at once. First just get the thing drawn, then work at identifying individual ones with a mouse-click, then think of how the game goes. one step at a time!!!

Here was the order I proceeded ... one step at a time...each step is basically only changes one method. Parts of the code are below:

  1. redesign ideas (see below)
  2. (1 pt.) get the button on the screen (init): See it here
  3. consider what the button will do, and thus consider possible reuse of code that draws the grid
  4. (5 pt.) make the grid to at least draw the squares (displayGame): See it here and generating the colors
  5. (5 pt.) make the grid draw the same color only twice (use random and arrays): See it here
  6. (1 pt.) get the surrounding lines: See it here
  7. (1 pt.) make the New Game button create a new grid: See it here
  8. (5 pt.) consider the game - we need to know which box is chosen when clicked with the mouse (mouseReleased): See it here by looking at the Java Console and using System.out.println in the MouseReleased() method. It would be helpful to remember the array lecture and an example
  9. (1 pt.) make the Box change once clicked: See it here
  10. (1 pt.) make the Game begin with the boxes gray: See it here
  11. (5 pt.) make it play the game (i.e., proper flips at proper times). This finished code is only one method different than the one in the previous step.

Here, again, is the game done with the mouse working and flipping back when colors do not match.

One student's final submission...nice. Make sure to play the whole game.
Another amazing - make sure to play to the end and then choose New Game. Finally, one without the "third click" trick code to show pause

You need to be very careful of where you place variables in loops. Specific examples

OK, step 1: redesign


Look at it as OBJECTS
What objects does one manipulate in the game?

This leads to the idea of the game being an object, but also consider the boxes in the grid as possible objects.

Why?

Because they hold information! They are things with a location and a color. Thus they could be things with these as IVs

Object Model??

Let's talk redesign

Here is a possible Lab 7 startup. Notice the use of System.out.println() throughout. Keep the Java Console open so you can see the "clues" these are giving you. I am going to use the code below to show how you can use some of one's old material (lab 5) to begin a re-design that can really help this lab.
NOTE: Compile this class - you will see some errors about variable Box. Note that you are declaring things as Box but have not defined the class Box. See
below for a start on the Box class.

We can use modularity in two ways here:

  1. use of loops
  2. use of classes

I am going to do both...loops and classes.

How about a Box class. This gives you a "handle" to each box... rather than just knowing the coordinates

Discuss - why would this be useful?

What does one do with a box?

What else would each Box do? What does the Game do?

Hopefully, this is helpful...

Hmmmmm - can the MouseListener be in the boxes themselves! so when a box is clicked, it knows the source was the box and we do not have to play with coordinates? Check it out....hmmm these Boxes are not Components ... why do I think that is important?