Java comparison issue

Status
Not open for further replies.

lordhughes

Seniorius Lurkius
37
I am implementing a basic version of Conway's game of life but when applying the rules I come across a weird problem and I hope that someone here know whats going on.

It is just a basic if else if statement but values change depending if I compare them or not.
Code:
public void rules() {
        if(neighbours > 0) System.out.println(neighbours);
        if(alive && (neighbours > 3 || neighbours < 2)) {
            alive = false;
        }
        else if(!alive && neighbours == 3) {
            alive = true;
        }   
    }

The value is printed before any comparison is carried out but having the if else if changes the result of the print.
with the if else if the results are

Code:
1
1
1
2
1
1
2
1
1
1

and without

Code:
1
1
1
2
1
2
3
2
3
2
1
2
1
1
1

Does anyone have any idea whats going on?

Cheers,
John
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
neighbours

* Complex and Subtle:
Are you running this in a multi threaded environment?
The additional operations being performed could alter the behaviour

* Boring and Most likely:
The code *calling* this function depends on the alive state, by testing it you alter the state as you go along. You may not have *intended* that to be the case but it seems the most likely reason.
 

lordhughes

Seniorius Lurkius
37
Thanks for the quick reply.

The class which has the rules() method only has one externally alterable variable which is neighbours, a check is done on the alive boolean but it is not changed, the state of the cell is handled by the cell class itself which alters the state of alive.

I have not started with threads yet as this problem came up so still in early stages.
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
It also matters in your Life if you're modifying the board you're trying to count neighbors on. You have to have two arrays... one is 'current state' and the other is 'next state'. If you only have one board, you're changing the current state while you're trying to count neighbors on the current state. Not that a println should have anything to do with that, though.
 

lordhughes

Seniorius Lurkius
37
I do have two boards, Cell's are retrieved from the current board, altered then placed in the next state board. Leaving the current board unchanged. After each iteration current becomes next state and it repeats.

The neighbour value can not become negative, the method which counts the neighbours has a count initiated at 0 and the only operation is ++.

The values I posted in my original post where taken on a game board which only had a blinker on it (3 alive cells in a vertical line) so I knew what the values should be and they are correct when the rules are not being applied.
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
There isn't enough information to find the problem. The value of alive feeding back into the the value of neighbor and the number of times this function is called.

You can prove to yourself that it isn't the conditional part of the function that is changing anything by commenting out the "then" parts of the if-then-else-then statement.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I do have two boards, Cell's are retrieved from the current board, altered then placed in the next state board. Leaving the current board unchanged.

This is a guess, because you're not supplying enough info:
This is java, so one assumes that this is a class. How are you 'moving' it to the other board.
Surely you would have to *replace* the instance in the original array with a copy.

There are a few ways to skin this cat but many depend on you intimately understanding the concepts.

It is best for you to keep the objects which represent state and the means to calculate their next value *immutable*

thus the call to decideYourStateOnTheNextTick()[1] should return a bool (Faramir states earlier) and then you simply rebuild the next state array with newly created ones. this is actually pretty inefficient but since you are currently *incorrect* I wouldn't worry about that and would instead focus on it this way.

[1] why is it called "rules" that's awful since it in no way implies mutation of state, or indeed much at all
 
Ok sorry for the lack of information I am terrible at explaining things. I am aware that the name rules is terrible.

I will try and explain it all.

A 2D grid is made up of cell objects, each cell has a state which is determined by the boolean value alive, the state of alive depends upon the number of alive neighbours that cell has. Cell has two variables neighbours and alive. Alive is only changed within the class, it can only be checked by an external class. Neighbours is mutable from outside.

The Grid class has two game boards, current and next. Neighbourhood information is taken from current table for a cell, then the cell is copied to the next grid and repeat until all cells have been checked and copied. The grid class only changes the neighbour variable (int) inside the cell object, once the number of alive neighbours has been set in the cell the rule() (need better name) method is called and the state of the cell checked and changed. Once all the cells have been copied to the next grid the current is replaced with next.

I am aware my program structure is not brilliant.

Thank you for your patience and feedback.
 
Status
Not open for further replies.