-
Excellent! That did the trick, thank you! Now the program actually runs, asks me to play, after I enter 'y' - it creates another board, then stops =/ But progress at last. I'll have a go at fixing this when I get home. Thank you to everyone that helped me get this far.
~Sarah87
-
Note that this line
if (neighbours == 3)
does not do what it should do...
I suppose it should be
if (p== 3)
-
Ok, I'm a little lost as to what to do next. Basically, I can now run my program - It comes up with the original board, asking me to play. I hit 'y' and it makes a new board with the wrong pattern and won't let me create any more from there (will not ask me to play again, even though I included another getchar in the main function! :S)
I'm fairly sure the line:
Is wrong, but I wasn't sure what else to put there, when I first made it.
I've uploaded a screen shot of how the program looks when run:
http://i19.tinypic.com/6f96yid.jpg
And my latest code:
Code:
#include <stdio.h>
#include <string.h>
void display_fctn (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 neighbours (int p, int q);
char b1[20][21] = {"**..................",
"**..................",
"....................",
"....................",
"....................",
"....................",
"....................",
"........***.........",
"........*...........",
".........*..........",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};
char b2[20][21];
int main() {
char c;
display_fctn(b1, b2);
printf("Would you like to play? (y/n)");
c = getchar();
while (c == 'y') {
new_board(b1, b2);
copy_back(b1, b2);
display_fctn(b1, b2);
c = getchar();
}
}
void display_fctn(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=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] = '*'; /* b2 corresponding position gets a '*' */
}
else
{
b2[i][j] = '.'; /*b2 gets a '.' */
}
}
}
}
b2[i][j] = '\0'; /* b2[i][j] = '\0'; ----assign ????? \0 */
}
void copy_back(char b1[][21], char b2[][21]) {
int i;
for (i=0; i<20; i++) {
strcpy(b1[i], b2[i]);
}
}
int neighbours(int p, int q) {
int count;
/* ((8 ifs?????))*/
if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
count = p + q; /* ??? */
}
return count; /* return count */
}
/* initialise variables eg int i = 0 ?? */
Any tips on where to go from here would be great. Thank you. =)
-
Maybe complete your neighbours function?
-
Let's look at these two functions:
Code:
void copy_back(char b1[][21], char b2[][21]) {
int i;
for (i=0; i<20; i++) {
strcpy(b1[i], b2[i]);
}
}
You shouldn't have to deal with strings in this program. The arrays are char, and char is just a small positive integer, a char with no end of string marker "'\0', is NOT a string.
I don't like messing with string marker's for a single char - imo, it's more work than help. One mistake and you have a harder bug to find and correct.
Try:
Code:
int row, col;
/* This uses 20 rows from 0 to 19? Please adjust as needed. */
for (row = 0; row < 20; row++) {
for (col = 0; col < 20; col++)
b1[row][col] = b2[row][col];
} /* curly braces aren't needed here, but they may be helpful to you */
Code:
int neighbours(int p, int q) {
int count;
/* ((8 ifs?????))*/
if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
count = p + q; /* ??? */
}
return count; /* return count */
}
In neighbors(), you need to test for living neighbors, in every direction. That includes several special cases, where the cell is:
From 12 Noon to 12 Midnight, clockwise:
1) On the top row. You don't want to try and check a cell that is out of bounds, so you can't decrement the row counter, when checking these cells.
So check towards 3, 4:30, 6, 7:30, & 9 o'clock directions, only.
* * * * * * * * * *
* * * * * * * * * *
2) On the top row, in the far right hand corner. You can't decrement the row or increment the column counter.
Check towards 6, 7:30, and 9 o'clock only.
* * * * * * * * * *
* * * * * * * * * *
3) On the bottom row, in the far right hand corner. You can't increment the row counter, or increment the column counter.
Check towards 12, 9, and 10:30 o'clock only.
* * * * * * * * * *
* * * * * * * * * *
4) On the bottom row, anywhere except the corners. You can't increment the row counter.
What directions would you need to check from here?
5) On the bottom row, in the far left hand corner. You can't increment the row counter, or decrement the column counter.
What about from here?
6) On the far top left hand corner. Here, you can't decrement the row or the column counter.
How about from here?
A normal interior cell needs to be checked in directions of 12, 1:30, 3, 4:30, 6, 7:30, 9, and 10:30.
Good luck!
-
Thanks for the help with those Adak, I was quite lost there. At last my program is finished and working perfectly! =D A BIG BIG thanks to all those who helped (you know who you are) I really do appreciate it. Now I can move on and start studying for the final exam (hooray :P)
Thanks again guys =)