2

I took a programming class, and I'm revisiting old programs that I did not quite get right. This one is a Game Of Life program, and I have a question about code cleanup.

I need to make sure that an array element is in bounds before checking whether its neighbor's boolean value is true or false. I have a statement to check if firstGen[0][0]'s top-left (up one row, left one column) is in bounds. Is there an easier or more elegant way to check if an element is in bounds or to restrict the element checks to the boundaries of a given array without using four && conditionals per if statement?

Note that I have only changed the first if statement thus far, so there may be errors elsewhere. I also excluded the boundary checks for the other neighbors.

    public static boolean[][] generation(boolean[][] firstGen)
    {
    int length = firstGen.length;
    boolean[][] newGen = new boolean[length][length];

    for (int j = 0; j < firstGen[0].length; j++)
        { for (int i = 1; i < firstGen.length; i++)
            {
                int count = 0;
                if  ((i-1 >= 0) && (i-1 < length) && (j-1 >= 0) && (j-1 < length)) //top-left element exists
                    { if  (newGen[i-1][j-1] == true) count++; } //increment `count` if top-left element is true

                if ((newGen[i][j] == false) && (count == 3)) newGen[i][j] = true;
                else if ((newGen[i][j] == true) && (count == 1)) newGen[i][j] = false;
                else if ((newGen[i][j] == true) && (count > 3)) newGen[i][j] = false;
                else break;
             }
        }
        return newGen;
      }
0

2 Answers 2

3

If i and j are in bounds, then you know for sure that i - 1 < length and j - 1 < length are both true.

Also:

  • i - 1 >= 0 can be written i > 0
  • if (condition == true) can be rewritten if (cond)

So you could replace:

if  ((i-1 >= 0) && (i-1 < length) && (j-1 >= 0) && (j-1 < length)) //top-left element exists
    { if  (newGen[i-1][j-1] == true) count++; } //increment `count` if top-left element is true

by:

//increment `count` if top-left element is true
if  (i > 0 && j > 0 && newGen[i-1][j-1]) count++;
Sign up to request clarification or add additional context in comments.

3 Comments

+1 And suddenly it's so simple :-) I would additionally recommend what he'll need in the next step: an elegant way to go through all the neighbors without code repetition.
@assylias Sometimes Java solutions are so simple. They're the kind tat make you just wanna bang your head into a wall for make things unnecessarily complicated. I would also like to thank you for the reminder about true and false boolean conditionals. Again, such a derp thing.
@Marko I'm not sure I have it quite solved, but I have a mental solution to handle that next step as well. I'm thinking nested for loops.
2

That's the best way I can think of to check if its out of bounds, but an alternative method in general, and one that I think gives programs like the Game of Life more exciting outcomes, is adding periodic boundaries. Basically this means that if you walk off one edge, you end up on the other side (like in pac-man). It sounds complicated, but really all it takes is the % function, which returns the remainder of division between the two numbers given.

So:

27 % 5 = 2;

So for adding periodic boundries you would update x and y positions like this:

x = (x + xStep + horizontalSize) % horizontalSize;
y = (y + yStep + verticalSize) % verticalSize;

Where xStep and yStep are +1 or -1 depending on what direction you want to go. (this works nicely with a for loop) The addition of the size is to make sure you go below zero when you get close to borders.

Then you never have to worry about messy border conditions, everything simply overlaps. No need to check each and every border. I hope this makes sense. Please ask for clarification if not. I've used this more for random walker programs but the idea is the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.