0

I'm trying to make 'The Game of Life' in Java. I made a 2d matrix and filled it with boolean values.

Now I'm trying to count the boolean values of adjacent cells that are true, but when I use =, I get all of the cells surrounding it (eg: a cell in the middle gives 8, a cell on a corner gives 3) and when I use ==, I just get all 0's.

Example if statement (first one works, second doesn't work):

if(!celloc.equals("URC") && !celloc.equals("RightB") && !celloc.equals("LRC")) {
            if(current[i][j+1] = true) {
                life++; // right
            }}
8
  • You should try if(current[i][j+1] == true) { Commented Jan 21, 2019 at 18:31
  • 2
    If current is the array that contains the boolean values then if(current[i][j+1]) is enough Commented Jan 21, 2019 at 18:31
  • Tried both,they give me 0's. Commented Jan 21, 2019 at 18:32
  • Your question is not clear, can you please try to explain better. Commented Jan 21, 2019 at 18:34
  • Whats not clear exactly? Commented Jan 21, 2019 at 18:36

1 Answer 1

1

A single "=" is for assignment, use "==" when testing for equivalency. Also, if "current[i][j+1]" is a boolean you can simply type:

if(current[i][j+1]) {

to test if that value is true. You may be getting 0 because you may not be getting to that if statement. Try adding some output to see if your first if statement is ever actually true.

Sign up to request clarification or add additional context in comments.

4 Comments

I must be getting to the if statement because = lets life count for all adjacent values. Tried ==,gives me 0's. same as if(current[i][j+1]).
Try other debugging statements to see what is going on. Make sure the values of i and j are actually what you expect them to be when it is checking, print out "current" if it is not too big. It sounds like you never actually check a value of current that is true.
@user569685 : using a single '=' fulfills the if condition because of the value you used in the assignment - true. The value returned by an assignment is the assigned value. Using ' = true' will therefore always result in the condition being true.
I print out current previously in the code,also i use its coordinates so i know they're accurate. I dont wanna post the whole code because its 200ish lines.

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.