• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

I don't understand Programming at all. Help!

Status
Not open for further replies.

Salamando

Member
Now that I got that done, I don't have a clue what to do next. I know I'm supposed to have this array change based on the Game of Life's rules, but I don't know how to implement those rules into java.

• A dead cell with exactly three live neighbors becomes live.
• A live cell with exactly one live neighbor becomes dead.
• A live cell with more than three live neighbors becomes dead.

Technically, you have an array of arrays. Or a matrix. The two for loops you already have are used to go through each element in that matrix and set them to a randomized boolean. Now what you need is a way to go through each element in that matrix and determine if it should be live or dead.

Consider a random entry at i,j (or gol[ i ] [ j ] ). To figure out if it lives or dies, you need to look at it's neighbors. How do you think you'd do that?

For CS? Maybe. I'm just saying, eventually there's a shift to the theoretical that can be applied to a wide variety of things.

Graduated with CS, and I never stopped programming. Sure, some courses were more theory, but a lot still involved coding. Even my compilers class. They had us build our own compilers.
 
For CS? Maybe. I'm just saying, eventually there's a shift to the theoretical that can be applied to a wide variety of things.

I think he means "once you get into the work force, it'll be all applied (as opposed to theoretical) again"

In which case he's right. I imagine that'll hold true for most people who don't go further into graduate studies with it, at least.
 
Technically, you have an array of arrays. Or a matrix. The two for loops you already have are used to go through each element in that matrix and set them to a randomized boolean. Now what you need is a way to go through each element in that matrix and determine if it should be live or dead.

Consider a random entry at i,j (or gol[ i ] [ j ] ). To figure out if it lives or dies, you need to look at it's neighbors. How do you think you'd do that?

I would need if statements right? Like, if (0,1) & (0,2) & (1,0) are alive then have that cell die. Otherwise keep it alive? Something like that?
 

Salamando

Member
I would need if statements right? Like, if (0,1) & (0,2) & (1,0) are alive then have that cell die. Otherwise keep it alive? Something like that?

You could do that, but consider every combination of cells possible. A cell in the middle has 8 neighbors. Computing every combo would take awhile. Instead just count the number of live neighbors. Do your if statements based on that value.

So if you're looking at gol[ i ] [ j ], how would you the live-ness of it's neighbors? Or even a single neighbor?
 

OneEightZero

aka ThreeOneFour
I'm currently learning Java for a CS111 class. It's not been too difficult so far, but I can tell the difficulty is going to ramp significantly over the next few weeks.
 

IceCold

Member
For CS? Maybe. I'm just saying, eventually there's a shift to the theoretical that can be applied to a wide variety of things.

When you start working you won't be making your own avl trees or priority queues. Neither will you implement your own depth first search graph algorithms or quick sorts. All of this stuff is more important for the interview process than the actual job (since you can just google all this crap when you need them). What will be more important is knowing how to design scalable, maintainable code.
 
You could do that, but consider every combination of cells possible. A cell in the middle has 8 neighbors. Computing every combo would take awhile. Instead just count the number of live neighbors. Do your if statements based on that value.

So if you're looking at gol[ i ] [ j ], how would you the live-ness of it's neighbors? Or even a single neighbor?

When going over this lab, this is what the instructor gave us:
Code:
int neighbors(int r, int c, boolean[][]gol, int N) {
	int count=0;
			for(int i=r-1; i<=r+1; i++)
			for(int j=r-1; c<=r+1; c++)
			{	if(i>=0 && j>=0 && i<N && i<N)
				if((i!=r||j!=c)&& gol[i][j])
					count++;
			}
	return count;
Would that work?
 
First I believe it sets the neighbors to the 4 values in parentheses. Next it just sets a counter to be add onto if necessary.

What I'm thinking is that the rest basically counts up all of the alive cells around the current one and says that if all of the conditions are met, then the count# would increase by 1.

Hopefully, I'm at least in the ballpark. xD

Is this part wrong?

Code:
for(int j=r-1; c<=r+1; c++)

and this
Code:
&& i<N && i<N)
The first part, yes, those "r"s should be "c"s. Thanks
The second part should have a j where the second i is. My own handwriting is hard to read sometimes xD
 

Salamando

Member
Is this part wrong?

Code:
for(int j=r-1; c<=r+1; c++)

Yeah, it's wrong. Wonder if it was intentional on the part of the professor?

First I believe it sets the neighbors to the 4 values in parentheses. Next it just sets a counter to be add onto if necessary.

What I'm thinking is that the rest basically counts up all of the alive cells around the current one and says that if all of the conditions are met, then the count# would increase by 1.

Hopefully, I'm at least in the ballpark. xD

Ehhh. It's not setting the neighbors to anything. There's really three questions you should be able to answer here...

1) What are the for loops doing? What are all the elements they're looking at/ all the possible values for i and j?

2) What does the first if statement do?

3) What does the second if statement do?
 
First I believe it sets the neighbors to the 4 values in parentheses. Next it just sets a counter to be add onto if necessary.


The code defines a function called 'neighbours' which returns an int. You could call it anything instead of 'neighbours', like 'ByronicHerosNeighboursCount' but don't call it that.

You probably want to read something like this: ( I just found it but I assume it's ok)
http://www.tutorialspoint.com/cplusplus/cpp_functions.htm
 
Ehhh. It's not setting the neighbors to anything. There's really three questions you should be able to answer here...

1) What are the for loops doing? What are all the elements they're looking at/ all the possible values for i and j?

2) What does the first if statement do?

3) What does the second if statement do?

"Set" may have been the wrong word,would it be more accurate to say that "neighbors" is defined by what's in the parentheses?

The for loops are (the first one) int i is set to r -1 which would be one of the neighbors minus 1 and while i is greater than or equal to r+1, i will increase by 1. The second one would be the same as the first except with different variables.

During the first if loop, is both i and j are less than or equal to 0 as well as being less than n then the counter would increase by one. Also if i is not equal to r or j is not equal to c AND gol[j](kinda lost here) then the counter would increase by one.

Am I getting warmer?
 
r is the row of the cell that you want to find the neighbour count of, c is the column of it.
It checks a grid of 9 cells around that one, with ifs to ignore cells beyond the edges of the square array. (less than 0 or greater or equal to N)


In this:
Code:
if((i!=r||j!=c)&& gol[i][j])

first half is always true except for the middle cell of the 3x3 grid, so it will ignore the cell at r,c.


Do you understand why there are two for loops after each other? the second one should be indented really. And inside parentheses I think.
 
Well, the code inside does yes. I think it should look like this

Code:
			for(int i=r-1; i<=r+1; i++) {
			    for(int j=c-1; j<=c+1; j++)
			    {	if(i>=0 && j>=0 && i<N && j<N)
			    	    if((i!=r||j!=c)&& gol[i][j])
					count++;
		    	    }
                        }
 
Okay, thanks.

I think that's it for now guys, thanks. I was seriously considering giving up and dropping the class if I didn't get at least a little understanding about what I was doing.

If I have any more questions, I will be sure to ask in this thread or the programming one.

Thanks a bunch guys ^_^
 
Status
Not open for further replies.
Top Bottom