This is a project I am working at my school as an introduction to two dimensional array. In this program, I attempted to create a two dimensional array, figure out how many neighbouring bacteria said cell have, and create, destroy, and leave the cell as it is. The error I am having is that nothing happens after I press my button. Looking through the script, I am having a hard time finding the logical error. Thank you very much for your time!
// The "Assignment13" class.
import java.applet.*;
import java.awt.*;
public class Assignment13 extends Applet
{
// Instance variables
Button button[] [] = new Button [10] [10];
boolean bacteriaCheck[] [] = new boolean [10] [10];
Button done = new Button ();
Button nextgen = new Button ();
static TextArea text;
Font f;
int one, two;
int temp = 0;
public void init ()
{
f = new Font ("Arial", 10, 24); // Font creation
// Creates buttons based on the counter
for (int i = 0 ; i <= 9 ; i++)
{
for (int k = 0 ; k <= 9 ; k++)
{
button [i] [k] = new Button (); // Populates array
}
}
Panel panel = new Panel ();
GridLayout grid = new GridLayout (10, 10); // Creates a 10x10 grid
setLayout (new BorderLayout ());
panel.setLayout (grid);
text = new TextArea (10, 20);
// Adds buttons to panel
for (int i = 0 ; i <= 9 ; i++)
{
for (int k = 0 ; k <= 9 ; k++)
{
panel.add (button [i] [k]);
bacteriaCheck [i] [k] = false;
}
}
panel.add (nextgen); // Adds "Next Generation" button to panel
panel.add (done); // Adds "Done" button to panel
done.setLabel ("Done");
nextgen.setLabel ("Next Generation");
add ("Center", panel); // Puts panel in the center of applet
add ("East", nextgen); // Puts "Next Generation" button on the east side of applet
add ("South", done); // Puts "Done" button at the bottom of applet
add ("West", text);
for (int i = 0 ; i <= 19 ; i++)
{
// Randomly generates 20 cells
one = (int) (Math.random () * 10);
two = (int) (Math.random () * 10);
if (bacteriaCheck [one] [two] == true)
i = i - 1;
else
{
button [one] [two].setLabel ("X"); // Sets label to "X" on the randomly generated cells
button [one] [two].setEnabled (false); // Sets the buttons to false so they can't be overwritten
button [one] [two].setFont (f); // Sets the font on the buttons
bacteriaCheck [one] [two] = true;
}
}
} // init method
public boolean action (Event e, Object o)
{
if (e.target == done)
{
// Disables buttons
nextgen.setEnabled (false);
done.setEnabled (false);
System.exit (0); // Exits applet
}
if (e.target == nextgen)
{
advanceday ();
}
return true;
} //action method
public void advanceday ()
{
for (int xCord = 1 ; xCord == 10 ; xCord++)
{
for (int yCord = 1 ; yCord == 10 ; yCord++)
{
if (bacteriaCheck [xCord] [yCord] = false && neighbourCount (bacteriaCheck, xCord, yCord) == 3)
{
button [xCord] [yCord].setLabel ("X"); // Sets label to "X" on the randomly generated cells
button [xCord] [yCord].setEnabled (false); // Sets the buttons to false so they can't be overwritten
button [xCord] [yCord].setFont (f); // Sets the font on the buttons
bacteriaCheck [xCord] [yCord] = true;
}
else if (bacteriaCheck [xCord] [yCord] = true && neighbourCount (bacteriaCheck, xCord, yCord) > 3 && neighbourCount (bacteriaCheck, xCord, yCord) < 2)
{
button [xCord] [yCord].setLabel (""); // Sets label to "X" on the randomly generated cells
button [xCord] [yCord].setEnabled (true); // Sets the buttons to false so they can't be overwritten
bacteriaCheck [xCord] [yCord] = false;
}
}
} //advanceday
}
public static int neighbourCount (boolean[] [] bacteriaCheck, int x, int y) //count neighbours
{
// X and Y co-ordinates for all spots around current point
int[] xVals = {x - 1, x - 1, x - 1, x, x, x + 1, x + 1, x + 1};
int[] yVals = {y - 1, y, y + 1, y - 1, y + 1, y - 1, y, y + 1};
int nCount = 0;
// Count neighbours algorithm
for (int i = 0 ; i < 8 ; i++)
{
if (xVals [i] >= 0 && yVals [i] >= 0 && xVals [i] <= 11 && yVals [i] <= 11)
{
if (bacteriaCheck [(xVals [i])] [(yVals [i])] == true)
{
nCount++;
}
}
}
text.append (Integer.toString (nCount));
return nCount;
} //neighbourCount
} // Assignment13 class