Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

incrementing and scope

  Asked By: Bertha    Date: Jan 19    Category: Java    Views: 503
  

I have included some code with a 2d for loop, and an increment when
certain conditions are met (these conditions aren't important to my
problem). For some reason the neighbours will not increment any
further than 1 even when more than 1 is counted, I'd appreciate any
suggestions or any solutions

public int Neighbours(int x_, int y_)
{
int neighbours = 0; //Sets neighbours to 0

for(int y = -1; y < 2; y++)
{
for(int x = -1; x < 2; x++)
{
if (Cell.validPosition((x_ + x), (y_
+ y)) == true) //Checks to see if its on the grid

{
if (x != 0 && y != 0)
{
if (c[(x_ + (x))][(y_
+ (y))].getAlive())
{
neighbours
++; //Increments each time a neighbour is found
}
}
}
}
}
System.out.println("[" + x_ + ", " + y_ + "]
Neighbours:" + neighbours);
return neighbours;
}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Adalricus Fischer     Answered On: Jan 19

It's hard to say what is happening because I don't know what some of
your code  does. You have some function .getAlive() which seems to be
a method of your 2D array c[][]. Arrays aren't classes so they can't
have methods. Is this a typo? Anyway since getAlive() is part of
the if() it still would be difficult to determine the behaviour. One
question though, what happens when you set the limits of the four
loops to (x= -1; x<3; x++),(y= -1; y<3; y++)

 
Answer #2    Answered By: Ada Bailey     Answered On: Jan 19

Firstly, c[][] is an array of objects of cell  type. .getAlive()
method is a method of Cell.

Maybe I was a little vague in sending this code, I just figured I
might have got something fundamental wrong and it wouldn't matter if
anyone who looked at my code  knew what it was supposed to do.

So here's a brief outline:

it is basically a Conway's Life Generation program, and the method I
have posted checks each cell that is passed into the method for any
neighbouring alive cells.

The reason I used the for loop

> > for(int y = -1; y < 2; y++)
> > {
> > for(int x = -1; x < 2; x++)

is so each neighbouring cell is checked, like so:
xxx
xox
xxx

x being the neighbours and o being the coordinates passed in to the
method.

if I exchange < 2 for < 3 it would chack in this fashion
xxxx
xoxx
xxxx
xxxx

Which isn't what I want.

I am still having difficulty but I have narrowed the problem down to
this line (which is needed for the chack to be done correctly)

if (x != 0 && y != 0)

Basically this line makes sure that it isn't checking ITSELF for a
neighbour then lets the variable increment. Without this line, I
would get 1 too many neighbours each time  I checked an alive cel for
neighbours (both alive and dead cells need to be checked for the
program to run correctly). But for some strange reason, this very
line is stopping the incrementation from going past 1!

If you can help I would REALLY appreciate it, as I am totally screwed!

 
Didn't find what you were looking for? Find more on incrementing and scope Or get search suggestion and latest updates.




Tagged: