game of life C++ difficulty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alias09
    New Member
    • Mar 2010
    • 3

    game of life C++ difficulty

    I'm trying to code a simple "game of life" simulator, but I'm having trouble. I can't figure out how to make the generation change to the next one and display it correctly

    All I'm getting is a blank array, when I go from the 0 generation to the next one.
    There's probably some simple steps I'm missing, but for the life of me, I can not figure them out. (I'm very much a beginning programmer, only in my first programming class)

    this is the function I'm working on that's giving me trouble:

    Code:
    void CalculateNextGeneration (char Board [NumRows] [NumCols])
    	{
    
    	int Count; //count how many '*' are present
    	char Board2 [NumRows] [NumCols]; 
    	for (int i =0; i <= NumRows; i++) //initialize 2nd array
    		{
    		for (int j = 0; j < NumCols; j++)
    			{
    			Board2 [i] [j] = Board [i] [j];
    			}
    		}
    
    
    
    	for (int i = 0; i < NumRows; i++)       
    		{ 
    		for (int j = 0; j < NumCols; j++)   
    			{
    			Count = 0;   //resets for each loop
    			if (Board [i - 1] [j - 1] == '*' )      // check upper left for neighbors
    			{ 
    			Count++; 
    			} 
    				else if (Board [i] [j - 1] == '*' )     //check above 
    				{ 
    				Count++; 
    				}  
    					else if (Board [i + 1] [j - 1] == '*' )    //upper right 
    					{ 
    					Count++; 
    					}     
    						else if (Board [i - 1] [j] == '*' )     //in front 
    						{    
    						Count++; 
    						}     
    							else if (Board [i + 1] [j] == '*' )    //after
    							{ 
    							Count++; 
    							} 
    								else if (Board [i - 1] [j + 1] == '*' )  //bottom left 
    								{ 
    								Count++; 
    								}     
    									else if (Board [i] [j + 1] == '*' )     //below 
    									{ 
    									Count++; 
    									} 
    										else if (Board [i + 1] [j + 1] == '*' )   //bottom right 
    										{ 
    										Count++; 
    										} 
    				if (Count == 3) // born if 3 neighbors
    					{
    					Board2 [i] [j] = '*';
    					}
    				else if ((Count <= 1) || (Count >= 4)) // dies if more then 3 (so 4 or more) neightbors, or less then 2 (1 or 0)
    					{
    					Board2 [i] [j] = ' ';
    					}
    			 
    			} 
    		}     
    	Generation++;
    	Display (Board2);
    	}
    before this, I initialize the array with inputed x and y values, and display it, then this function is called. I can't understand the problem.
    Any help or advice/tips I could get?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Your problem is that you're using else if. It should just be an if.
    The most number of neighbours you will ever have using your method is 1, thus everyone dies.

    Other notes:
    How are you not getting array out of bounds issues?
    The first time this is called,
    if (Board [i - 1] [j - 1] == '*' )
    you're probably looking for Board[-1][-1]

    Also, I thought if a living cell has 2 other living neighbours, it stays alive?

    Comment

    • Alias09
      New Member
      • Mar 2010
      • 3

      #3
      Ah, else if.. is it really that simple? I'll try it, thanks.

      As for not getting out of bounds, the grid that's being changed is 2 shorter in dimensions then what it actually is. (10 by 10 grid available for manipulation, but the actual array is 12 x12),
      wait, you're right... Earlier I had it at i = 1 and j = 1 for those tests, but I altered it because I was trying to troubleshoot, thanks for bringing that to my attention. It still worked though, strangely.

      Edit: It did work, it worked perfectly, Thank you very much!
      Edit2: hmm, now after I increased the grid/array size and test numbers with two digits, it's blowing up when it reaches the CaculateNextGen eration function...
      Edit3: seems like it's working now, just a few bugs to work out. Thanks again.

      Comment

      Working...