My Game of Life, i need some help.

How to start with 'preset neighbours'

Page 1 of 1

0 Replies - 599 Views - Last Post: 04 December 2009 - 05:53 PM Rate Topic: -----

#1 Jamitter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 09

My Game of Life, i need some help.

Posted 04 December 2009 - 05:53 PM

This is what i have so far. ( don't mind the empty cases, i'm going to fill those in once i know how to start with preset patterns.)


(assuming you know what "the game of life" is)
I have two questions, how come when you just start with 'generate random pattern' it looks terrible, nothing at all like the game of life. Hopefully i didn't do it completely wrong. Also when you go to 'choose preset pattern' and it shows you your choices, i need that pattern to be the first "generation" then continues from there. and when you choose to generate a random one, it does just that. please help! here is my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int r = 22; // Variable for the Dimension 'row'
const int c = 80; // Variable for the Dimension 'column'



void neighbour_count(char First_Gen[][80], char Second_Gen[][80]); // Function used to count the number of neighbours each cell has
void display_gen(char First_Gen[][80]); // Function used to randomly populate the world for the first generation
void neighbour_count2(char First_Gen[][80], char Second_Gen[][80]); // Function used to transfer survivors of the 1st generation to the 2nd
void display_gen2(char First_Gen[][80]); // Function used to display the 'new generation'
void Preset_Pattern_3(char First_Gen[][80], char Second_Gen[][80]);

int main()
{
	char First_Gen[r][c], Second_Gen[r][c]; // Two-Dimensional arrays to save positions of 2 generations at a time
	char a = 'y';
	int choice, PatternChoice;
	srand((unsigned)time(NULL)); // Initializing the rand()
	cout<<"Welcome to the Game of Life,  created by Matt Jones." << endl;
	cout<<"Please choose to: "<< endl; 
	cout<<"1)Choose an existing pattern for the 1st generation." << endl;
	cout<<"2)Generate a random pattern. " << endl;
	cin >> choice;

	switch(choice)
	{
	case 1:
		cout <<" Please choose one of these patterns: " << endl;
		cout <<"1) *****" << endl;
		cout << endl;
		cout <<"2) ***"<< endl;
		cout <<"   ***"<< endl;
		cout <<"   ***"<< endl;
		cout << endl;
		cout <<"3) *   "<< endl;// r6  /  c24
		cout <<"  * *  "<< endl;// r7 / r7   c23 / 25
		cout <<" *   *  "<< endl;// r8 / r8  c22 / 26
		cout <<"  * *  "<< endl;// r9  / r9  c23 / 25
		cout <<"   *  "<< endl;// r10 / r10  c24
		cout << endl;
		cout <<"4) *   "<< endl;
		cout <<"  ***  "<< endl;
		cout <<" ** **  "<< endl;
		cout <<"  ***  "<< endl;
		cout <<"   *  "<< endl;
		cout << endl;
		cout <<"5)***"<< endl;
		cout <<" *   *"<< endl;
		cout <<" *   *"<< endl;
		cout <<" *   *"<< endl;
		cout <<"  *** "<< endl;
		cin >> PatternChoice;
		switch(PatternChoice)
		{
			case 1:
				break;
			case 2:
				break;
			case 3:
				Preset_Pattern_3(First_Gen, Second_Gen);
			case 4:
				break;
			case 5:
				break;
		}

		system("PAUSE");
		return(0);

	case 2:
		break;
		return(0);
	}

	


	/* Loop used to repeat the steps to move from the first generation to the next, 
	continuing until the user exits the program manually. (For the time being) */
top :
	do
	{
		char a = 'y';
		neighbour_count(First_Gen, Second_Gen);
		neighbour_count2(First_Gen, Second_Gen);
		display_gen(First_Gen);
		display_gen2(First_Gen);
		

	}
	while (a != 'n');
	return(0);
}

void neighbour_count(char First_Gen[][80], char Second_Gen[][80])
{
	for (int x = 0; x < r; x++)
	{	
		for (int y = 0; y < c; y++)
		{
			/* This next block of code's purpose is to check every cell
			in the 'world' to see how many neighbours is has, if any,
			and will determine if that cell will live, die, or help
			in creating a birth */

			int Num_Neighbours = 0;
			{
			if (First_Gen[x][y-1] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x-1][y-1] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x-1][y] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x-1][y+1] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x][y+1] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x+1][y] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x+1][y+1] == '*')
			Num_Neighbours++;
			else
			if (First_Gen[x+1][y-1] == '*')
			Num_Neighbours++;

			if (Num_Neighbours == 3)
				{
					Second_Gen[x][y] = '*';
				}
				else
				{
				Second_Gen[x][y] = ' ';
				}
			}
		}
	}
	cout  << endl;
}




void neighbour_count2(char First_Gen[][80], char Second_Gen[][80])
{
	for(int x=0; x<r; x++)
	{
		for(int y=0; y<c; y++)
		{  // This line of code copies the surviving cells into the next generation's array
			First_Gen[x][y] = Second_Gen[x][y];
		}
	}
	cout << endl;
}


void display_gen(char First_Gen[][80])
{
	for (int x=0; x<r; x++)
	{
		for (int y=0; y<c; y++)
		{	
			if (First_Gen[x][y] != '*')
				{
					First_Gen[rand()%22][rand()%80] = '*'; // This generates the random population for the first generation
					First_Gen[x][y] = ' ';
				}
		}
	}
}

void display_gen2(char First_Gen[][80])
{
	for(int x=0; x<r; x++)
	{
		for(int y=0; y<c; y++)
		{
			cout << First_Gen[x][y]; // This displays the new generation after all the previous functions have been proformed
		}
		cout << endl;
	}
}

void Preset_Pattern_3(char First_Gen[][80], char Second_Gen[][80])
{
	char a='y';

				First_Gen[6][24] = '*';
				First_Gen[7][23] = '*';
				First_Gen[7][25] = '*';
				First_Gen[8][22] = '*';
				First_Gen[8][26] = '*';
				First_Gen[9][23] = '*';
				First_Gen[9][25] = '*';
				First_Gen[10][24] = '*';

				do
	{
		char a = 'y';
		neighbour_count(First_Gen, Second_Gen);
		neighbour_count2(First_Gen, Second_Gen);
		display_gen(First_Gen);
		display_gen2(First_Gen);
		

	}
	while (a != 'n');
	
				
}



Thanks in advance, cheers. also dont mind the "sloppy" coding, its just a rough draft, going to clean it up once i figure this stuff out.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1