Thread: Game Of Life

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    Game Of Life

    hi all
    Im a new person here and probably quite new in c programming.
    I just want to ask some help.
    Everyone here have heard about this game before??
    Does anyone have try to code this program??
    Can I see the code??

    All code can be sent to [email protected]
    This is my first project and I'm still cannot understand the implementation of an array 2d.....
    Hope you guys all can help a little bit..

    Thx all.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If this is homework, try to do it yourself first. If this isn't homework, try to do it yourself first. If you get stuck, post your code.

    Also, try google. Here's a wonderful link, too:

    http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

    A 2D array is like a 1D array, except it has two dimensions. :P

    Code:
    int array[30][30];
    You can access each element using array[x][y]. So define an array, initialise it to a state representing "dead" cells, then put a few "live" cells in. Then iterate through the array and apply the Game of Life rules to it. You may need to keep two copies of the array, because the next iteration of game of life needs each cell to know about the previous state, and your calculations will clobber the old state.
    Last edited by cwr; 09-21-2005 at 12:43 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Everyone here have heard about this game before??
    Yes

    > Does anyone have try to code this program??
    Yes

    > Can I see the code??
    No

    > All code can be sent to [email protected]
    Yeah right!
    Do your own fsking homework next time.
    Anyone too dumb to type in "game of life source code" into google deserves to fail miserably.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Hahaha.....I actually have wrote a quite long code for the interface. And now I have to make the array board. it quite suceesful actually but the problem is everytime I run the part of the array, there is some unneccesary symbol appear in my array...

    I actually ask for the code only for the reference..the is no way im going to use it to be sent to my lecture.... learning C from example is more understandable than read the whole book about the sintaks....

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > everytime I run the part of the array, there is some unneccesary symbol appear in my array
    So post it, and then perhaps we can tell you where you're going wrong.

    > I actually ask for the code only for the reference
    Looking at other peoples code no more teaches you C than reading a book does. Chances are, the C code is a worse example than a decent book.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    Find examples of source code using Google and try to understand them. After that, translate one into C and see what you get.

    This Google search produced some good results:

    Code:
    "game of life" source
    as evidenced here

    Din1983, edit your post to remove your e-mail address. As Salem has pointed out (in his usual manner) nobody is going to e-mail stuff to you: firstly it defeats the object of having a forum and secondly it's not the way this board operates. The only thing you will get is spam once a spambot harvests your address. This will only be a matter of time.

    Salem, you should also edit your post to remove his e-mail address for the same reason.
    Last edited by Driver; 09-22-2005 at 05:47 AM. Reason: Add information
    I think you can put a signature here.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    The weird symbol ..... I already get rip off it.... and now im implement the code for program generate the next generation.... I think this is the most critical part of it...

    Question: can I use this kind of code in C array ??
    Code:
    for(x=1;x<9;x++){
        for(y=1;y<9;y++){
              if (cell [x] [y+1] == '*')
                   counter ++;
              if(cell [x] [y-1] == '*')
                    counter ++;
               ...........and so on......
             }
       }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yes you can.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Is that code mean ..... the counter is added when the condition is true right??
    Anyone have a good code to show??

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Post your actual code.

    Code snippets containing a few statements and then "...........and so on......" just don't cut it, and won't get other people to finish the code for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Alright...sory for that...

    Code:
    int counter()
    {
    for (x=1;x<9;x++){
          for (y=1;y<9;y++){
          if (cell [x+1][y+1] =='*')
                   counter++;
          if (cell[x+1][y] =='*')
                   counter++;
          if (cell[x+1][y-1] =='*')
                   counter++;
          if (cell[x][y-1] =='*')
                   counter++;
          if (cell[x-1][y-1] =='*')
                   counter++;
          if (cell[x-1][y] =='*')
                   counter++;
          if (cell[x-1][y+1] =='*')
                   counter++;
          if (cell[x][y+1] =='*')
                   counter++;
          
               }
         }
         return counter;
    }
    Any mistake in this code??

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > counter++;
    Well did you compile it?
    For a start, this is also the name of your function, so the chance that it will compile is zero.

    > cell [x+1][y+1]
    How did you define the cell array - you need at least 10x10
    You also need to make sure the strip of cells around the perimeter are initialised to say zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    This is my full code for the program so far.... it is not quite complete actually... and I do hope someone can understand it and try to find the error for me....

    This game consist of 2 gameplay call Machine destiny Game and Human destiny Game.... In machine game the pattern of the cell is generate randomly but in the human game the user can define their own pattern...

    The problem might be with the array and also function call...I think I might need to study more on it....here is the long code...the longest code I ever wrote...hehehe....

    Code:
    /*Projek:Game Of Life*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SELMATI ' ';//assign dead cell
    #define SELHIDUP '*'; //assign life cell
    
    void srand(unsigned seed);
    void papar_menu();//function to display main menu
    void papar_mesin();//function to display machine game menu
    void papar_manusia();//function to display human game menu
    void papar_berhenti();//function to display end game
    void papar_corak();//function to display the pattern randomly(for machine game)
    void papar_buat_corak();//function for user enter the location of cell
    char proses_generasi(int);//function to process the next generation
    int kira_jiran();//function to count the neighbour
    int jana_kedudukan_sel();//function to generate random location for cell
    
    int main(){
    	int pilihan, pilihan2, pilihan3;//pilihan(malay) == option(English)
    	int jum_jiran, x, y;  //jum_jiran == total_neighbour
    	char sel[9][9], sel2[9][9]; //sel == cell
    	srand ( time ( NULL ) );
    	do{	   	
    	papar_menu();  //papar_menu() == display_menu()
    	printf("\t\tSelection:");
    	scanf ("%d",&pilihan);
    	if (pilihan==1){
    		do{
    		papar_mesin();//papar_mesin() == display_machine()
    		printf("\t\tSelection:");
    		scanf ("%d",&pilihan2);
    			if(pilihan2==1){
    				printf("start game\n");
    				papar_corak();//papar_corak() == display_pattern()
    				jum_jiran = kira_jiran ();//kira_jiran() == count_neighbour
    				printf("%d\n",jum_jiran);
    				proses_generasi (jum_jiran);//proses_generasi()== process_generation()
    				sel[x][y] = sel2[x][y];//sel[x][y] == cell[x][y]
    				for ( x = 0; x < 9; x++ ) {
        				for ( y = 0; y < 9; y++ )
        				{
            				printf( " %c", sel[x][y] );
        						}
        					printf( "\n" );
      						}
    				}
    			if(pilihan2>2) {
    				printf("\t\t-WRONG INPUT-\n\n");
    					}
    				}while(pilihan2>2);
    	    	}
    	else if (pilihan==2){
    		do{
    		papar_manusia();//papar_manusia() == display_human()
    		printf("\t\tSelection:");
    		scanf ("%d",&pilihan3);
    			if(pilihan3==1){
    				printf("Enter Pattern\n");
    				papar_buat_corak();
    				}
    			if(pilihan3>2) {
    				printf("\t\t-WRONG INPUT-\n\n");
    					}
    		 		}while(pilihan3>2);	
    		 	}
    	else if (pilihan==3){
    		 papar_berhenti();//papar_berhenti() == display_endgame()
    		 break;
    		 			}
    	else {
    		 printf("\t\t-WRONG INPUT-\n\n");
    		 		}
    	}while(pilihan>3 || pilihan2==2 || pilihan3==2);
    	
     	return 0;
     	
    }void papar_menu(){
    	
    	printf("\t\t######******######\n");
      	printf("\t\t@= GAME OF LIFE =@\n");
          	printf("\t\t######******######\n");
          	printf("\n\t     -Welcome to Game Of Life-\n");
          	printf("\nRules Of The Game.\n");
          	printf("1. A cells  is born in an empty  box when it has exactly 3 neighbors.\n");
          	printf("2. A cells dies from loneliness if it has fewer than 2 neighbors.\n");
          	printf("3. A cells dies of overcrowding if it has more than 3 neighbors.\n");
          	printf("4. Otherwise, the cells  survives.\n");
          	printf("\nPlease select the option from Main Menu.\n");
          	printf("\t\t:Main Menu:\n");
     	printf("1.Machine Destiny Game\n");
     	printf("2.Human Destiny Game\n");
     	printf("3.Quit Game\n");
    	  	
          
    }void papar_mesin(){
    	printf("\t\t========================\n");
          	printf("\t\t@ Machine Destiny Game @\n");
          	printf("\t\t========================\n");
          	printf("In this game, computer will generate the pattern of cell randomly for you.\n");
          	printf("All you have to do is sit back and watch the cell progress.\n");
          	printf("Please choose option below to proceed and have fun!\n");
          	printf("1.Start Game\n");
          	printf("2.Main Menu\n");
    }
    void papar_manusia(){
    	printf("\t\t======================\n");
          	printf("\t\t@ Human Destiny Game @\n");
          	printf("\t\t======================\n");
          	printf("In this game, you are freely to choose the pattern of cell.\n");
          	printf("Then, you can see the cell progress based on your cell pattern.\n");
          	printf("Please choose option below to proceed and have fun!\n");
          	printf("1.Enter Pattern\n");
          	printf("2.Main Menu\n");
    }
    void papar_berhenti(){
    	
          	printf("\t\t=============\n");
          	printf("\t\t@ Quit Game @\n");
          	printf("\t\t=============\n");
          	printf("Thank you for trying this game. I hope you have found a lot of fun here.\n");
          	printf("Any comments, suggestions or bugs can be sent to [email protected].\n");
    }
    void papar_corak(){
    	  	int x,i;
      		int y,bil_sel;
      		char sel[9][9]; /* array untuk membuat kotak sel == array to make empty cell box*/
      
      	for( x = 0; x < 9; x++ ){		//memastikan kotak sel dikosongkan dahulu 
      		for( y = 0; y < 9; y++ ){     	// == make sure the cell box is empty
      			sel[x][y]=SELMATI;
      			}
      		}
      	bil_sel = ( rand() % 64 ) + 20;
    	sel[0][0]=' ';//membuat nombor untuk baris dan lajur
     	sel[0][1]='1';//assign the number for row and column
      	sel[0][2]='2';
      	sel[0][3]='3';
      	sel[0][4]='4';
      	sel[0][5]='5';
      	sel[0][6]='6';
      	sel[0][7]='7';
      	sel[0][8]='8';
      	sel[1][0]='1';
      	sel[2][0]='2';
      	sel[3][0]='3';
      	sel[4][0]='4';
      	sel[5][0]='5';
      	sel[6][0]='6';
      	sel[7][0]='7';
      	sel[8][0]='8';
      
      	for(i = 1;i < bil_sel; i++){
    		x = jana_kedudukan_sel();
    		y = jana_kedudukan_sel();
          	sel[x][y] = SELHIDUP; /* masukkan sel dalam kotak == insert the life cell in box*/
      	}
      	printf("\t\t========================\n");
       	printf("\t\t@ Machine Destiny Game @\n");
       	printf("\t\t========================\n");
      	for ( x = 0; x < 9; x++ ) {
        		for ( y = 0; y < 9; y++ ) {
           			printf(" %c", sel[x][y] );//display the pattern of cell
        			}
        		printf( "\n");
      	}
    }
    void papar_buat_corak(){
    	  	int x,i;
      		int y,bil_sel;
      		char sel[9][9]; 
      
      for(x = 0;x < 9; x++){			//memastikan kotak sel dikosongkan dahulu
      		for(y = 0;y < 9; y++){
      			sel[x][y]=SELMATI;
      			}
      		}	
      	printf("\t\t======================\n");
       	printf("\t\t@ Human Destiny Game @\n");
       	printf("\t\t======================\n");
       	printf("Please enter your cell pattern. The pattern is based on the\n"); 
       	printf("location(row and column)and quantity of cells.\n");
       	printf("The maximun quantity of cells is 64.\n");
    	printf("And the valid number for row and column is 1 to 8.\n\n");
    	do{
    	printf("\tHow many cell you need:");
      	scanf ("%d", &bil_sel);
      	}while ( bil_sel > 64 );
      		sel[0][0]=' ';/*membuat nombor untuk baris dan lajur*/
      		sel[0][1]='1';
      		sel[0][2]='2';
      		sel[0][3]='3';
      		sel[0][4]='4';
      		sel[0][5]='5';
      		sel[0][6]='6';
      		sel[0][7]='7';
      		sel[0][8]='8';
      		sel[1][0]='1';
      		sel[2][0]='2';
      		sel[3][0]='3';
      		sel[4][0]='4';
      		sel[5][0]='5';
      		sel[6][0]='6';
      		sel[7][0]='7';
      		sel[8][0]='8';	
      for(i=1;i<=bil_sel;i++){
    	  printf("\tPlease insert the row for cell %d:",i);
    	  scanf ("%d", &x );
    	  printf("\tPlease insert the column for cell %d:",i);
    	  scanf ("%d", &y );
         	  sel[x][y] = SELHIDUP; // masukkan sel berdasarkan pilihan pengguna 
      			}  // == insert the life cell according to the user choice
      	for ( x = 0; x < 9; x++ ) {
        		for ( y = 0; y < 9; y++ ) {
            		printf( " %c", sel[x][y] );
        			}
        		printf( "\n" );
      	}
          
          
    }
    char proses_generasi(int){ // function to process the next generation
    	int x,y,jum_jiran;
    	char sel[9][9],sel2[9][9];
    	sel2[x][y] = sel[x][y];
    	   for (x=0; x<9; x++){
    	        for (y=0; y<9; y++){
    	             if (sel[x][y] == ' ' && jum_jiran == 3 ){
    			sel2[x][y] = SELHIDUP;
    				}
    	             else if (sel[x][y] == '*' && jum_jiran == 2){
    			sel2[x][y] = SELHIDUP;
    				}
    	             else if (sel[x][y] == '*' && jum_jiran == 3){
    			sel2[x][y] = SELHIDUP;
    				}
    	             else {
    			 sel2[x][y] = SELMATI;
    				}
    			    }
    			}
    		return sel2[x][y];
    		
    }
    int kira_jiran (){ // function to count the neigbour
    	int x,y,jiran;
    	char sel[9][9];
    		for ( x=0; x<9; x++){
    			for ( y=0; y<9; y++){
    				if ( sel[x+1][y+1] == '*')
    					jiran++;
    				if ( sel[x+1][y] == '*' )
    					jiran++;
    				if ( sel[x+1][y-1] == '*')
    					jiran++;
    				if ( sel[x][y-1] == '*' )
    					jiran++;
    				if ( sel[x-1][y-1] == '*' )
    			      		jiran++;
    				if ( sel[x-1][y] == '*' )
    					jiran++;
    				if ( sel[x-1][y+1] == '*' )
    					jiran++;
    				if ( sel[x][y+1] == '*' )
    					jiran++;
    					
    					}
    			}
    		return jiran;
    	
    	
    	
    } 
    int jana_kedudukan_sel(){ // function to generate random number for cell location
    	return ( rand() % 8 ) + 1;
         
    }
    Please help me with this .... it really mean a lot to me...thank you so much...

  14. #14
    ---
    Join Date
    May 2004
    Posts
    1,379
    Sorry but it's kinda hard to understand the function names.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    10


    it is....poor me....but its ok..thx for everything that you have done...

    Question: in this game, the pattern of cell are created in one function. in other function, I want to count the neighbour of the cell based on the cell pattern that already create before. so what is the sintaks for my function to count the cell neighbour??I still cannot understand how the function is going to retrieve the cell pattern created and use it in that function.....

    it is like this....
    Code:
    void create_pattern ( char cell[x][y] );
    int count_neighbour ( char cell[x][y] );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM