Let's talk redesign.
We can use modularity in two ways here:
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:
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:
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. You need to be very careful of where you place variables in loops. Specific examples
OK, step 1: redesign
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.
We can use modularity in two ways here:
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?
Some new ideas (INCOMPLETE) for the Applet class are to have the init() call a method
to set up the array of Boxes
The color will come from a Color array that you have and randomly
pick items (colors) out of.
An simple array of Colors (not generated randomly) could look like:
Color colorArray[] = {Color.red,Color.blue,Color.cyan, Color.green, Color.magenta,Color.pink,Color.orange, Color.yellow};
You might use a specified array like that to at least get going until
you figure the random generator. See here for random info
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?
How about getting the colors?
Let's talk about the random generator of colors. Easiest way is
to match colors to a number, since java provides a random number
generator
The color will come from a Color array that you have and randomly
pick items (colors) out of.
An simple array of Colors (not generated randomly) could look like:
Color colorArray[] = {Color.red,Color.blue,Color.cyan, Color.green, Color.magenta,Color.pink,Color.orange, Color.yellow};
OR, you could do some mathematical truncation, etc to determine which
box...(this is easier...and what I did)
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?
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!!!
Documentation and design points:
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
Look at it as OBJECTS
What objects does one manipulate in the game?
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.
What else would each Box do? What does the Game do?
The new paint would be simply:
Now that you have reconsidered design, go back to the design steps now so that you do not get too confused.
For either of these, since we only want 8 choices of numbers mod
(or the java operator %) may be useful. Why? How does mod arithmetic work?
rint(double)
returns the closest integer to the argument.
or one can cast (int)Math.random()
Nice option since it is a class method so can call without instantiationFor example: nextInt(8) returns a random number between 0 and 7.
This involves something like the array we saw in class in these notes
Again, this is like the array we saw in class in these notes
Then:
(For example, if you have integer division (x - initialBoxX)/50 what does this give?)