I need to write a simple program as an assignment on the terms of the game "Life." All field values ​​are stored in my array of Boolean values. I have written a method that checks cells around a cell under different conditions and increases the counter +1 if it finds a “live” (true) cell. In addition to location conditions, there is naturally a condition that takes a value from an array. And if this value is false, then execution for some reason stops and does not go any further. In the example below, "1" will not be displayed in the console and the program will not terminate (it will hang for something) if fieldLikeBooArray [length - width + point] is false:

public int checkPointsAround(int point, int width, boolean[] fieldLikeBooArray){ int alivePointsAround = 0; int length = fieldLikeBooArray.length; int count = 0; if (count == 0 && point - width < 0 && fieldLikeBooArray[length - width + point]) alivePointsAround++; //верхний сосед первая строка else if (count == 0 && fieldLikeBooArray[point - width]) alivePointsAround++; //верхний сосед остальные строки System.out.println(1); return alivePointsAround;} 

Why? Moreover, if you break it into two separate conditions (which seems very crooked), it works. Example:

 if (point - width < 0) { if (fieldLikeBooArray[length - width + point]) alivePointsAround++; //верхний сосед первая строка } else if (fieldLikeBooArray[point - width]) alivePointsAround++; System.out.println(1); 

Perhaps the question is extremely simple, but for the second day I can not understand where I was wrong. Help! If necessary, I can lay out all the code.

  • The condition can not stop the execution of the program. And put braces in the body of the condition, it helps not to make mistakes - ArchDemon am
  • Yes. Absolutely agree. The condition should not affect this. They are either executed. Or not. And the program runs further. But for some reason this is not the case. If fieldLikeBooArray = true, then flies, and if false, then it does not execute further ... and the output "1" does not occur. I accept the comment on parentheses, but in this situation it does not affect the result. - MarsiK76

1 answer 1

Looks like I understood what the error is. It is wrong to take part of the field as a condition for execution. Because because of this, it is from a particular case (if the cell is "false") that goes to a common one and falls there with an ArrayIndexOutOfBoundsException.

Why the program does not end and the error does not fall into the console is a mystery to me.