Simple game of life implementation

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 6722 Views - Last Post: 27 March 2009 - 08:12 AM Rate Topic: -----

#1 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Simple game of life implementation

Posted 10 March 2009 - 09:45 AM

I am working on a simple implementation of the game of life, though it's coming along cumbersome. My code is
#define LL 100
#define LH 100
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

int arr[LL][LH];
int gencount=1;

void trans(int arr[LL][LH]);   
void init(int arr[LL][LH]);
int checkcell(int arr[LL][LH], int i, int j);
int checkneigh(int arr[LL][LH], int i, int j);
void finddie(int arr[LL][LH]);
void findnewlife(int arr[LL][LH]);

void main(int arr[LL][LH], int gencount){// main, obviously)
	while(gencount!=0){
	init(arr[LL][LH]);
	finddie(arr[LL][LH]);
	findnewlife(arr[LL][LH]);
	trans(arr[LL][LH]);
	gencount++;
	}
}


void init(int arr[LL][LH]) //random initialization of the grid
{
rand(1);
int i, o;
for(i=0; i<LL; i++){
	for(o=0; o<LH; o++){
		arr[i][o]=abs(rand()%2)
	}
}
}

int checkcell(int arr[LL][LH], int i, int j)//checks if cell is alive
{
	if(arr[i][j]==1)
		return 1;
	else
		return 0;
}






int checkneigh(int arr[LL][LH], int i, int j)//counts the number of living neighbours to a given cell
{
	int t;
		  if(arr[i-1][j-1]==1)
			  t++;
		  if(arr[i][j-1]==1)
			  t++;
		  if(arr[i+1][j-1]==1)
			  t++;
		  if(arr[i-1][j]==1)
			  t++;
		  if(arr[i+1][j]==1)
			  t++;
		  if(arr[i-1][j-1]==1)
			  t++;
		  if(arr[i-1][j+1]==1)
			  t++;
		  if(arr[i][j+1]==1)
			  t++;
		  if(arr[i+1][j+1]==1)
			  t++;
		  return t;
}



void finddie(int arr[LL][LH])//finds all cells that are to die when the generation passes
{
	int i, j, k, t;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
			t=checkcell(arr[LL][LH], i, j);
			if(t==1){
			k=checkneigh(arr[LL][LH], i, j);
			if((k<2)||(k>4))
				arr[i][j]=2;
			}
		}
	}
}





void findnewlife(int arr[LL][LH])//finds all cells that are to be born when the generation passes
{
	int i, j, k, t;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
		t=checkcell(arr[LL][LH], i, j);
		if(t==0){
		k=checkneigh(arr[LL][LH], i, j);
		if(k==3)
			arr[i][j]=3;
		}
		}
	}
}


void trans(int arr[LL][LH]){//executes the generation pass, makes all neccessery changes.
int i, j;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
			if(arr[i][j]==2){//if a cell is to die, it dies
				arr[i][j]=0;}
			if(arr[i][j]==3){//if a cell is to be born, it comes alive
				arr[i][j]=1;}
			}
	}
}




it returns 10 errors, similar ones.
main.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(20) : error C2664: 'init' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(21) : error C2664: 'finddie' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(22) : error C2664: 'findnewlife' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(23) : error C2664: 'trans' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(30) : error C2660: 'rand' : function does not take 1 parameters
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(35) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(85) : error C2664: 'checkcell' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(87) : error C2664: 'checkneigh' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(106) : error C2664: 'checkcell' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(108) : error C2664: 'checkneigh' : cannot convert parameter 1 from 'int' to 'int [][100]'
		Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

main.obj - 10 error(s), 0 warning(s)





Thanks in advance for any help. I'm really not much good at coding yet)

Is This A Good Question/Topic? 0
  • +

Replies To: Simple game of life implementation

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Simple game of life implementation

Posted 10 March 2009 - 09:52 AM

In main, in the calls to functions init, finddie, findnewlife, and trans, you are attempting to pass the array element at index [LL][LH] instead of the whole array.

Do you understand why that won't work?

This post has been edited by bsaunders: 10 March 2009 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:03 AM

Ah, yeah, i'm getting it... How do I i make it work though, arr[][]?
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:08 AM

I should work if you just provide the name of the variable without the subscripts.
Was This Post Helpful? 1
  • +
  • -

#5 tjust80  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 73
  • Joined: 01-December 08

Re: Simple game of life implementation

Posted 10 March 2009 - 10:09 AM

you done need to pass the global parameter int arr[LL][LH] at whole in any function, so that just remove this parameter from the prototype and declaration of functions

Regards
T
Was This Post Helpful? 1
  • +
  • -

#6 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:11 AM

Hmm, without subscripts it gives me syntax errors.
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(20) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(21) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(22) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(23) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(86) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(88) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(107) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\life2d\main.cpp(109) : error C2059: syntax error : ']'


Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:15 AM

Are you calling as init, finddie, findnewlife, and trans as:

init(arr);
finddie(arr);
findnewlife(arr);
trans(arr);
?

This post has been edited by bsaunders: 10 March 2009 - 10:15 AM

Was This Post Helpful? 1
  • +
  • -

#8 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:25 AM

Ah, yes, stupid mistake...
It should work now, but i'm getting :
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol [email protected]
Debug/life2d.exe : fatal error LNK1120: 1 unresolved externals

any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:26 AM

What compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#10 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:33 AM

View Postbsaunders, on 10 Mar, 2009 - 09:26 AM, said:

What compiler are you using?

Microsoft Visual C++ 6
Was This Post Helpful? 0
  • +
  • -

#11 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Simple game of life implementation

Posted 10 March 2009 - 10:42 AM

Are you sure you're building the program as a Win32 Console Program, and not a Win32 Application?
Was This Post Helpful? 0
  • +
  • -

#12 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Simple game of life implementation

Posted 10 March 2009 - 10:56 AM

you have many problems .. some of them:

1. main function should returns int which is 0

2. main function should never take parameters,
.... and if it should then with (char *argv[],int argc)

3. try to replace arr[LL][LH] with arr** to generalize

4. avoid shortcuts

5. this is function main after solving,

void main(int arr[][LH], int gencount){
	while(gencount>0){
		init(arr);
		finddie(arr);
		findnewlife(arr);
		trans(arr);
		gencount--;
	}
}


Was This Post Helpful? 1
  • +
  • -

#13 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Re: Simple game of life implementation

Posted 10 March 2009 - 11:08 AM

View Postbsaunders, on 10 Mar, 2009 - 09:42 AM, said:

Are you sure you're building the program as a Win32 Console Program, and not a Win32 Application?


It now returns no errors as a Console Program. It was a win32 application... Though it wont run, it crashes...
#define LL 100
#define LH 100
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>


int arr[LL][LH];
int gencount=1;

void trans(int arr[LL][LH]);   
void init(int arr[LL][LH]);
int checkcell(int arr[LL][LH], int i, int j);
int checkneigh(int arr[LL][LH], int i, int j);
void finddie(int arr[LL][LH]);
void findnewlife(int arr[LL][LH]);

void main(int arr[LL][LH], int gencount){// main, obviously)
	while(gencount!=0){
	init(arr);
	finddie(arr);
	findnewlife(arr);
	trans(arr);
	gencount++;
	}
}


void init(int arr[LL][LH]) //random initialization of the grid
{
srand(1);
int i, o;
for(i=0; i<LL; i++){
	for(o=0; o<LH; o++){
		arr[i][o]=abs(rand()%2);
	}
}
}

int checkcell(int arr[LL][LH], int i, int j)//checks if cell is alive
{
	if(arr[i][j]==1)
		return 1;
	else
		return 0;
}






int checkneigh(int arr[LL][LH], int i, int j)//counts the number of living neighbours to a given cell
{
	int t;
		  if(arr[i-1][j-1]==1)
			  t++;
		  if(arr[i][j-1]==1)
			  t++;
		  if(arr[i+1][j-1]==1)
			  t++;
		  if(arr[i-1][j]==1)
			  t++;
		  if(arr[i+1][j]==1)
			  t++;
		  if(arr[i-1][j-1]==1)
			  t++;
		  if(arr[i-1][j+1]==1)
			  t++;
		  if(arr[i][j+1]==1)
			  t++;
		  if(arr[i+1][j+1]==1)
			  t++;
		  return t;
}



void finddie(int arr[LL][LH])//finds all cells that are to die when the generation passes
{
	int i, j, k, t;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
			t=checkcell(arr, i, j);
			if(t==1){
			k=checkneigh(arr, i, j);
			if((k<2)||(k>4))
				arr[i][j]=2;
			}
		}
	}
}





void findnewlife(int arr[LL][LH])//finds all cells that are to be born when the generation passes
{
	int i, j, k, t;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
		t=checkcell(arr, i, j);
		if(t==0){
		k=checkneigh(arr, i, j);
		if(k==3)
			arr[i][j]=3;
		}
		}
	}
}


void trans(int arr[LL][LH]){//executes the generation pass, makes all neccessery changes.
int i, j;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
			if(arr[i][j]==2){//if a cell is to die, it dies
				arr[i][j]=0; printf("%d", arr[i][j]);}
			if(arr[i][j]==3){//if a cell is to be born, it comes alive
				arr[i][j]=1;printf("%d", arr[i][j]);}
			}
	}
}


Was This Post Helpful? 0
  • +
  • -

#14 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Simple game of life implementation

Posted 10 March 2009 - 11:11 AM

What happens when you run the debugger?

Where is the error occurring?

This post has been edited by bsaunders: 10 March 2009 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#15 DVH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-March 09

Re: Simple game of life implementation

Posted 11 March 2009 - 10:20 AM

Hmm, I solved that last problem. I have a new one as I'm trying to incorporate graphics. I'm using the allegro library, here's the current code:

main:
#define LL 100
#define LH 100
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include "graph.h"
#include <allegro.h>

int arr[LL][LH];
int gencount=1;

void trans(int arr[LL][LH]);   
void init(int arr[LL][LH]);
int checkcell(int arr[LL][LH], int i, int j);
int checkneigh(int arr[LL][LH], int i, int j);
void finddie(int arr[LL][LH]);
void findnewlife(int arr[LL][LH]);


void main(){
		gdraw();
	while(gencount>0){
		init(arr);
		finddie(arr);
		findnewlife(arr);
		gbegin();
		trans(arr);
		gend();
		gencount++;
	}
}


void init(int arr[LL][LH]) //Initialization, not ready
{

		arr[3][7]=1;
		arr[4][7]=1;
		arr[5][7]=1;
		arr[3][6]=1;
		arr[4][6]=1;
		arr[5][6]=1;

}

int checkcell(int arr[LL][LH], int i, int j)//Checks if cell is alive
{
	if(arr[i][j]==1)
		return 1;
	else
		return 0;
}






int checkneigh(int arr[LL][LH], int i, int j)//Counts number of living neighbours
{
	int t=0;
		  if(arr[i-1][j-1]==1)
			  t++;
		  if(arr[i][j-1]==1)
			  t++;
		  if(arr[i+1][j-1]==1)
			  t++;
		  if(arr[i-1][j]==1)
			  t++;
		  if(arr[i+1][j]==1)
			  t++;
		  if(arr[i-1][j-1]==1)
			  t++;
		  if(arr[i-1][j+1]==1)
			  t++;
		  if(arr[i][j+1]==1)
			  t++;
		  if(arr[i+1][j+1]==1)
			  t++;
		  return t;
}



void finddie(int arr[LL][LH])//Finds all cells to die this generation
{
	int i, j, k, t;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
			t=checkcell(arr, i, j);
			if(t==1){
			k=checkneigh(arr, i, j);
			if((k<2)||(k>4))
				arr[i][j]=2;
			}
		}
	}
}





void findnewlife(int arr[LL][LH])//Finds all cells to be born this generation
{
	int i, j, k, t;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
		t=checkcell(arr, i, j);
		if(t==0){
		k=checkneigh(arr, i, j);
		if(k==3)
			arr[i][j]=3;
		}
		}
	}
}


void trans(int arr[LL][LH]){//Makes all the generation changes in the array
int i, j;
	for(i=0; i<LL; i++)
	{
		for(j=0; j<LH; j++)
		{
			if(arr[i][j]==2)//cell to die
				arr[i][j]=0; drawgend(i, j);//Draws dead cells
			if(arr[i][j]==3){//Cell to be born
				arr[i][j]=1;drawgenl(i, j);}//draws living cells
			}
	}

}END_OF_MAIN();




graphics:

#include "graph.h"
#include <allegro.h>
#define USE_CONSOLE



void gbegin(void)
{
	 BITMAP *buffer;
		
   allegro_init();
   install_keyboard();
   install_mouse();
   install_timer();
   set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
   set_palette(desktop_palette);
   clear_bitmap(screen);
   acquire_screen();

}

void gend(void)
{
   release_screen();
}

void gclear(void)
{
   clear(screen);
}

void gdraw()
{
   rect(screen, 121, 41, 521, 441, makecol(0,0,0));
}
void drawgenl(int i, int j)
{
int x=120; 
int y=40;
rectfill(screen, x+4*i, y+4*j, x+4*i+4, y+4*j+4, makecol(0,0,0));
}
void drawgend(int i, int j)
{
int x=120;
int y=40;
rectfill(screen, x+4*i, y+4*j, x+4*i+4, y+4*j+4, makecol(255,255,255));
}





A header, graph.h

void gbegin(void);
void gend(void);
void gclear(void);
void gdraw(void);
void drawgenl(int i, int j);
void drawgend(int i, int j);




Edit:2 errors on the main:
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/lifecons.exe : fatal error LNK1120: 1 unresolved externals

This post has been edited by DVH: 11 March 2009 - 10:34 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2