Hi there, I'm a first time poster and first year C programming student.

I am a little stuck on the following question:

-----------------------------------------------------------------------------
"Conway's life is a special kind of game. It isn't really a game - just a spectator sport. It is played on a chess board, where each cell is either alive or dead. Each turn consists of deciding which squares on the board stay the same or change (becoming either alive or dead) and then admiring the new pattern they produce.

A live square stays alive if it has exactly two or exactly three live neighbours, otherwise it dies.
A dead square becomes alive if it has exactly three live neighbours, otherwise it stays dead.

All births and deaths occur simultaneously. The next round depends only on the positions at the end of the previous round. A neighbouring square is one step away in any direction (including diagonals).

Write a program that uses a 20x20 board, initialised to a given pattern, to play the game. After each turn print out the board pattern using a . for a dead square and a * for a live square. Squares off the board are considered permanently dead.

Hint: you need two 2-D arrays (both global variables). Each one being an array of strings. Form the second array from the first array, using the above rules, then copy it back to the first array, print it and repeat. After each repetition ask the user whether they want to continue or stop.

If your program is working correctly this pattern should show a glider hitting a brick

**..................
**..................
....................
....................
....................
....................
....................
........***.........
...................
.........
..........
....................

all the remaining rows are filled with dead squares "

I have given it a very serious attempt, believe me, I've been trying all week and was up all night last night trying to fix the errors in it :( I can see there are many errors showing in the compiler, but whenever I try to fix the remaining errors, more appear!

Here is the code I have come up with so far, I got the basic layout from a tutorial at my college, so that should be ok. My code is as follows:

#include <stdio.h>
#include <conio.h>
#include <string.h>

void display_fctn (char b1, char b2);
void new_board (char b1, char b2);
void copy_back (char b1, char b2);
int neighbours (int p, int q);

char b1[20][21] = {"**.................."
                         "**.................."
                         "...................."
                         "...................."
                         "...................."
                         "...................."
                         "...................."
                        "........***........."
                        "........*..........."
                        ".........*.........."
                        "...................."
                        "...................."
                        "...................."
                        "...................."
                        "...................."
                        "...................."
                        "...................."
                        "...................."
                        "...................."
                        "...................."};


int main() {
    display_fctn();
    printf("Would you like to play? (y/n)");
    c = getch();
    while (c = 'y') {
        new_board();
        copy_back();
        display_fctn(b1);
        c = getch();
    }
}


void display_fctn(char b1, char b2) {
    int i;
    for (i=0; i<20; i++) {
        printf("%s \n", b1[i]);
    }
}

void new_board(char b1, char b2) {
    int i, j;
    for (i=0; i<20; i++) {
        for (j=0; j<20; j++) {
            neighbours(p,q);
            if (b1[i][j] = '*') {
                if (neighbours == 2 || neighbours == 3) {
                    b2[i][j] = '*';
                } else {
                    b2[i][j] = '.';
                } else {
                    if (neighbours == 3) {
                        b2[i][j] = '*';                        /*    b2 corresponding position gets a '*' */
                    } else {
                        b2[i][j] = '.';                                                 /*b2 gets a '.' */
                }
            }
        }
    }
    b2[i][j] = '\0';     /* ----assign \0 */
}

void copy_back(char b1, char b2); {
    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 */
    }
}

Sorry if I waffled on a bit, I would really like to get this running and the errors out of the way, so that I can move on with my programing. Thank you very much to anyone who takes the time to help, I would really appreciate it! :icon_confused: Hopefully one day I could return the favor, you look like a great bunch of people, and a great community to be a part of!
Thanks.

edit: Incase anyone wanted to know, these are the errors showing up in my compiler (A lot, I know): - Every time I go through to try and fix them, more come up.

:31: warning: initializer-string for array of chars is too long
: In function `int main()':
:7: error: too few arguments to function `void display_fctn(char, char)'
:35: error: at this point in file
:37: error: `c' undeclared (first use this function)
:37: error: (Each undeclared identifier is reported only once for each function it appears in.)
:8: error: too few arguments to function `void new_board(char, char)'
:39: error: at this point in file
:9: error: too few arguments to function `void copy_back(char, char)'
:40: error: at this point in file
:41: warning: invalid conversion from `char (*)[21]' to `char'
:7: error: too few arguments to function `void display_fctn(char, char)'
:41: error: at this point in file
: In function `void display_fctn(char, char)':
:50: error: invalid types `char[int]' for array subscript
: In function `void new_board(char, char)':
:58: error: `p' undeclared (first use this function)
:58: error: `q' undeclared (first use this function)
:59: error: invalid types `char[int]' for array subscript
:60: error: ISO C++ forbids comparison between pointer and integer
:60: error: ISO C++ forbids comparison between pointer and integer
:61: error: invalid types `char[int]' for array subscript
:63: error: invalid types `char[int]' for array subscript
:64: error: expected primary-expression before "else"
:64: error: expected `;' before "else"
:73: error: invalid types `char[int]' for array subscript
:79: error: invalid types `char[int]' for array subscript
:79: error: invalid types `char[int]' for array subscript
:89: warning: return-statement with a value, in function returning 'void'
Failure

Compiler errors are compounded. The first error creates another and another, and so on.

For example: I see that you use a variable named c a few lines
below main, but you never declared that variable.
p and q suffers the same problem. Fix those and things start looking better.

you prototype:
void copy_back (char b1, char b2);
However you use it as:
copy_back();
You did not pass any argument, and it needs two.

void display_fctn (char b1, char b2); has the same problem.
Ckeck all arguments in the functions.

Member Avatar for iamthwee
char b1[20][21] = {
                   {"**.................."},
                   {"**.................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"........***........."},
                   {"........*..........."},
                   {".........*.........."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."},
                   {"...................."}
                  };

Also look at your function declarations.

> I can see there are many errors showing in the compiler,
> but whenever I try to fix the remaining errors, more appear!
Always start with the first one in the list. Everything else in the list can be a consequence of the compiler trying to make a guess as to how to fix the problem. Though with practice, you'll be able to filter out those and fix later actual errors as well in the same edit/compile cycle.

Also, it is entirely possible to write your code one line at a time, then press compile to see that it is OK. Then you never get into the situation of having lots of errors and no clue.
http://cboard.cprogramming.com/showthread.php?t=88495
In essence, never write more lines than you're prepared to deal with being wrong.

commented: What more can be said?. Very good. +2

Thanks very much for the help so far guys. Taking what you said on board, I managed to reduce my errors by about half ... am still very lost with the rest though.

I just had a look in my compiler options and the language was set to C++ instead of C, (I'm assuming this should be set to C considering I am learning the C language...right?) now it comes up with some different looking errors and more of them ... doh! This is driving me up the wall, I'm already late in handing this in (i realise this is entirely my fault :P )

I also remembered I never declared my b2 variable, as I wasn't sure how / where to do it. Could anyone show me how to do this?

Also ... it appears I have a conflict between integer comparison which is causing a lot of errors ... but I have no idea how to fix this.

Any further tips / corrections would be very much appreciated.

Thank you once again!

So what's your latest code look like.

All the functions which pass a board back and forth need to be declared thus void display_fctn (char b1[][21], char b2[][21]); > int neighbours(int p, int q); {
Remove that ;

So what's your latest code look like.

All the functions which pass a board back and forth need to be declared thus void display_fctn (char b1[][21], char b2[][21]); > int neighbours(int p, int q); {
Remove that ;

Latest code:

#include <stdio.h>
#include <conio.h>
#include <string.h>

void display_fctn (char b1[][21], char b2[][21]);
void new_board (char b1, char b2);
void copy_back (char b1, char b2);
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 = getch();
    while (c == 'y') {
        new_board(b1, b2);
        copy_back(b1, b2);
        display_fctn(b1, b2);
        c = getch();
    }
}


void display_fctn(char b1, char b2) {
    int i;
    for (i=0; i<20; i++) {
        printf("%s \n", b1[i]);
    }
}

void new_board(char b1, char b2) {
    int i, j, p, q;
    for (i=0; i<20; i++) {
        for (j=0; j<20; j++) {
            neighbours(p, q);
            if (b1[i][j] = '*') {
                if (neighbours == 2 || neighbours == 3) {
                    b2[i][j] = '*';
                } else {
                    b2[i][j] = '.';
                } else {
                    if (neighbours == 3) {
                        b2[i][j] = '*';                        /*    b2 corresponding position gets a '*' */
                    } else {
                        b2[i][j] = '.';                                                 /*b2 gets a '.' */
                }
            }
        }
    }
/*    b2[i][j] = '\0';     /* ----assign  ????? \0 */
}

void copy_back(char b1, char b2) {
    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 ?? */

Latest errors: (Now that I am set the compiler to C instead of C++):
.c:4:
../lib/gcc/mingw32/3.4.0/../../../../include/conio.h:61: error: syntax error before "_COORD"
../lib/gcc/mingw32/3.4.0/../../../../include/conio.h:62: error: syntax error before '*' token
: In function `main':
:61: warning: passing arg 1 of `new_board' makes integer from pointer without a cast
:61: warning: passing arg 2 of `new_board' makes integer from pointer without a cast
:62: warning: passing arg 1 of `copy_back' makes integer from pointer without a cast
:62: warning: passing arg 2 of `copy_back' makes integer from pointer without a cast
: At top level:
:69: error: conflicting types for 'display_fctn'
:7: error: previous declaration of 'display_fctn' was here
:69: error: conflicting types for 'display_fctn'
:7: error: previous declaration of 'display_fctn' was here
: In function `display_fctn':
:72: error: subscripted value is neither array nor pointer
: In function `new_board':
:81: error: subscripted value is neither array nor pointer
:82: warning: comparison between pointer and integer
:82: warning: comparison between pointer and integer
:83: error: subscripted value is neither array nor pointer
:85: error: subscripted value is neither array nor pointer
:86: error: syntax error before "else"
:90: error: subscripted value is neither array nor pointer
:95:22: warning: "/*" within comment
: At top level:
:96: error: syntax error before '}' token
: In function `copy_back':
:101: error: subscripted value is neither array nor pointer
:101: error: subscripted value is neither array nor pointer
: At top level:
:113: error: syntax error before '}' token
Failure


Am I making progress?


Thanks again

> All the functions which pass a board back and forth need to be declared thus
Yes, all of them.
Not just the single line I posted.

And remove conio.h and replace the getch() call with getchar()

As a side note:
Instead of using inlinecode surrounding your code, use code.
The back slash '\" needs to be forward "/" inside the tag.

if (b1[i][j] = '*')

This will always evaluate to TRUE, probably you ment "=="

There's also a problem with a "hanging" else.

Finally, my program is up and running perfectly! Thanks very much to everyone who helped out. =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.