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; Absolutely Mike, check it out: The first branch of the original conditional executes when num_neighbors is: Code: =-------O <-------------|-----|---|---|------> 0 ...

  1. #31
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Absolutely Mike, check it out:

    The first branch of the original conditional executes when num_neighbors is:

    Code:
                        =-------O
    <-------------|-----|---|---|------>
                  0     2       4
    That's the collection of numbers for which the first condition is true, i.e. [2, 4).... Indeed the integers 2 and 3. The second branch...

    Code:
                 <------O       =------>
    <-------------|-----|---|---|------>
                  0     2       4
    Yes, that's the complement with respect to the number line, i.e. (-inf, 2) union [4, inf). Which is identical to your suggestion:

    Code:
                        =   =   
    <-------------|-----|---|---|------>
                  0     2   3   4
    For the first branch, that is [2, 3], and the else block executes for anything else...

    Code:
                 <------O   O------>
    <-------------|-----|---|---|------>
                  0     2       4
    That is the set of numbers (-inf, 2) union (3, inf).

    Thanks for the prompt. I apologize that I haven't been more involved, I'm kind of in the middle of several activities at the moment including sharpening up my PERL for a couple contracts that I would like to land....

    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.

  2. #32
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Thanks for your help
    I think, we solved problem.
    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, i);
    				if( num_neighbors == 2 || num_neighbors == 3)
    					n_array[j][i]='X';
    				else 
    					n_array[j][i]=' ';
                }
    			else {
    				num_neighbors=checkx(o_arr, j, i);
    				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 ((row > 0 && col > 0 && arr[row-1][col-1])=='X')
              num_neighbors++;
          if ((row > 0 && arr[row-1][col])=='X')
              num_neighbors++;
          if (row > 0 && arr[row-1][col+1] == 'X')
    		  num_neighbors++;
          if ((col > 0 && arr[row][col-1])=='X')
              num_neighbors++;
          if ((arr[row][col+1])=='X')
              num_neighbors++;
          if ((col > 0 && 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:
    Code:
    X XXX  X XXX  X    XXX X 
    XXX   X   X  XX    XX XXX
    X XXX XXX  X XX  XX X XXX
     X     XX X    X  XX    X
    X      X  X   X   X  XX  
    X X  XX XXXXX   X XXX XX 
     X XXX  XXX XX X  X   X  
     XXX  XXXX  X  XXX   X XX
     X  X X   X XXX XXXXX  XX
    X  X X   XXXX XX   X X  X
    X   X X XXX        X     
      XXXX X X X X X         
    X  X X XXX  X XX  XX   X 
    XX XX XX XX X X XXXXXXX  
      XXX  X     XX X   XX XX
     XXX  XXX X XX    X    X 
    X  XX   XXX   X XX XXX X 
     XX   XX   XXXXX XXX X  X
     X X XX X   XXXX X X  XXX
    X  XXX       XX XXX X  X 
     X  XXXXX XXXX  X X XXX  
     X X      X XXXX  XX  XXX
    X      X XX   XX XXX XX  
    X XXX         XXXX X X XX
     X XX  X X      XXX X X X
    Generation: 1
    
    X XX     XX  XX    XXXXX 
    XXXX XXX    XXX   XX XX X
      X   XXX        XXX   X 
           X          X      
          XX  XX     XXXXXX  
      X  X  X XXX     XX  X  
     X XX    X  X  XXX    X  
     XX  XXX   XXXXXXXXXX  XX
        X    XX XXXXX XXX  X 
         X  X XX             
        XXX XXX              
      XXX XX X   XXX         
    X XXXXXXXX X XXX   XXXX  
      X X X      XXXXX XXXX  
     X X  XX    XX         X 
     XXX   XX X      XX X X  
      XX   XX   X XXXX XX    
     XX  XXX   XX  XXXX     X
       X X      XXXXXXXX  XX 
       XXXXX   X X  XX XXXX  
        X     XXXXXX  XXXXXX 
             XX  XXX X X XXX 
       X          XX XX XXX  
      XXX          XXXXX X X 
     X X   XXX       XX XX   
    Generation: 2
    
    X  X  X     X X   XXXXXX 
     XX  XXX         X X     
          XX         XX      
          XX         XXXXX   
          X  XXX     XXXXX   
      X X     XX    XXX      
     XXX  X    XXX   XXX     
         X    XXX   XXX X  X 
             XXXX            
        XX XX X              
        XXXXXX    X          
     XX X  X X  XX X    XX   
      XXXXXX     X XX XX  X  
      X   X     XXX          
     X X  XX                 
     XXX   XX      X XXXX    
     XX    X   XX XX XXX     
        XXX    XXX    X    X 
       X XX     XXXXXXXXX X  
       XXX    XX XXXX X   X  
             XXXXX X  XXXX X 
                 XXXXXXXX X  
      XX          XX  XXXXX  
      XX    X       XXXXXX   
     XX    XXX       XX  X   
    Generation: 3
    
      X  XX           XXX    
         XXX         XX      
          XX         XXXX    
          X   X      X   X   
             XXX    X XX     
     XXX      XXX   XXX      
              X X   X XX     
             X  X            
            XXXX             
        XX  XX               
       XXXXXX    XX          
     XX XX X    X XX   XX    
      XX XX     XXXX         
      X   X                  
     X X  XX          XX     
     XX    X      XXXX X     
         XX    XXXX  XX      
        XXX    XXXXXX XX     
       X X     XXX  XXXX     
             X X XXX  XXXXX  
                XX XX   XX   
                 XXXXXX X X  
      XX                 X   
     XX    XX       XXXXX    
     X               XXX X   
    Generation: 4
    
         XX          XXX     
         XXX         XXX     
          X          XX      
             XX      X       
      X      XXX    X X      
              XX    XXX      
                             
            XXXX             
            XXX              
       X XX X                
      XXXXXX     XX          
     XXXXXX     X  X         
      X  X                   
      X   X                  
     XX   X        XXXX      
          X     X XXXXX      
        X X    X  XXXXX      
        XX     X XXXX  X     
              XXXXXXXXXXXX   
                XXXX   XXX   
                XX  XX XXX   
                             
     XX              XXXX    
     X              XX XX    
                     X X

Page 3 of 3 FirstFirst 123
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