Thread: Conway's Game of Life

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Conway's Game of Life

    Hi there,

    I am a first year Programming student, and this is my first time asking for assistance. Due to Programming not being what I expected, this will probably be the last time I ever do any form of Programming. I have been put off for life.

    Basically we have been given a task where we have to write a C version of John Conway's Game of Life, using a 20x20 board and the pattern shown in my code (below).

    Code:
    #include <stdio.h>
    #include <string.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 next_cells (int p, int q);
     
    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 = next_cells(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 next_cells(int p, int q) {
        int count;
        if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
            count = p + q;
            }
        return count;
    }
    Unable to get the program working myself, I spoke to the lecturer who gave the following tips, however I have been unable to implement them successfully. Some extra guidance would be really amazing.

    * I should use getch rather than getchar (since it doesn't clear the buffer).

    * Basically my next_calls function is awful, and I am only using 1 IF when I should be using another 7, because I am only counting one of the neighboring cells.

    * I need to increment my count variable.

    * In every one of the 8 IFs, I should be protecting against the possibility that [p][q] is an edge entry on b1.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What did you expect programming to be? The other month I made a Conway's GOL program and loved it. It's such a shame to hear that you didn't enjoy it.

    * I should use getch rather than getchar (since it doesn't clear the buffer).
    "getch()" doesn't wait for a new line character. When you use getchar(), the function waits for a new line character and then leaves the new line character in the stdin. You will need to include conio.h.

    * Basically my next_calls function is awful, and I am only using 1 IF when I should be using another 7, because I am only counting one of the neighboring cells.
    You need to look at
    row -1, column -1
    row 0, column -1
    row +1, column -1
    row -1, column 0
    row +1, column 0
    row -1, column +1
    row 0, column +1
    row +1, column +1

    That is every neighbour.


    * I need to increment my count variable.
    Are you sure that they didn't say initialise? i.e. count=0;

    * In every one of the 8 IFs, I should be protecting against the possibility that [p][q] is an edge entry on b1.
    I can see that you are trying to do that with the p!=0 and q!=19, but you should test if p!=19 and q!=0 as well
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    I would have enjoyed my Programming class more, if I didn't have the added stress of three other papers throwing assignments at me. I can understand why there is such a high drop out/failure rate, especially for this class. I can't learn under pressure.

    Thanks for the information you provided Click_Here.
    Last edited by Anhur; 09-25-2012 at 06:22 PM.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I also thought that the fact that your teacher said that your function was "awful" is a bit rough - Words like that can curb anyone's enthusiasm.
    Sure, it need a bit of work, but a good teacher should make you want to work on it.

    OK, so now I got that out of the way!

    I think that the first thing you should do is pick a random spot in the middle, give it (say) 3 neighbours and see if you can get your next_cell function to find them - Move them around and see if you are not missing anywhere around the element.

    Only after you have that working, pick a spot near the edge - add if statements that can tell if the element is at the edge - "if current element is not on column 0, check column -1"

    If you get into any troubles with your code, post it and we'll help you.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by Click_here View Post
    "getch()" doesn't wait for a new line character. When you use getchar(), the function waits for a new line character and then leaves the new line character in the stdin. You will need to include conio.h.
    Not quite. The terminal (or some equivalent, depending on OS) waits for a complete line, then getchar() reads ones character from that line. The rest of the characters are left in the buffer.

    See FAQ > Flush the input buffer - Cprogramming.com for a method to clear the input buffer without using non-standard hacks like fflush(stdin) and getch().

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by christop
    fflush(stdin)
    You should never use fflush(stdin).
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    I need to protect against the possibility that [p][q] is an edge entry on b1. I not sure how I go about writing this.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You have already started this in your code:
    Code:
    if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
    p == 0 is on the edge -> q == 19 is on the edge

    Don't forget to check for p == 19 and p == 0 (other edges)
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    I'm just going to have to hand this in as is. This particular assignment will fail, but I won't end up with an incomplete. I know what this program has to do, and understand how the game works - I just can't write it.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It might help writing in comments saying what you want the program to do at particular points

    /* Check to see if not on the edge */
    /* Check to see if row-1 column -1 is set */

    That way the marker can give you extra points even though the program isn't working.

    It might also help if after you hand the program in, that you ask your teacher for a time to go through your program with you and get it working - I'm sure that they'll appreciate the effort.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When I did my own version of this program a few months ago, I used a common method for dealing with edge conditions. Simply add an extra border to the array, and define those cell as always being "dead." This way, when you're testing an edge, you can use the same tests you use everywhere else since you have an extra layer (i.e. you're protected against going out of bounds of your array).

    Code:
    5x5 board (5x5 array) that requires special cases on edge conditions:
    
      .....
      .....
      .....
      .....
      .....
    
    
    
    5x5 board (7x7 array) with extra border so you will never encounter an edge condition:
    
     xxxxxxx
     x.....x
     x.....x
     x.....x
     x.....x
     x.....x
     xxxxxxx     x = not part of the board, always dead

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    I understand what you are saying, however I really would not have a clue about how to add an extra border to the array let alone finish this assignment. I have another one I have to start. Thanks for your input anyway.

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Anhur View Post
    * I should use getch rather than getchar (since it doesn't clear the buffer).
    That's just wrong ( both normally and ethically )!
    "getch()" doesn't care about the buffer, it grabs the character the user just pressed.
    "getchar()" doesn't clear the buffer, it just grabs one character from it( and removes it of course ).
    ( Your teacher told you these?? OMG!!! )

    You can use "getchar()" to clear the input buffer, but inside a loop, not by itself.
    Last edited by GReaper; 09-26-2012 at 02:47 PM.
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    Would someone be able to help me write the IF Statements?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Anhur View Post
    Would someone be able to help me write the IF Statements?
    I haven't been following your thread Anhur, but I'm familiar with Conway's Game of Life. Can you be more specific about your problem?

    Are you having trouble with your edges (going out of bounds), or ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text Based Conway's game of Life in C
    By spideysagnik in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2011, 07:10 AM
  2. Recomposing the Conway's Game code in C
    By Noya in forum C Programming
    Replies: 8
    Last Post: 05-20-2011, 09:04 PM
  3. John Conway's Game Of Life
    By O Mighty Nips in forum C Programming
    Replies: 3
    Last Post: 05-14-2011, 11:30 PM
  4. Replies: 20
    Last Post: 05-24-2007, 01:06 AM
  5. Conway's Life
    By prxom1 in forum C Programming
    Replies: 1
    Last Post: 03-27-2002, 02:13 PM