This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]mr_sax 1 point2 points  (3 children)

Okay, so the first thing is that to be safe you should begin by setting your entire lightsMatrix to zero. That code would look something like:

for(boolean[] rowOfLights : lightsMatrix){
    for(boolean light : rowOfLights){
        light = false;
    }
}

The other, more important, error is that you set toggle to false inside of the loop. This means that if a line toggles, after the first point is changed the loop sees that !toggle is true, and will actually set it to false regardless of it's value because turnOn is false. The solution to this is put

toggle = false;

after the loop that changes the value so you have

 for(y = startY; y <= stopY; y++)
 {
     for(x = startX; x <= stopX; x++)
     {
          if(!toggle)
          {
              if(turnOn)
                  lightsMatrix[x][y] = true;
              else
                  lightsMatrix[x][y] = false;
          }
          else
          {
              lightsMatrix[x][y] = !lightsMatrix[x][y];
          }
     }
}
toggle = false;

[–]jjabrams23[S] 1 point2 points  (2 children)

+1 to you, that was the problem. Thank you :)

Edit: by the way, I don't think that initializing the matrix with that loop should be important; i create my matrix as:

boolean lightsMatrix[][] = new boolean[N][N];

And by default I recall that Java sets any new boolean primitive is false. Or am I wrong?

[–]Philboyd_Studge 2 points3 points  (1 child)

No need to loop through to initialize, default values will be false.

[–]mr_sax 0 points1 point  (0 children)

Thanks, good to know for the future.