I'm trying to write the code for the Game of Life program, using arrays and for loops. I based most of my code on what my teacher did in class and modified it to fit the requirements of my assignment. I've hit a wall, though. When I execute the program, I'm able to input what how many cells and what cells I want occupied. The program displays my input correctly for the first Generation, but every Generation after that has all the cells filled with astericks.

#include <iostream>

using namespace std;

#include <memory.h>
#include <stdlib.h>
#include <time.h>

void main ()
	{
	const	long	NumCols	(60);
	const	long	NumRows	(60);
			bool	Board	[NumRows + 2] [NumCols + 2];
			bool	NextBoard	[NumRows + 2] [NumCols + 2];
			long	Col;
			time_t	CurrTime;
			long	Generation;
			long	i;
			long	NumNeighbors;
			long	NumOccupied;
			long	Row;
			time_t	StartTime;
	const	time_t	WaitTime (3);

	memset (Board, false, (NumRows + 2) * (NumCols + 2) * sizeof (bool));

	cout << "How many cells do you want occupied? ";
	cin >> NumOccupied;
	for (i = 0; i < NumOccupied; i++)
		{
		cout << "Which row and column position is to be occupied: (1 to 60) ";
		cin >> Row >> Col;
		 if(Row > NumRows, Col > NumCols)
					{
					cout << "Not a valid position, reenter" << endl;
					i--;
					}
				else
					Board [Row] [Col] = true;
		}

	for (Generation = 0; ; Generation++)
		{
		system ("cls");		// "cls" needs to be changed if you are on UNIX
		cout << "Generation " << Generation << endl;
		for (Row = 1; Row <= NumRows; Row++)
			{
			for (Col = 1; Col <= NumCols; Col++)
				cout << (Board [Row] [Col] ? '*' : ' ');
				if (Board [Row] [Col])
						cout << '*';
					else
						cout << ' ';
			cout << endl;
			}

		for (Row = 1; Row <= NumRows; Row++)
			for (Col = 1; Col <= NumCols; Col++)
				{
				// count the number of occupied neighbors
				NumNeighbors = 0;
				if (Board [Row - 1] [Col - 1])
						NumNeighbors++;
					else;
				if (Board [Row - 1] [Col])
						NumNeighbors++;
					else;
				if (Board [Row + 1] [Col - 1])
						NumNeighbors++;
					else;
				if (Board [Row + 1] [Col])
						NumNeighbors++;
					else;
				if (Board [Row + 1] [Col + 1])
						NumNeighbors++;
					else;
				if (Board [Row + 1] [Col])
						NumNeighbors++;
					else;
				if (Board [Row - 1] [Col + 1])
						NumNeighbors++;
					else;
					
				// do the same for the other cells around this one
				// now apply the rules
				if (NumNeighbors >= 4)
						NextBoard [Row] [Col] = ' ';
				if (NumNeighbors <= 1)
						NextBoard [Row] [Col] = ' ';
				if (NumNeighbors = 3)
						NextBoard [Row] [Col] = '*';
				else;
				}
		
		memcpy (Board, NextBoard,  (NumRows + 2) * (NumCols + 2) * sizeof (bool));

		StartTime = time (0);	// gets current time in seconds since Jan 1, 1970
		do	{
			CurrTime = time (0);
			} while ((CurrTime - StartTime) < WaitTime);
		}
	}

Any help would would be appreciated.

Recommended Answers

All 3 Replies

Lines 50 vs 87-92. Make up your mind on bool vs char contents of Board[][].

You aren't testing Board[Row][Col-1] nor Board[Row][Col+1] .
Wouldn't two nested loops be better than all those IF statements?.

So I added Board[Row][Col-1] and Board[Row][Col+1] to my list of rules, and also condensed them all into one IF statement. I also modified the IF statements below that. I have a different problem now: whenever I execute the program all the cells in column 60 are filled. I'm incredibly new to programming, so the issue completely eludes me.

#include <iostream>

using namespace std;

#include <memory.h>
#include <stdlib.h>
#include <time.h>

void main ()
	{
	const	long	NumCols	(60);
	const	long	NumRows	(60);
			bool	Board	[NumRows + 2] [NumCols + 2];
			bool	NextBoard	[NumRows + 2] [NumCols + 2];
			long	Col;
			time_t	CurrTime;
			long	Generation;
			long	i;
			long	NumNeighbors;
			long	NumOccupied;
			long	Row;
			time_t	StartTime;
	const	time_t	WaitTime (3);

	memset (Board, false, (NumRows + 2) * (NumCols + 2) * sizeof (bool));

	cout << "How many cells do you want occupied? ";
	cin >> NumOccupied;
	for (i = 0; i < NumOccupied; i++)
		{
		cout << "Which row and column position is to be occupied: (1 to 60) ";
		cin >> Row >> Col;
		 if(Row > NumRows, Col > NumCols)
					{
					cout << "Not a valid position, reenter" << endl;
					i--;
					}
				else
					Board [Row] [Col] = true;
		}

	for (Generation = 0; ; Generation++)
		{
		system ("cls");		// "cls" needs to be changed if you are on UNIX
		cout << "Generation " << Generation << endl;
		for (Row = 1; Row <= NumRows; Row++)
			{
			for (Col = 1; Col <= NumCols; Col++)
				cout << (Board [Row] [Col] ? '*' : ' ');
				if (Board [Row] [Col] = true)
						cout << '*';
					else
						cout << ' ';
			cout << endl;
			}

		for (Row = 1; Row <= NumRows; Row++)
			for (Col = 1; Col <= NumCols; Col++)
				{
				// count the number of occupied neighbors
				NumNeighbors = 0;
				if (Board [Row] [Col - 1] & Board [Row] [Col + 1] & Board [Row - 1] [Col - 1]
				& Board [Row - 1] [Col] & Board [Row + 1] [Col - 1] & Board [Row + 1] [Col]
				& Board [Row + 1] [Col + 1] & Board [Row - 1] [Col + 1])
						NumNeighbors++;
					else;
					
				// do the same for the other cells around this one
				// now apply the rules
				if (NumNeighbors >= 4)
						NextBoard [Row] [Col] = false;
				if (NumNeighbors <= 1)
						NextBoard [Row] [Col] = false;
				if (NumNeighbors == 3)
						NextBoard [Row] [Col] = true;
				else;
				}
		
		memcpy (Board, NextBoard,  (NumRows + 2) * (NumCols + 2) * sizeof (bool));

		StartTime = time (0);	// gets current time in seconds since Jan 1, 1970
		do	{
			CurrTime = time (0);
			} while ((CurrTime - StartTime) < WaitTime);
		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.