Game Of Life

This is a discussion on Game Of Life within the C Programming forums, part of the General Programming Boards category; OK, the problem you have is that you're not passing the cell arrays defined in main() from one function to ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,688
    OK, the problem you have is that you're not passing the cell arrays defined in main() from one function to another.

    Here's an example
    Code:
    #include <stdio.h>
    
    #define SIZE  9
    
    void generate ( int cell[SIZE][SIZE] );
    void print ( int cell[SIZE][SIZE] );
    
    int main(void) {
      int board[SIZE][SIZE] = { { 0 } };
      generate ( board );
      print ( board );
      return 0;
    }
    
    void generate ( int cell[SIZE][SIZE] ) {
      cell[5][5] = 5;
    }
    
    void print ( int cell[SIZE][SIZE] ) {
      printf( "%d\n", cell[5][5] );
    }
    So you should just have the old and new generations defined in main(), and the rest of the functions should get visibility of those arrays by passing them around as parameters. Because all arrays are passed as pointers, modifications to the arrays in one function (eg. generate) will be visible in another (eg. print)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SELMATI ' '//dead cell
    #define SELHIDUP '*'//life cell
    #define MAXROW 9 //maxrow
    #define MAXCOL 9 //maxcolumn
    
    void srand(unsigned seed);
    int papar_menu();//main menu function
    int papar_mesin();//machine menu function
    int papar_manusia();//human menu function
    void papar_berhenti();//end game function
    void corak_rawak(char sel[MAXROW][MAXCOL]);//generate random pattern
    void corak_pemain(char sel[MAXROW][MAXCOL]);//generate user pattern
    char proses_generasi(char sel[MAXROW][MAXCOL], char sel2[MAXROW][MAXCOL]);//process next generation....seem not working well
    int kira_jiran(int row, int col);// count neighbour function
    void papar_corak_mesin(char sel[MAXROW][MAXCOL]);//display the pattern for machine game
    void papar_corak_pemain(char sel[MAXROW][MAXCOL]);//display the pattern created by user
    
    int main(int argc, char *argv[]){
      int pil1, pil2, pil3, pil4, k = 1;//pil = option
      char board1[MAXROW][MAXCOL] = {" "};
      char board2[MAXROW][MAXCOL] = {" "};
      int r, c;
      
      srand ( time ( NULL ) );
      
    	do{	   	
    	pil1 = papar_menu();  
    	     if (pil1 == 1){
    		    do{
    		       pil2 = papar_mesin();
    			        if(pil2 == 1){
    				    printf("start game\n");
    				    corak_rawak ( board1 );
    				    papar_corak_mesin ( board1 );
    				    do{
    				    printf("This is the pattern of your cell in generation %d.\n", k);
    					printf("Proceed to the generation %d ?( 1-Yes, 2-No ).\n", k+1);
    					do{
    					printf("\tYour Selection: ");
    					scanf ("%d", &pil4);
    					if( pil4 > 2 )
    						printf("\t\t-WRONG INPUT-\n\n");
    						}while( pil4 > 2 );
    					proses_generasi ( board1, board2 );
    //					board1[r][c] = board2[r][c];
    					papar_corak_mesin ( board1 );
    					k++;	
    							}while(pil4 == 1 );
                               }
                        if(pil2 > 2){
    		            printf("\t\t-WRONG INPUT-\n\n");
                               }
    				}while(pil2 > 2);
    	    	}
    	     else if (pil1 == 2){
                do{
    		       pil3 = papar_manusia();
    			        if(pil3 == 1){
    				    printf("Enter Pattern\n");
    				    corak_pemain ( board1 );
    				    papar_corak_pemain ( board1 );
    				    do{
    				    printf("This is the pattern of your cell in generation %d.\n", k);
    					printf("Proceed to the generation %d ?( 1-Yes, 2-No ).\n", k+1);
    					do{
    					printf("\tYour Selection: ");
    					scanf ("%d", &pil4);
    					if( pil4 > 2 )
    						printf("\t\t-WRONG INPUT-\n\n");
    						}while( pil4 > 2 );
    					papar_corak_pemain ( board1 );
    					k++;	
    							}while(pil4 == 1 );
                               }
                        if(pil3 > 2) {
    				    printf("\t\t-WRONG INPUT-\n\n");
                               }
    		 		}while(pil3 > 2);	
    		 	}
    	else if (pil1 == 3){
    		 papar_berhenti();
    		 break;
    		 			}
    	else {
    		 printf("\t\t-WRONG INPUT-\n\n");
    		 		}
    	}while( pil1 > 3 || pil2 == 2 || pil3 == 2 || pil4 == 2 );
               
                     
                     
      system("PAUSE");	
      return 0;
    }
    int papar_menu(){
         int pil1;
         printf("\n\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");
    	 printf("\tYour Selection: ");
         scanf ("%d", &pil1);
         return pil1; 	
          
    }
    int papar_mesin(){
          int pil2;
          printf("\n\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");
          printf("\tYour Selection: ");
          scanf ("%d", &pil2);
          return pil2;
    }
    int papar_manusia(){
         int pil3;
    	 printf("\n\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");
       	 printf("\tYour Selection: ");
       	 scanf ("%d", &pil3);
       	 return pil3;
    }
    void papar_berhenti(){
    	 printf("\n\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 din0908@gmail.com.\n\n");
    }
    void corak_rawak(char sel[MAXROW][MAXCOL]){
    	 int bil_sel, row, col, i;
    	 
    		bil_sel = ( rand() % 64 ) + 10;
    		for(i = 1;i <= bil_sel; i++){
    		row = ( rand() % 8 ) + 1;
    		col = ( rand() % 8 ) + 1;
          	sel[row][col] = SELHIDUP; 			/* masukkan sel dalam kotak */
      	}
    }
    void corak_pemain(char sel[MAXROW][MAXCOL]){
    	int bil_sel, row, col, i;
    	
    	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 );
      	for(i=1;i<=bil_sel;i++){
    	  printf("\nPlease insert the row for cell (1-8) %d:", i);
    	  scanf ("%d", &row );
    	  printf("\nPlease insert the column for cell (1-8) %d:", i);
    	  scanf ("%d", &col );
         	  sel[row][col] = SELHIDUP; // masukkan sel berdasarkan pilihan pengguna
    		}
    }
    char proses_generasi(char sel1[MAXROW][MAXCOL],char sel2[MAXROW][MAXCOL]){
    	int row, col, jum_jiran;
    //	char sel2[MAXROW][MAXCOL];
    	
    //	jum_jiran = kira_jiran ( row, col );
    	
    	for (row = 1; row < 9; row++){
    		for (col = 1; col < 9; col++){
    			jum_jiran = kira_jiran ( row, col );
    			
    			if (sel[row][col] == ' ' && jum_jiran == 3 ){
    				sel2[row][col] = SELHIDUP;
    						}
    			else if (sel[row][col] == '*' && jum_jiran == 2){
    				sel2[row][col] = SELHIDUP;
    						}
    			else if (sel[row][col] == '*' && jum_jiran == 3){
    				sel2[row][col] = SELHIDUP;
    						}
    			else {
    			    sel2[row][col] = SELMATI;
    						}
    			    	}
    			 }
    	return sel2[row][col];	
    }
    int kira_jiran(int row, int col){
    	int jiran;
    	char sel[MAXROW][MAXCOL];
    	
    	if ( sel[row+1][col+1] == '*')
    			jiran++;
    	if ( sel[row+1][col] == '*' )
    			jiran++;
    	if ( sel[row+1][col-1] == '*')
    			jiran++;
    	if ( sel[row][col-1] == '*' )
    			jiran++;
    	if ( sel[row-1][col-1] == '*' )
    			jiran++;
    	if ( sel[row-1][col] == '*' )
    			jiran++;
    	if ( sel[row-1][col+1] == '*' )
    			jiran++;
    	if ( sel[row][col+1] == '*' )
    			jiran++;
    	
    	return jiran;
    }
    void papar_corak_mesin(char sel[MAXROW][MAXCOL]){
    	int row, col;
    	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';
      	
    	printf("\t\t========================\n");
       	printf("\t\t@ Machine Destiny Game @\n");
       	printf("\t\t========================\n");
      	for ( row = 0; row < 9; row++ ) {
        		for ( col = 0; col < 9; col++ ) {
           			printf(" %c", sel[row][col] );
        			}
        		printf( "\n" );
      	}	
    	
    }
    void papar_corak_pemain(char sel[MAXROW][MAXCOL]){
    	int row, col;
    	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';
    	
    	printf("\t\t======================\n");
       	printf("\t\t@ Human Destiny Game @\n");
       	printf("\t\t======================\n");
      	for ( row = 0; row < 9; row++ ) {
        		for ( col = 0; col < 9; col++ ) {
           			printf(" %c", sel[row][col] );
        			}
        		printf( "\n" );
      	}
    }
    This is the latest coding for my Game of life. I think this one is more manageable and if you still remember the variable I use in my last coding.... I hope you can still understand this coding.... I really hope so... .....

    The problem is there is some kind of run time error when the program try to replace the first generation with the second generation and so on..... maybe there some kind of coding error that I have done... especially at here......
    Code:
    board1[r][c] = board2[r][c];
    or maybe at the other function..... Hope someone can help a little....

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,688
    > board1[r][c] = board2[r][c];
    You're trying to copy one board to another, yet this only copies a single cell (note that r,c are uninitialised as well).

    This is one way of copying the whole board in one easy step (C doesn't have an array assignment operator)
    Code:
    memcpy ( &board1[0][0], &board2[0][0], sizeof board1 );
    Code:
    int kira_jiran(int row, int col){
    	int jiran;
    	char sel[MAXROW][MAXCOL];
    1. Initialise your count to 0 before you start adding
    2. pass the sel array as a parameter, you're just counting uninitialised junk in a local array with an uninitialised counter.

    > for ( row = 0; row < 9; row++ )
    for ( row = 0; row < MAXROW; row++ )
    Same for all your other loops, use the appropriate defined constant to limit the loop, not some magic number.

    Code:
     	sel[0][1]='1';
      	sel[0][2]='2';
      	sel[0][3]='3';
    etc etc
    I don't get this - I thought cells were either ' ' for empty or '*' for alive.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Salem
    This is one way of copying the whole board in one easy step (C doesn't have an array assignment operator)
    Code:
    memcpy ( &board1[0][0], &board2[0][0], sizeof board1 );
    Not sure if you were just trying to explicitly show the addressing, but I'd have thought:
    Code:
    memcpy ( board1, board2, sizeof board1 );
    would be cleaner to read.

  5. #20
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    OoO... I see... so for memcpy... both sintaks can be use and since I'm a beginner programmer, so the first post by Salem is more readable by me...

    For sel[0][0] = ' ';

    No more include in my program... because my first thought that I want to include some number in the table so that my lecture know whether the coordinat input by user is right or not.... So after thinking for a while, I have found a new ways to do that...

    Here is my complete code for my Game of Life.... hope you guys all try to play it and have fun
    Attached Files Attached Files

  6. #21
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Ah... forgot to ask....

    How long have you guys all involve in programming???

    If I want to advance to the next level, what should I learn in programming???

    Which programming nowadays is better, C++ or Java and which one is more use by industri company??

    Just asking something that is not related to C.... hope did not got scolded by board administrator......... :P

Page 2 of 2 FirstFirst 12
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-07-2010, 11:08 PM
  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, 05:44 PM

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