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;

{
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()
{
print();
enter();

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

char f;cin>>f;
return 0;
}``````

All 5 Replies

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

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/ )

Several problems exist.

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

``````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.

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.

It works, thanks for the answers.

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.