Like Tree2Likes

Game of life program

This is a discussion on Game of life program within the C Programming forums, part of the General Programming Boards category; Well, did you try it? I see 25x25 grids now. They look like they change from generation to generation. I ...

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Well, did you try it?

    I see 25x25 grids now. They look like they change from generation to generation. I haven't analyzed to see if it's doing the right thing, (mostly because I don't know what that is) but it's doing something.
    Last edited by mike65535; 05-04-2011 at 09:11 AM.

  2. #17
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    @mike65535
    Where did you add "printf("\n")"
    Please share source code of that part...

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    You added it!

  4. #19
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You're almost there. When you initialize the array[][] you should also assign the same values to n_array[][]. Otherwise n_array will have garbage in its outer edges because they are never initialized. This garbage will be placed in the array after 1st generation.

  5. #20
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by nonoob View Post
    You're almost there. When you initialize the array[][] you should also assign the same values to n_array[][]. Otherwise n_array will have garbage in its outer edges because they are never initialized. This garbage will be placed in the array after 1st generation.
    I didn't understand what you mean.
    I think,I have already done what you want
    Actually, the output is not working properly.
    @nonoob
    Please show over the code
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    #define ROWS 25
    #define COLS 25
    void game_of_life(char o_arr[][COLS], char n_array[][COLS]);
    int checkx(char arr[][COLS],int row, int col);
    int main(void)
    {
    	char array[ROWS][COLS],n_array[ROWS][COLS];
    	srand(time(NULL));
    	int i, j, r_integer[ROWS][COLS], num_gen;
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			r_integer[j][i] = (rand() % 100) > 50 ? 1 : 0;
    		}
    	}
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			  if(r_integer[j][i] == 0)
                      array[j][i]=' ';
                  else
    				  array[j][i]='X';            
    			}
    		}			
    	for(i=0;i<ROWS;i++){
    		printf("\n");
    		for(j=0;j<COLS;j++){
    			printf("%c", array[j][i]);
    		}
    	}
    	for(num_gen=0;num_gen<4;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(array,n_array);
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				array[j][i]=n_array[j][i];
    			}
    		}
    		for(i=0;i<ROWS;i++){
    			printf("\n");
    			for(j=0;j<COLS;j++){
    				printf("%c", n_array[j][i]);
    			}
    		}
    	}
    	return(0);
    }
    void game_of_life(char o_arr[][COLS], char n_array[][COLS])
    {
    	int i, j, num_neighbors=0;
    	for(i=0;i<COLS;i++){     
    		for(j=0;j<ROWS;j++){
    			if ((o_arr[j][i])=='X'){
    				num_neighbors = checkx(o_arr, j+1, i+1);
    				if( num_neighbors <4 && num_neighbors >= 2)
    					n_array[j][i]='X';
    				else if (num_neighbors >=4 || num_neighbors < 2)
    					n_array[j][i]=' ';
                }
    			else {
    				num_neighbors=checkx(o_arr, j+1, i+1);
    				if (num_neighbors == 3)  
    					n_array[j][i]='X';                
    				else 
    					n_array[j][i]=' ';
                  }
              }
          }
    }
    		
    int checkx(char arr[][COLS],int row, int col)
    {
          int num_neighbors = 0;
          if ((arr[row-1][col-1])=='X')
              num_neighbors++;
          if ((arr[row-1][col])=='X')
              num_neighbors++;
          if ((arr[row][col-1])=='X')
              num_neighbors++;
          if ((arr[row][col+1])=='X')
              num_neighbors++;
          if ((arr[row+1][col-1])=='X')
              num_neighbors++;
          if ((arr[row+1][col])=='X')
              num_neighbors++;
          if ((arr[row+1][col+1])=='X')
              num_neighbors++;
          return(num_neighbors);
    }
    output is:
    Code:
    X XXX X  X  X   XX  XXX  
     XX    XX   XXXXX X XXX X
    XXXX  XX      X    XXXX  
      XX XX   X              
     X  X     XX    XX X  XX 
    X XX XXXX        X   XXXX
    X X   X    X X X X  X XX 
    X      XXXX  XXX XX XX   
    XX   XXXX  XX   XX XXX X 
     XXX XX XX  X X       XXX
     XXXX X  X  X  X XXX X  X
      X X  XXXXX  X    XX    
    XX   X  XXXXX X  XX X X  
    X  X X XXXXX X   XXX XXXX
    X X      XXX X    X   X X
    XX  XXX X  X X    XX  XXX
        X XXX XX XX XXX    X 
    X X  X    X  X X  XXX X X
    X XX  XX XX XXX X XX   X 
    X X   X XXX X   X  X XXX 
    X XX       XX     XX XX X
    XX XX X  XXX X X XXXXXX X
        XXXX X X   XX XX XXXX
    XXX X X  X      XXXX   XX
    XXXXX   XXXX   XX  XXXXX 
    Generation: 1
    
       XX X      XXX X    X  
      X XXXXX    X XX X  XX  
         XX       X   XX  X  
       X  X   X         XXX  
     X XXX   XXX    XX XX  X 
      XXX   X  X    XX X   X 
        X  X X    X XX    X  
     X  X    XXX  XXX X      
      X     X  XX XX  X  X X 
         X    X  X   XXX X XX
     X X  X    XX  X    XX   
     XXXXX     X  X X  X  X  
     X X X      X     XX     
      X XXX      X     X   XX
    X X XX    X XX   XX  X  X
    XX X X   X  XXX    X    X
    X X X  X X    X          
     XX XX   X XXX XX     X X
           X   X XX   X X XX 
     X    XX   XX          X 
    X            X XX       X
     X    X X XX XX        XX
     X X X  X              XX
       X  XXX           XXXX 
        XXX         XX    X  
    Generation: 2
    
          X     X XX X  XXX  
      XX   XX    XXXX X  XX  
         XX X     X  X XXXX  
          X   X    X XXX  X  
     X  XX    XX  X    XXX X 
       XX   X                
        X     X X  X X    X  
        X     XX X   XX  X   
      X     X   X XX XX    X 
        X    XX XX X   XX  X 
    X   X     X X X  X XX    
    X    X     XX X XXXX  X  
    XX   X      X   X XX     
         X    X  X   XXX   XX
      X XX    XX X   X      X
     X  XX   XX  X          X
    X   XX X   X  X       X  
    XXX X       XXXXX   XX  X
           X     XX   X XXXX 
    X    XXX XX XX         X 
    X     X    X XXX       XX
            X XX X         XX
     X   XX X           X XX 
     XXX X X           X XX  
          X     XX X   X     
    Generation: 3
    
        XXX          X    X  
      XX X XX   X     XX  X  
       X XX X    X X     XX  
      X   X   X    X  XX X   
     XX X   XXXX  X    XXX   
       XX   X X    X         
        X       X    X   XX  
      X      X   X XX    X   
      X     X  X  X X      X 
        X   XX X XX    XX    
    X  XX      X   X   XX    
    X  X X    XX  XX   X  X  
    XX   X    XXX  X  XX     
      X XX   XX  X    X    XX
         X     X X          X
     XX XX   XX  XX       X X
    X X XX     X  X    X  X  
    XXX XX         XX XX  X X
           X  XX  X   X X  X 
    X   XX XX X  X       X X 
    X   X X       X        XX
     X      X XX         XXX 
     X XX X            XXXX  
     XXXXX      XX   XX XX   
          X        X  X      
    Generation: 4
    
      X   X          X    X  
      X X  XX    X    XXX X  
    X XXXX X       X  X XX   
     X    X   X   X  XX  X   
     X XX    X X         X   
      XXX   X X   XX         
                XX X      X  
      X    XXX X X XX    X   
            X  X X    X      
      XXX   X  XX X  X XX    
    X   X      X XXX  XXX    
    XX   X     XXXXX X X  X  
    X  XXX      X     X      
    X X XX      XX         XX
      X XX   XXXXX        X X
     X   X   X  XX   X  X   X
       XXX   X   XX XXXX     
    XXX XX        XXX  XXXX X
           XX XX X    X   XX 
    X    X XXXX         X XX 
    X X X X            XX XX 
    X XXX   X XX          X  
         X           X   X   
                XX XXXXXX    
       XX X        X
    Last edited by paranoidgnu; 05-04-2011 at 10:08 AM.

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by mike65535 View Post
    Well, did you try it?

    I see 25x25 grids now. They look like they change from generation to generation. I haven't analyzed to see if it's doing the right thing, (mostly because I don't know what that is) but it's doing something.
    This is a game
    Last edited by paranoidgnu; 05-04-2011 at 10:01 AM.

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    It would be a lot easier to see if it was working, if you initialised your array to say a glider, rather than some random pattern.
    mike65535 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by Salem View Post
    It would be a lot easier to see if it was working, if you initialised your array to say a glider, rather than some random pattern.
    Or some given pattern so that it runs the same each time.

  9. #24
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    I shrunk your ROWS and COLS to 5 for testing, which made it much easier. I noticed a three problems:
    1. Why are you calling checkx with j+1 and i+1? You should just be using j and i, so it corresponds to the same grid coordinate you checked in if (o_arr[j][i]).
    2. You're missing the following case from your checkx function: arr[row-1][col+1]
    3. You don't properly handle the corner/edge cases. Your checkx code needs to make sure that it doesn't go out of bounds of the grid, so you need something like:
    Code:
    if (row > 0 && col < COLS-1 && arr[row-1][col+1] == 'X')  // only check the square to the top-right if it's in bounds
    Do likewise for the other 7 neighbors.
    Last edited by anduril462; 05-04-2011 at 11:33 AM. Reason: typo
    mike65535 likes this.

  10. #25
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    2. You're missing the following case from your checkx function: arr[row-1][col+1]
    I noticed that in the results - it appeared to be ignoring some of the diags. I didn't dig into it. Good catch.

  11. #26
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    The last thing I want to mention is your messy use of i and j. Your grid is row-major, meaning you defined it with array[ROWS][COLS], but you don't always access it that way, sometimes you access array[col][row]. This could easily cause a seg fault if your grid is non-square, and will definitely produce incorrect results as you iterate through generations. When doing something with a 2-d arrays that are "rows and columns", I recommend using more sensible index variable names, like r and c or row and col. That way, you always make sure you're doing things in the right order, since all your accesses are array[row][col], and things like array[col][row] stand out as wrong. I'll let your find/fix all your loops/array accesses.

  12. #27
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You need to check that inside the function game_of_life(), the loops and the call to checkx don't go out of bounds.
    I already pointed out that there was an error in checkx() but I was ignored.

  13. #28
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    paranoidgnu,

    I think Mike got a good one. I haven't personally gone over your code yet, but...

    Quote Originally Posted by mike65535 View Post
    Code:
    if (num_neighbors <= 3 || num_neighbors >= 2)
    That line looks suspicious. Is it not true for all values of num_neighbors?
    Suspicious is an understatement. The number line draws that the Algebra teacher has one do would demonstrate that is the entire number line, and thus always true. Let's see...

    Code:
                                            =-------------------------------> (2nd)
                      <----------------------------= (1st)
    <------------------------|-------|------|------|--------------------------------->
                             0              2      3 
    1st Condition: n <= 3
    2nd Condition: n >= 2
    So no mater where num_neighbors is on the number line, the conditional statement is true.... I hope I didn't make that too difficult, but those sorts of correctness proofs come in handy when dealing with logic errors. Please do keep them in mind.

    Best Regards,
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  14. #29
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    @ new_ink2001 He claims to have fixed that.

    But

    Code:
    				if( num_neighbors <4 && num_neighbors >= 2)
    					n_array[j][i]='X';
    				else if (num_neighbors >=4 || num_neighbors < 2)
    					n_array[j][i]=' ';
    Why so convoluted?


    Code:
    				if( num_neighbors == 2 || num_neighbors == 3)
    					n_array[j][i]='X';
    				else 
    					n_array[j][i]=' ';
    Doesn't that do the same thing?

  15. #30
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    This is confusing, too:

    Code:
    checkx(o_arr, j+1, i+1)
    Why add one to i and j (which should be row and column...or is it vice versa?) before you send? Why not send the actual current location and let checkx do the math?

    I think checkx needs a bounds test as well (for when it runs up against the limits of the valid grid), but could be wrong.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  2. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 02:30 PM
  3. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  4. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  5. Game of Life Program Help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 09:32 AM

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