Conway's Game of Life

This is a discussion on Conway's Game of Life within the C Programming forums, part of the General Programming Boards category; There need to be 8 IF statements within my next_calls function. One IF for each of the neighboring cells. At ...

  1. #16
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    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.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    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++
    The only thing left is to add the test for the edges. Post up your code and I'll be back in one hour.

  3. #18
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    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.

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,360
    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

  5. #20
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    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;
    }
    Is this even remotely correct?

  6. #21
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,360
    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

  7. #22
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,360
    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

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,360
    b1[p-1][q+1] is North-East
    Fact - Beethoven wrote his first symphony in C

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,360
    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

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Anhur View Post
    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;
    }
    Is this even remotely correct?
    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;
    }

  11. #26
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    Thanks guys!
    I'll implement all of these changes now, and I'll post the new code as soon as I am done.

  12. #27
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    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.

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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:
    Code:
    pseudo code idea:
    
    for(each row)  {
       for(each column) {
         count = count_Neighbors(row, col)  
         if(count > Some Amount) {
            code to handle that
         }
       }
    }
    This should all go inside your main while game loop (or in a function called from that while loop).

  14. #29
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    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 = ?) 
                         ?
        }
    }

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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++) {

Page 2 of 3 FirstFirst 123 LastLast
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, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21