954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

the game of life

Trying to do some basic "Game of life", but for some reason i'm getting some weird results, the code:

#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
using namespace std;
#include "windows.h"
const int V=30;//Vertical
const int H=40;//Horizontal

char A[V][H];
int ne=0;

//Set all to dead
void setDead()
{
	for (int i=0;i<V;++i)
	{
		for (int j=0;j<H;++j)
		{
			A[i][j]='-';
		}
	}
}

//Print
void print()
{
	system("cls");
	for (int i=0;i<V;++i)
	{
		for (int j=0;j<H;++j)
		{
			cout<< A[i][j];
		}
		cout<< endl;
	}
}

//Enter alive cells
void enter()
{
	int h=0,v=0;
	bool status=false;

	while(true)
	{
	cout<< "Enter Vertical: ";
	cin>> v;

	cout<< "Enter Horizontal: ";
	cin>> h;

	if ( (h==-1)||(v==-1) )
	{
		break;
	}

	A[v][h]= '*';

	print();
	}
}

//Compute
void compute()
{
	for (int i=0;i<V;i++)
	{
		for (int j=0;j<H;j++)
		{
			    ne=0;
				if ( A[i][j-1]=='*' ) { ne+= 1; }
				if ( A[i-1][j-1]=='*' ) { ne+= 1; }
				if ( A[i-1][j]=='*' ) { ne+= 1; }
				if ( A[i-1][j+1]=='*' ) { ne+= 1; }
				if ( A[i][j+1]=='*' ) { ne+= 1; }
				if ( A[i+1][j+1]=='*' ) { ne+= 1; }
				if ( A[i+1][j]=='*' ) { ne+= 1; }
				if ( A[i+1][j-1]=='*' ) { ne+= 1; }

				if ( A[i][j]=='*' && ne<2 ) { A[i][j]='-'; }
				else
				if ( A[i][j]=='*' && ne>3 ) { A[i][j]='-'; }
				else
				if ( A[i][j]=='*' && (ne==2||ne==3)) { A[i][j]='*'; }
				else
				if ( A[i][j]=='-' && ne==3 ) { A[i][j]='*'; }
		}
	}
}

int main()
{
	setDead();
	print();
	enter();

	while(true)
	{
	print();
	compute();
	Sleep(1000);
	}

	char f;cin>>f;
	return 0;
}
nuclear
Junior Poster in Training
94 posts since Aug 2011
Reputation Points: 2
Solved Threads: 3
 

What exactly do you mean by "weird results"? Please elaborate.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 
What exactly do you mean by "weird results"? Please elaborate.

I'm not getting what im supposed to get, it begins doing something similar to what its supposed to do, but turns out wrong. ( "Game of life" - http://www.bitstorm.org/gameoflife/ )

nuclear
Junior Poster in Training
94 posts since Aug 2011
Reputation Points: 2
Solved Threads: 3
 

Several problems exist.

First if you reference an array out of bounds you are going to get a mess.

Consider your code:

for (int i=0;i<V;i++)
    {
      for (int j=0;j<H;j++)
	{
	  ne=0;
	  if ( A[i][j-1]=='*' ) { ne+= 1; }
// .....


What happens as j is zero?? Similarly what happens when i is V-1 or j is H-1. You are then accessing unknown memory locations.


The solution to your problem is the define char A to be char A[V+2][H+2]; . Then set the boundary to be dead and then work from 1 to V and 1 to H.


Next Error:

You update the map on a cell by cell basis. The game is normally set out in a form by which the state of the game at a point in time is used to calculated the change, and then the whole change is implemented at once across the whole map. This can be easily carried out by having two arrays and switching between them each move.

StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 

Your algorithm is not going to do what you want it to because you're updating the cells as you check whether they should be alive or dead in the next iteration. To get the effect that you want, you have to make copy of the grid with the updated cell states in it.

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

It works, thanks for the answers.

nuclear
Junior Poster in Training
94 posts since Aug 2011
Reputation Points: 2
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: