I'm stuck..please help me out..

This is a discussion on I'm stuck..please help me out.. within the C++ Programming forums, part of the General Programming Boards category; I'm having problems setting up the grid for this problem and i'm not really sure if it's completely correct..So could ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Post I'm stuck..please help me out..

    I'm having problems setting up the grid for this problem and i'm not really sure if it's completely correct..So could you please help me out where you can..

    Here's the question:

    WRITE A PROGAM TO STIMULATE THE “GAME OF LIFE”. THERE IS A CITY THAT IS LAID OUT AS A GRID. EACH CELL IN THE GRID CAN BE EITHER ALIVE OR DEAD. EACHCELL HAS A MAXIMUM OF EIGHT NEIGHBOURS, WHICH ARE THE CELLS ADJACENT TO IT IN THE GRID. IF A CELL HAS MORE THAN FOUR NEIGHBOURS, IT WILL DIE OF OVERCROWDING IN THE GENERATION. IF IT HAS LESS THAN THREE NEIGHBOURS IT WILL ALSO DIE IN THE NEXT GENERATION FROM LONELINESS, OTHERWISE THE CELL LIVES IN THE NEXT GENERATION. THE GRID HAS NO BOUNDARIES, SO IF YOU ARE AT THE LAST CELL OF THE RIGHT EDGE OF THE GRID AND MOVE ONE CELL TO THE RIGHT, YOU WILL BE IN THE FIRST OF THE LEFT EDGE OF THE GRID AND VICE VERSA. IF YOU ARE THE LAST CELL AT THE BOTTOM OF THE GRID AND MOVE DOWN ONE CELL, YOU WILL BE AT THE FIRST CELL AT THE TOP OF THE GRID AND VICE VERSA. THE PROGRAM SHOULD RANDOMLY POPULATE A 50*50 GRID WITH LIVING CELLS, DISPLAY THE GRID ON THE SCREEN AND SHOULD ITERATE THROUGH 1000 GENERATIONS. AFTER EACH GENERATION THE NEW GRID IS DISPLAYED ON THE SCREEN.

    And here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int array[50][50]={0};
    int array1[50][50]={0};
    
    
    int function1(int num,int num2)
    {
    	int num3;
    	num3=array[num][num2];
    	return num3;
    }
    
    
    int function2(int num,int num2)
    {
    	int num3=0;
    	
    	if(num<0&&num2<0)
    	{
    		num3=function1(49,49);
    		return num3;
    	}
    	else
    	if(num<0&&num2>49)
    	{
    		num3=function1(49,0);
    		return num3;
    	}
    	else
    	if(num>49&&num2<0)
    	{
    		num3=function1(0,49);
    		return num3;
    	}
    	else
    	if(num>49&&num2>49)
    	{
    		num3=function1(0,0);
    		return num3;
    	}
    	else
    	if(num<0&&num2>=0)
    	{
    		num3=function1(49,num2);
    		return num3;
    	}
    	else
    	if(num>49&&num2>=0)
    	{
    		num3=function1(0,num2);
    		return num3;
    	}
    	else
    	if(num>=0&&num2<0)
    	{
    		num3=function1(num,49);
    		return num3;
    	}
    	else
    	if(num>=0&&num2>49)
    	{
    		num3=function1(num,0);
    		return num3;
    	}
    	return 0;
    }
    
    
    void function3()
    {
    	int num,num2;
    	for(num=0;num<50;num++)
    	{
    		for(num2=0;num2<50;num2++)
    		{
    			array[num][num2]=array1[num][num2];
    		}
    	}
    	for(num=0;num<50;num++)
    	{
    		for(num2=0;num2<50;num2++)
    		{
    			array1[num][num2]=0;
    		}
    	}
    }
    
    
    int function4(int num,int num2)
    {
    	int num3=0;
    	if((num<0||num>49)||(num2<0||num2>49))
    	{
    		num3=function2(num,num2);
    		return num3;
    	}
    	else
    	{
    		num3=function1(num,num2);
    		return num3;
    	}
    }
    
    int function5(int num,int num2)
    {
    	int num3=0;
    	
    	if((function4((num-1),(num2-1)))==1)
    	{
    		num3++;
    	}
    	if((function4((num-1),num2))==1)
    	{
    		num3++;
    	}
    	if((function4((num-1),(num2+1)))==1)
    	{
    		num3++;
    	}
    	if((function4(num,(num2+1)))==1)
    	{
    		num3++;
    	}
    	if((function4((num+1),(num2+1)))==1)
    	{
    		num3++;
    	}
    	if((function4((num+1),num2))==1)
    	{
    		num3++;
    	}
    	if((function4((num+1),(num2-1)))==1)
    	{
    		num3++;
    	}
    	if((function4(num,(num2-1)))==1)
    	{
    		num3++;
    	}
    	return num3;
    }
    
    
    void function7()
    {
    	int num,num2;
    	for(num=0;num<50;num++)
    	{
    		for(num2=0;num2<50;num2++)
    		{
    			if(((function5(num,num2))==4)||((function5(num,num2))==3))
    			{
    				array1[num][num2]=1;
    			}
    			else
    			{
    				array1[num][num2]=0;
    			}
    		}
    	}
    }
    
    
    void main()
    {
    	int num,num2,num3;
    
    	array[49][0]=1;
    	array[49][49]=1;
    	array[0][0]=1;
    	array[0][1]=1;
    	array[0][2]=1;
    	array[0][3]=1;
    	array[0][4]=1;
    	array[0][5]=1;
    	array[0][6]=1;
    	array[0][7]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    	array[2][49]=1;
    
    	for(num=0;num<50;num++)
    		{
    			for(num2=0;num2<50;num2++)
    			{
    				if((array[num][num2])==1)
    				{
    					printf("*");
    				}
    				else
    				{
    					printf(" ");
    				}
    			}printf("\n");
    		}
    
    	for(num3=0;num3<300;num3++)
    	{
    		system("cls");
    		function7();
    		function3();
    		for(num=0;num<50;num++)
    		{
    			for(num2=0;num2<50;num2++)
    			{
    				if((array[num][num2])==1)
    				{
    					printf("*");
    				}
    				else
    				{
    					printf(" ");
    				}
    			}printf("\n");
    		}Sleep(240);
    	}
    }

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    You should really give your functions and variables meaningful names and comment your code. It makes it a lot easier to understand what's going on

    What problem is it that you're having, exactly?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Basically i think i have everything else down ok,except for the whole grid situation..
    The whole idea is for it to be like John Conway's "Game of Life", and it should all be in a grid. But i don't know how to do that..I'm new to programming so please bear with me.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Void main should be int main
    Is this C or C++ code?
    Also your functions should have prototypes before they are implemented
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Maybe you should start off smaller and build up to the full program? So, work on a function that draws the grid.

    I don't know what you're referring to re: Conway's Game of Life, but I can't imagine this code does what you want to do... Start off small and build up to the full program. Label your variables and functions meaningfully.. eg. Score instead of num1.. or whatever

    Atm you have stuff like this in there :-
    Code:
    array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    	array[1][49]=1;
    I mean every one of those lines does the same thing, hehe, so.... you probably don't know what this code really does either

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    int array[50][50]={0};
    int array1[50][50]={0};
    2D arrays must be initialized with
    Code:
    {{0}}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by dwks View Post
    2D arrays must be initialized with
    Code:
    {{0}}
    Uh, really?

    The problem with the code is that it is probably too long, it doesn't use descriptive names, it does use magic numbers all over the place etc. Therefore it is somewhat hard to help with that.

    In general, with a game of life, you'll need something like that:

    1) Mark cells randomly to be alive.

    2) Loop over the grid and count the living neighbours of each cell. Since your world wraps around, you can probably use the modulo operator (&#37 and won't need any special cases for borders at all.
    Depending on the count of neighbours make a desicion what will happen to this cell in the next generation. But don't modify the cell right away. Instead save the decision somewhere (you'll need another array for that).

    3) Loop over the grid again, and change the state of the cells according to the decisions that you have stored.

    4) Output and repeat with 2).

    By the way, do "dead" cells come to life too?
    Last edited by anon; 04-02-2007 at 04:20 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    > Uh, really?
    Oops. I was thinking of something else . . .

    By the way, do "dead" cells come to life too?
    Yes. http://en.wikipedia.org/wiki/Conway's_Game_of_Life

    The problem with the code is that it is probably too long, it doesn't use descriptive names, it does use magic numbers all over the place etc. Therefore it is somewhat hard to help with that.
    Yes, definitely. I'm sure the OP can think of better names than function1, function2 etc.

    Also your functions should have prototypes before they are implemented
    Function prototypes are not strictly required here, since every function call comes after the function has been defined.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21