473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. void CalculateNextGeneration (char Board [NumRows] [NumCols])
  2.     {
  3.  
  4.     int Count; //count how many '*' are present
  5.     char Board2 [NumRows] [NumCols]; 
  6.     for (int i =0; i <= NumRows; i++) //initialize 2nd array
  7.         {
  8.         for (int j = 0; j < NumCols; j++)
  9.             {
  10.             Board2 [i] [j] = Board [i] [j];
  11.             }
  12.         }
  13.  
  14.  
  15.  
  16.     for (int i = 0; i < NumRows; i++)       
  17.         { 
  18.         for (int j = 0; j < NumCols; j++)   
  19.             {
  20.             Count = 0;   //resets for each loop
  21.             if (Board [i - 1] [j - 1] == '*' )      // check upper left for neighbors
  22.             { 
  23.             Count++; 
  24.             } 
  25.                 else if (Board [i] [j - 1] == '*' )     //check above 
  26.                 { 
  27.                 Count++; 
  28.                 }  
  29.                     else if (Board [i + 1] [j - 1] == '*' )    //upper right 
  30.                     { 
  31.                     Count++; 
  32.                     }     
  33.                         else if (Board [i - 1] [j] == '*' )     //in front 
  34.                         {    
  35.                         Count++; 
  36.                         }     
  37.                             else if (Board [i + 1] [j] == '*' )    //after
  38.                             { 
  39.                             Count++; 
  40.                             } 
  41.                                 else if (Board [i - 1] [j + 1] == '*' )  //bottom left 
  42.                                 { 
  43.                                 Count++; 
  44.                                 }     
  45.                                     else if (Board [i] [j + 1] == '*' )     //below 
  46.                                     { 
  47.                                     Count++; 
  48.                                     } 
  49.                                         else if (Board [i + 1] [j + 1] == '*' )   //bottom right 
  50.                                         { 
  51.                                         Count++; 
  52.                                         } 
  53.                 if (Count == 3) // born if 3 neighbors
  54.                     {
  55.                     Board2 [i] [j] = '*';
  56.                     }
  57.                 else if ((Count <= 1) || (Count >= 4)) // dies if more then 3 (so 4 or more) neightbors, or less then 2 (1 or 0)
  58.                     {
  59.                     Board2 [i] [j] = ' ';
  60.                     }
  61.  
  62.             } 
  63.         }     
  64.     Generation++;
  65.     Display (Board2);
  66.     }
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?
Mar 23 '10 #1

✓ answered by jkmyoung

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?

2 4799
jkmyoung
2,057 Expert 2GB
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?
Mar 23 '10 #2
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 CaculateNextGeneration function...
Edit3: seems like it's working now, just a few bugs to work out. Thanks again.
Mar 23 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
1
by: Gina | last post by:
I need to add the cell generation to a templated program. I am using graphics magician, but my problem is with the math. I cannot figure out my cell generations. I do know that I need two...
4
by: aura | last post by:
anybody got C code for this game of life using 8X8 array
2
by: Hans Kamp | last post by:
As a programming exercise I try to program a game. In my case it is called BugEater. The purpose is that you are a snake. It moves automatically but you can control it with the keys on your...
6
by: CapMaster | last post by:
I'm having some trouble programming the game of life. In the version my teacher gave us, it involves a class with a private grid variable for the game. Here's the class she gave us: .. const int...
3
by: ThaDoctor | last post by:
Hi. I am quite new to C++ so I think I would ask here what I am doing wrong with this code. I am writing a little game in a text console, but here is something that is in no way related to the...
1
by: vampiro666 | last post by:
hello, can anyone help me plz. i have an assignment to submit but i am having problem with it. the problem is just below: the game of life The game of life is a simple cellular automaton where...
1
by: blackslither | last post by:
I had to implement Game of Life in a 3D matrix (int a) , but the complexity of my implementation is O(n^6) (6 imbricated for) and it runs very slow . Can anyone help me with an optimized version of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.