Can someone help me find out what I am doing wrong? Why is it not showing my green square?
****Move it as the first if statement that is checked.( I moved to to the beginning of draw….) The other two statements should be else if statements. (not sure what to change)
int[][] grid; //creates a 2D array called grid
int sqSize = 50; // used for location and size of squares on grid
int gridSize = 500; // width and height size
int n = gridSize/sqSize; // number of rows and columns
int row=5;
int col=5;
public void setup(){
size(500, 500); //size the frame
noLoop(); // make it not loop
grid = new int[n][n]; //initializes the grid
}
void draw() {
int ran = (int)(Math.random() * n); // choose a random number for index in the grid
int ran2 = (int)(Math.random() * n); // choose a random number for index in the grid
if((row == ran) && (col == ran) && (ran == ran2))
{
fill(0, 255, 0);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}
//loop through the rows and columns
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
stroke(0);
if(row == ran && col == ran )
{
fill(255, 0, 0);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}
else {
fill(255,255,255);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}
}
}
System.out.println(ran);
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
stroke(0);
if(row == ran2 && col == ran2 )
{
fill(0, 0, 255);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}
}
}
System.out.println(ran2);
}
After you have copied the code and tested it, create a second random number called ran2 that also produces a random number between 0-9. (number of rows/columns). Create an if statement using the else if structure that will fill the square with blue at the new random index location [ran2][ran2]. Add a system.out.println statement so you can see the second random number produced.
Add another if structure that will test to see if ran = col and ran = row and that ran and ran2 are the same number. If they are fill the square with the color green. Test the code by hard coding the random numbers the same. Make sure you change it back to produce a random number. You need to check two conditions: if row and col equal ran and if ran equals ran2. An example of the code is below.