1 Replies - 521 Views - Last Post: 14 April 2011 - 06:41 AM Rate Topic: -----

#1 lokyblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-March 10

Game of life print problem

Posted 14 April 2011 - 05:45 AM

hello everyone,

i did this game for my homework it is simple but i guess i have a mistake here can anyone help me and how shpuld i clear the screen after one turn ?


/* dimensions of the screen */

#define TABLE_ROWS	25
#define TABLE_COLUMS 25

#include <stdio.h>

#include <time.h>


void initialize_table (char table[][TABLE_COLUMS]);    /* set everthing to another character than adding 'X' and ' ' */

void play (char table[][TABLE_COLUMS]);               /*playing the game depending the rules*/

void print (char table[][TABLE_COLUMS]);              /*printing the game  */


/* main program */

int main () 

{

    /* my variables*/

	int	 i,j;

    char table[TABLE_ROWS][TABLE_COLUMS];

    srand ( time ( NULL ) );


	initialize_table (table);            /*initiliazing 'X' and ' ' for very first time*/


	/* play game of life 100 times */

	for (i=0; i<100; i++) {
		print (table);
		play (table);

		/* clear the screen using VT100 escape codes */

	}


return 0;

}


/**************************************************initializing table***********************************************************************************/



void initialize_table (char table[][TABLE_COLUMS])
{

	int	i, j, a;

	for (i=0; i<TABLE_ROWS; i++) 
		{

		for (j=0; j<TABLE_COLUMS; j++)
	        { 
		
        	a = rand () % 2 ;  /*using rand function to fill the table*/

        	if( a==0 )                    /*if number was 0 the square filled with  ' '*/
        	  {

            	table[i][j] = ' ';

              }
        	else if ( a==1 )              /*if number was 1 the square filled with  'X'*/
              {

            	table[i][j] = 'X';
        
        	  }


    }


}
}

/*******************************************************************************************************************************************************/






/******************************************************* playing the game ******************************************************************************/


void play (char table[][TABLE_COLUMS])
{

     int neighbors;
    
     int  i, j;

     char table_new[TABLE_ROWS][TABLE_COLUMS];

	 for (i = 0; i < TABLE_ROWS; i++)
		 {
			 for (j = 0; j < TABLE_COLUMS; j++)
			 {
				     neighbors = 0;
			     /*Begin counting number of neighbors: */

			     if (table[i-1][j-1] == 'X') neighbors += 1;		/*up left corner*/

			     if (table[i-1][j] == 'X') neighbors += 1;		/*up head*/

			     if (table[i-1][j+1] == 'X') neighbors += 1;		/*up right corner*/

			     if (table[i][j-1] == 'X') neighbors += 1;		/*left side*/

			     if (table[i][j+1] == 'X') neighbors += 1;		/*right side*/

			     if (table[i+1][j-1] == 'X') neighbors += 1;		/*down left corner*/

			     if (table[i+1][j] == 'X') neighbors += 1;		/*bottom down*/

			     if (table[i+1][j+1] == 'X') neighbors += 1;		/*down right corner*/
			     

			     //Apply rules to the cell:
			     if (table[i][j]== 'X' || neighbors < 2)                     /* a cell dies of loneliness, if it has less than two neighbours */
			        table_new[i][j] = ' ';
			     
				 else if (table[i][j]== 'X' || neighbors > 3)				/* a cell is born, if it has exactly three neighbours*/
			        table_new[i][j] = ' ';
			     
  				 else if (table[i][j]== 'X' || (neighbors == 2 && neighbors == 3))        /* a cell survives, if it has two or three neighbours */
			        table_new[i][j] = 'X';
			     
				 else if (table[i][j]== ' ' || neighbors == 3)				/* a cell is born, if it has exactly three neighbours */
			        table_new[i][j] = 'X';
			 }
		 }
	

// replacing the new table with old one

     for (i = 0; i < TABLE_COLUMS; i++)
     {
      
  		 for (j = 0; j < TABLE_ROWS; j++)
             {

			  table[i][j] = table_new[i][j];

			 }

     }

}

/*******************************************************************************************************************************************************/




/********************************************************printing game on screen************************************************************************/





void print (char table[][TABLE_COLUMS]) 
{	

	int	i, j;

	/* for each colums */

	for (j=0; j<TABLE_COLUMS; j++) {

		/* print each row position */

		for (i=0; i<TABLE_ROWS; i++) 
		  {

    		printf("%c", table[i][j] ? 'X' : ' ');

    	  }

		/* starting new line after a row finished */

		printf("\n");
	}



       printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");


}


/******************************************************************************************************************************************************/




Is This A Good Question/Topic? 0
  • +

Replies To: Game of life print problem

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1688
  • View blog
  • Posts: 3,209
  • Joined: 30-May 10

Re: Game of life print problem

Posted 14 April 2011 - 06:41 AM

> if (table[i-1][j-1] == 'X')
You have to be careful here, because there are a lot of out-of-bound array accesses around the edges of the grid.

As for clearing the screen, that depends on your OS and Compiler.
If it really does support ANSI escape codes, a simple web search should tell you what to print,
say printf("\033" "2J");
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1