Thread: please help

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    6
    Rep Power
    0

    please help


    Code:
    #include 
    #include 
    
    #define MAX_SIZE 10
    void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
    void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
    void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
    int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size);
    void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size);
    int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size);
    void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size);
    
    int main()
    {
      int life_grid[MAX_SIZE][MAX_SIZE];
      int size = 5;
    
      zapGrid(life_grid,size);
      enterGrid(life_grid,size);
      printGrid(life_grid, size);
      do{
         printf("\n");
         calcNextGen( life_grid, size);
         printGrid( life_grid, size);
         printf("Number of live cells = %d\n", countLiveCells (life_grid, size));
    system("pause");
           }[d]while[/b](2 > 1);
    
        return 0;
    }
    void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size)
      {
      int i,j;
    /*print it out*/
         for(i = 0; i < size; ++i)
           {
               for(j = 0; j < size; ++j)
                  {
                       printf("%2d", countNeighbors(i,j,grid, size));
                   }
                 printf("\n");
            }
      }
    void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
       {
      int i,j;
     /*print it out*/
           for(i = 0; i < size; ++i)
               {
                  for(j = 0; j < size; ++j)
                     {
                         printf("%2d", grid[i][j]);
                      }
    printf("\n");
               }
        }
    
    void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
       {
          int i, j;
    /*fill it with all zeros */
             for(i = 0; i < size; ++i)
          {
              for(j = 0; j < size; ++j)
                  {
                       grid[i][j] = 0;
                   }
             }
         }
    
    void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
       {
        int row, col;
        printf("Please enter row and col for life or -1 -1 to stop\n");
        do{
               scanf("%d%d", &row, &col);
               if (row > -1 && col > -1)
               grid[row][col] = 1;
             }while(row > -1 && col > -1);
    
         }
    
       int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size)
        {
         int count = 0;
    /* top row */
        if (grid[row -1 ][col-1] == 1)
        count += 1;
        if (grid[row -1 ][col] == 1)
        count += 1;
        if (grid[row -1 ][col+1] == 1)
        count += 1;
    
    /*middle row */
        if (grid[row ][col-1] == 1)
        count += 1;
        if (grid[row ][col+1] == 1)
        count += 1;
    
    /* bottom row */
         if (grid[row +1 ][col-1] == 1)
         count += 1;
         if (grid[row +1 ][col] == 1)
         count += 1;
        if (grid[row +1 ][col+1] == 1)
        count += 1;
      return count;
        }
    
    void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size)
       {
          int new_grid[MAX_SIZE][MAX_SIZE]; 
         int count,i, j;
    
         zapGrid(new_grid, size);
    
         for(i = 0; i < size; ++i)
              {
                  for(j = 0; j < size; ++j)
                    {
                       count = countNeighbors(i, j, grid, size);
                       if(grid[i][j] == 1)
                     { /*live cell, does is keep living? */
       if(count == 2 || count == 3)
       new_grid[i][j] = 1; /*congratulations on making it to the next      generation */
                     }else{
    /*dead cell, is there birth?*/
    if(count == 3)
    new_grid[i][j] = 1; /*birth!!!! try to hold back the tears */
                               }
                 }/*for j*/ 
    }/*for i*/
    
    
    /*copy the new back over the old */
    for(i = 0; i < size; ++i)
           {
               for(j = 0; j < size; ++j)
                  {
                      grid[i][j] = new_grid[i][j];
                  }
             }
    }
    
    
    int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size)
        {
            int count=0 ;
            int i;
    
         for (i=0 ; i< size; ++i)
    
         count = count + size;
    
    
    
         return count;
           }
    i think i did the part "countLiveCells " at bottom last is wrong,would u give give me any idea ?thanks.

    p/s : and how do i make the code to ask the user if they want to see the next generation (so they can exit ).thanks alot
    Last edited by traixanha; April 23rd, 2003 at 10:37 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    14
    I understand you are trying to code the "game of life" but that you have posted ALL of your code here isn't helpfull.
    you have to tell what exactly is the problem, what are the rules, and generally wtf you are talking about...
    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222
    Also, traixanha, to make your code more readable, please enclose it in "code" tags, which preserves the indenting.

    Before the first line of code, print a pair of square brackets with the word, "code" between them (minus the quote marks). Then after the last line of code, print a pair of square brackets with "/code" between them. You can do the same thing with "quote", b (for bold), i (for italics), etc.

    For example:
    Code:
    void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size)
    {
        int i,j;
        /*print it out*/
        for(i = 0; i < size; ++i)
        {
            for(j = 0; j < size; ++j)
            {
                printf("%2d", countNeighbors(i,j,grid, size));
            }
            printf("\n");
        }
    }
    Making it easier for us to read makes it easier for us to help.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    6
    Rep Power
    0
    Originally posted by dwise1_aol
    Also, traixanha, to make your code more readable, please enclose it in "code" tags, which preserves the indenting.

    Before the first line of code, print a pair of square brackets with the word, "code" between them (minus the quote marks). Then after the last line of code, print a pair of square brackets with "/code" between them. You can do the same thing with "quote", b (for bold), i (for italics), etc.

    hi there
    thanks for tell me that,i have changed ,hope it will help ,thanks alot
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    14
    I this case i helps just a bit.
    dwise1_aol meant that you should do also that, in addition to what I said (i think...).

    Please tell us what do you wannna do, where is the problem, and what the rules you are playing the game by (there are many versions of this game).

    Help us help you.
    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222

    Re: please help


    Originally posted by traixanha

    ...
    Code:
    
    int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size)
        {
            int count=0 ;
            int i;
    
         for (i=0 ; i< size; ++i)
    
         count = count + size;
    
    
    
         return count;
           }
    i think i did the part "countLiveCells " at bottom last is wrong,would u give give me any idea ?thanks.

    p/s : and how do i make the code to ask the user if they want to see the next generation (so they can exit ).thanks alot
    As Gmorphus said, we're not quite sure what you are trying to do nor what problems you are encountering.

    Regarding the last function (quoted above), what I see it doing is to calculate the square of size (ie, size*size) and returning that value. I assume that is not what you are wanting to do and that the 2D array, grid, is supposed to be used. Were you wanting to nest for loops in order to index each element of grid and add its contents to the running count?

    Also, you do not need to specify the size of an array when it is in a function argument list.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    6
    Rep Power
    0
    hi
    when i try to run the progame , the 0 is present for dead cell, 1 is present for live cell,and in next generation the cell still alive only they have 2 neighbour plus 1 live cell = 3
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    14
    I don't want to be rude, but what can't you understand?
    you HAVE to tell us what you are trying to do, what are the rules of the game, where exactly do you think is the problem etc.

    I realy would like to help you, but I can't.
    Do the said above and we will help.
    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.

IMN logo majestic logo threadwatch logo seochat tools logo