The infamous game of life

Hey all!

So I am working on the Game of life..I have most of it down but I am COMPLETELY stumped when it comes to applying the rules to the game.. I am trying to get all the standard rules as follow;

Living cell dies of overcrowding if next generation has >4 neighrbors
Living cell dies of loneliness in the next generation if it has <1 neighbors
An Empty cell becomes a birth cell if it has 3 living neighbors in the next generation
All other cells remain unchanged..

I don't even know where to begin..I have 90% of the program written but applying the rules is KILLING me! Can someone help point me in the right direction?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;

#include <conio.h>	// needed for _kbhit
#include <memory.h>	// needed for memset below
#include <time.h>	// needed for time below

void main ()
	{
	const	long	NumRows	(20);
	const	long	NumCols	(20);

			bool	CurrGen	[NumRows + 2] [NumCols + 2];
			bool	NextGen	[NumRows + 2] [NumCols + 2];
			long	Row;
			long	Col;
			long	Generation;
			time_t	StartTime;
			long	WaitTime (4);

	// ***** Initialize the board

	memset (CurrGen, false, sizeof (CurrGen));
	memset (NextGen, false, sizeof (NextGen));
	do	{
		cout << "Enter a row and col position to occupy: ";
		cin >> Row >> Col;
		if ((Row > 0) && (Row <= NumRows) && (Col > 0) && (Col <= NumCols))
				CurrGen [Row] [Col] = true;
			else
				if ((Row != -1) || (Col != -1))
						cout << "Bad coordinates, try again" << endl;
					else;
		} while ((Row != -1) || (Col != -1));

	// ***** display the board

	Generation = 1;
	do	{
		system ("cls");
		cout << "Touch whichever key you wish to stop the program" << endl;
		cout << "         Generation " << Generation << endl;
		for (Row = 1; Row <= NumRows; Row++)
			{
			for (Col = 1; Col <= NumCols; Col++)
				cout << (CurrGen [Row] [Col] ? '*' : ' ');
			cout << endl;
			}

// ***** apply the rules
		for (Row = 1; Row <= NumRows; Row++)
			for (Col = 1; Col <= NumCols; Col++)
				{
				// find number of neighbors
				// find rule that matches and apply it
				}

		// ***** After applying rules and populating the NextGen

		memcpy (CurrGen, NextGen, sizeof (CurrGen));

		// ***** wait
		StartTime = time (0);
		do	{
			} while ((time (0) - StartTime) < WaitTime);
		Generation++;
		} while (!_kbhit ());
	}

start by writing a function that counts how many neighbors any given cell has.

The rest of it should be pretty easy after that.
Cute idea! I approve! was it assigned to you or did you come up with it on your own?
I grabbed this code off the webs and I've been trying to figure it out, but how can I make the the cells count neighbors? I am comfortable somewhat with the for and if/else loop but I'm completely lost on its implementation in this instance..but I can't put it down lol.
1
2
3
4
5
6
int aliveneighbors = 0;
if( cell_above_this_one_is_alive )
  aliveneighbors += 1;
if( cell_below_this_one_is_alive )
  aliveneighbors += 1;
//... etc 



EDIT:

Although it dawns on me... if you couldn't come up with that on your own... and you're expected to write a program on the game of life....

...

where you slacking off all semester? Or does your teacher just have insanely high expectations? =P
Last edited on
In my class we just got into Arrays and now its time for this program =[

I'm Doomed!


Disch can you expand on this code? I'll have to set "aliveneighbors" as a bool right?
Last edited on
Disch can you expand on this code?


Not really. At least not without doing all the work for you.

I'll have to set "aliveneighbors" as a bool right?


It's an int (as illustrated in my code). Maybe "numberofaliveneighbors" would be more descriptive, but I figured that was too long.

The idea is, you count the number of alive neighbors by checking each neighbor and adding 1 to 'aliveneighbors' for each neighbor that is alive.

Once you check all neighbors, you can use your 'aliveneighbors' value to determine what action to take (either birth the cell if == 3, or kill the cell if > 4, or kill if < 1, or whatever the rules are)
I'm not following you Disch..

Can you provide me a small example of how to intiliaze this?

Forgive me gents, I'm a Network Engineer and HORRIBLE at code, at least reading my posts should garner a good laugh for you pros =[

Can you provide me a small example of how to intiliaze this?


err... I just did. =P


1
2
3
4
5
6
7
8
9
10
11
12
//  our variable which counts the number of neighbors which are alive
int aliveneighbors = 0;

// check each neighbor to see if they're alive
if( cell_above_this_one_is_alive )  // check neighbor above us to see if it's alive
  aliveneighbors += 1;  //  if it is alive, count it


if( cell_below_this_one_is_alive )  // check neighbor below us
  aliveneighbors += 1;  // if alive, count it

// repeat for all neighbors 



Now, I would put that in a function so that it can be more easiliy used. Hopefully you already learned about functions:

1
2
3
4
5
6
7
8
int neighbors = CountAliveNeighbors(x,y);

if(neighbors < 1)
  kill_the_cell;
else if(neighbors == 3)
  birth_the_cell;
else if(neighbors > 4)
  kill_the_cell;

We still haven't gotten into functions yet =[

How can I "kill the cell"?

We still haven't gotten into functions yet =[


That's ridiculous. Writing the game of life without functions is borderline insanity.

I would say I don't really believe you, but I've seen far worse in some programming courses, so you never know....

How can I "kill the cell"?


It depends entirely on how you're representing each cell.

Basically each cell would have a variable indicating whether it's alive or dead. To kill it, you just change it's status to dead.



Although the more and more I speak with you, the more I see how ill equipped you are to handle this problem. I don't think anyone here will be able to help you other than handing out solutions (and that isn't really help).

You really need to either go to your teacher/professor and tell him you don't understand the assignment, or get a private tutor who can walk you through all of this.
Dangit...

It is a once a week class we meet for 3 hours at a time, last week we did Arrays and we are supposed to get into functions this week(when this program is due).

I'll go to the PC lab before class and try to get help, but thanks anyways!
Also, you can try reading the documentation on this website. If someone like me can self-teach C++ with no formal classes, I'm sure you'll have it in no time!
Last edited on
Topic archived. No new replies allowed.