There need to be 8 IF statements within my next_calls function. One IF for each of the neighboring cells. At the moment I have only looked at [p-1][q+1], which I think is the north east cell.
There need to be 8 IF statements within my next_calls function. One IF for each of the neighboring cells. At the moment I have only looked at [p-1][q+1], which I think is the north east cell.
What are the variables p and q - are those row and col, then?
Been awhile since you posted your code. Please post your most recent code.
The if() statements are not difficult. All of them are simply slight modifications of the one you have, as listed earlier in this thread.
The only thing left is to add the test for the edges. Post up your code and I'll be back in one hour.Code:if(neighbor cell at 12 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 2 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 3 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 4 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 6 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 8 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 9 o'cock) is the same as you, sameVariable++ else differentVariable++ if(neighbor cell at 10 o'cock) is the same as you, sameVariable++ else differentVariable++
My code hasn't changed. I just can't get my head around all the errors.
I can't even write the additional IF statements.
Hey there again
I suggest that you spend some time thinking about how to find if the element north is on not worrying about whether the element is at an edge or not (that can come in later) -> Hint: Use array notation.
Can you make a if statement that says, "if Element North is on, printf("On"); Else printf("Off");" - Show us your code that does this when you have done that
After that you need to think: Can you then do it for East, South and West? And then North-East, South-East, South-West and North-West.
Fact - Beethoven wrote his first symphony in C
Is this even remotely correct?Code:int next_cells(int p, int q) { int count; if (p != 0 && q != 19 && b1[p-1][q+1] == '*') { count = p + q; } else { if (p != 0 && q != 19 && [p-1][q-1] == '*') { count = p + q; } } } return count; }
I thought that I'd clarify my North/South notation
[1][2][3]
[4][5][6]
[7][8][9]
If '5' is the element under question: '2' is north, '8' South, '6' East, '3' North-East, ect...
Think -> If this was a 2D array, how would you test to see if the element North-West was on?
Fact - Beethoven wrote his first symphony in C
You are starting to be on the right track -
You are using p as the row and q as the column -> You should rename them "row" and "col" so it is easier for you to visualise the problem
Fact - Beethoven wrote his first symphony in C
b1[p-1][q+1] is North-East
Fact - Beethoven wrote his first symphony in C
Some pointers on your code:
Code:int next_cells(int p, int q) { int count; //Needs to start at 0 /* The checks here are to make sure 0 <= p <= 19 * and 0 <= q <= 19. That is why you are testing * for p != 0 and q != 19. I think that you have * missed that point in your second test */ if (p != 0 && q != 19 && b1[p-1][q+1] == '*') { /* Why isn't this "count = count + 1; ?*/ count = p + q; } else { /* This should not be in an 'else' for the first * 'if' statement, but another 'if' statement all * together. As it is at the moment, this code * will only excecute if North-East is not on */ if (p != 0 && q != 19 && [p-1][q-1] == '*') { /* Why isn't this "count = count + 1; ?*/ count = p + q; } } } return count; }
Last edited by Click_here; 09-26-2012 at 10:33 PM. Reason: 1) Clarity 2) Something I noticed (green)
Fact - Beethoven wrote his first symphony in C
Well...
I'll use r for row and c for column. P and q don't work as clearly, imo.
Code:int CountNeighbors(int r, int c) { //array is global? if not, add it as a parameter here int count = 0; if(r>0) { //12 o'clock if(array[r-1][c] == '*') count++; } if(r>0 && c<SIZE-1) { //2 o'clock SIZE-1 checks that c will not go out of bounds, in the next line of code if(array[r-1][c+1] == '*') count++; } //and add the rest of the if statements here, working clockwise (or in some ordered fashion) //and lastly return count; }
Thanks guys!
I'll implement all of these changes now, and I'll post the new code as soon as I am done.
Code:#include <stdio.h> #include <string.h> #include <conio.h> void display(char b1[][21], char b2[][21]); void new_board(char b1[][21], char b2[][21]); void copy_back(char b1[][21], char b2[][21]); int count_neighbours(int r, int c); char b1[20][21] = {"**..................", "**..................", "....................", "....................", "....................", "....................", "....................", "........***.........", "........*...........", ".........*..........", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "...................."}; char b2[20][21] = {"**..................", "**..................", "....................", "....................", "....................", "....................", "....................", "........***.........", "........*...........", ".........*..........", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "....................", "...................."}; int main() { char c; display(b1, b2); printf("Hi! Would you like to play the Game of Life? (y/n)"); c = getchar(); while (c == 'y') { new_board(b1, b2); copy_back(b1, b2); display(b1, b2); c = getchar(); } } void display(char b1[][21], char b2[][21]) { int i; for (i=0; i<20; i++) { printf("%s \n", b1[i]); } } void new_board(char b1[][21], char b2[][21]) { int i, j, p; for (i=0; i<20; i++) { for (j=0; j<20; j++) { p = count_neighbours(i,j); if(b1[i][j]=='*') { if (p ==2 || p == 3) { b2[i][j] = '*'; } else { b2[i][j] = '.'; } } else { if (p == 3) { b2[i][j] = '*'; } else { b2[i][j] = '.'; } } } } b2[i][j] = '\0'; } void copy_back(char b1[][21], char b2[][21]) { int i; for (i=0; i<20; i++) { strcpy(b1[i], b2[i]); } } int count_neighbours(int r, int c) { int count = 0; if(r > 0) { if(array[r-1][c] == '*'); count++; } if(r > 0) { if(array[r-1][c+1] == '*'); count++; } if(r > 0) { if(array[r][c+1] == '*'); count++; } if(r > 0) { if(array[r+1][c+1] == '*'); count++; } if(r > 0) { if(array[r+1][c] == '*'); count++; } if(r > 0) { if(array[r+1][c-1] == '*'); count++; } if(r > 0) { if(array[r][c-1] == '*'); count++; } if(r > 0) { if(array[r-1][c-1] == '*'); count++; } }
Last edited by Anhur; 09-26-2012 at 11:39 PM.
You have (hopefully) counted up the "neighborhood" around each cell. But now you need to return that count, so the game can be adjusted, and reflect the changes.
maybe something like:
This should all go inside your main while game loop (or in a function called from that while loop).Code:pseudo code idea: for(each row) { for(each column) { count = count_Neighbors(row, col) if(count > Some Amount) { code to handle that } } }
Code:int main() { char c; display(b1, b2); printf("Hi! Would you like to play the Game of Life? (y/n)"); c = getchar(); while (c == 'y') { new_board(b1, b2); copy_back(b1, b2); display(b1, b2); c = getchar(); for (r) { for (c) { count = count_neighbours(r, c); if (count = ?) ? } }
Not
if( count = ?)
but
if( count == ?) //you need TWO equal signs for a comparison in C
Look at the rules of your version of the Game of Life, and see what should be used in place of the ? Some number should trigger a change in the board cell value.
You need to run through the specifics of a for loop:
Code:for(r=0;r<numberOfRowsOnYourBoard;r++) { for(c=0;c<numberOfColumnsOnYourBoard;c++) {