Java
SIMULATION PROJECT - GAME OF LIFE
PROBLEM #1 - SOLUTION
HINTS
Please note that you might have a different (and even better) solution. These hints are simply guides that you may
chose to ignore.
HINT #1 –
CLASSES TO USE
You will need
three classes for your solution.
- ColourGrid
- Tester
- NOOPDraw
HINT #2 – CLASS SETUP
Most of the work is in ColourGrid. It probably
should contain the following pieces:
Datafields:
- rows - Number of
rows in the array
- cols - Number of
columns in the array
- grid - The actual
array itself
Constructor:
- Simply gets an argument for each datafield.
- It creates the array and calls the randomizeColours() method to give random colours to each element in
the array.
Methods:
- randomizeColours() – Simply loops through the
array using nested for loops and creates a random Color object for each
element. This method is called
from inside the constructor.
(Initially the code inside this method could be placed in the
constructor but once we want to do animation, we need a way to call this
directly.)
- drawGrid() – Loops through the array using
nested for loops, sets the drawing colour to
the current element’s color and then draws the rectangle at the correct
location.
HINT #3 –
DRAWING SQUARES AT RIGHT LOCATION
The array
holds the colours of each square. To
draw the squares, we need to find a pattern between the element[r][c] and the corresponding square’s x, y, width and height (which we need to draw a square).
For my solution, I used a width and height of 20.
Here is a
table considering the first row of squares.
A pattern is easy to find for the value of x.
Array indexes
|
Corresponding square’s information
|
r
|
c
|
x
|
y
|
width
|
Height
|
0
|
0
|
0
|
0
|
20
|
20
|
0
|
1
|
20
|
0
|
20
|
20
|
0
|
2
|
40
|
0
|
20
|
20
|
0
|
3
|
60
|
0
|
20
|
20
|
0
|
c
|
c*20
|
0
|
20
|
20
|
Here is a
table considering the first column of squares. Again, a pattern is easy to find for y.
Array indexes
|
Corresponding square’s information
|
r
|
c
|
x
|
y
|
width
|
Height
|
0
|
0
|
0
|
0
|
20
|
20
|
1
|
0
|
0
|
20
|
20
|
20
|
2
|
0
|
0
|
40
|
20
|
20
|
3
|
0
|
0
|
60
|
20
|
20
|
r
|
0
|
0
|
r*20
|
20
|
20
|
Now we can
also consider a few different squares that we randomly picked to find an
overall pattern.
Array indexes
|
Corresponding square’s information
|
r
|
c
|
x
|
y
|
width
|
Height
|
2
|
3
|
60
|
40
|
20
|
20
|
7
|
4
|
80
|
140
|
20
|
20
|
4
|
1
|
20
|
80
|
20
|
20
|
0
|
6
|
120
|
0
|
20
|
20
|
r
|
c
|
c*20
|
r*20
|
20
|
20
|
So,
the statement
NOOPDraw.fillRectangle(c*20, r*20, 20, 20);
will draw element[r][c] of the array at its
proper location.
HINT #4 - ANIMATION
The animation
aspect is handled in the main function of the Tester class. Here is the pseudo-code for the animation.
Create NOOPDraw
window.
Create ColourGrid
object cg.
Display cg
Loop
Wait one second
Update cg with new colours
Redisplay cg
End loop
HINT #5 – WAITING
We can make
the program wait 1 second by using:
Thread.sleep(1000);
It will
require that the function using the statement above throws an exception.
|