Exemplo n.º 1
0
Arquivo: worker.c Projeto: melfnt/wtr
void elabora (wator_t *pw, rect_t r)
{
	int i,j,k,l;
	for ( i=r.i; i<r.i+r.h; ++i )
	{
		for ( j=r.j; j<r.j+r.w; ++j )
		{
			if ( pw->plan->dtime[i][j]>0 )
			{
				k=i;
				l=j;
				if ( pw->plan->w[k][l] == FISH )
				{
					fish_rule4 ( pw, i, j, &k, &l );
					fish_rule3 ( pw, i, j, &k, &l );
				}
				else if ( pw->plan->w[k][l] == SHARK )
				{
					shark_rule2 ( pw, i, j, &k, &l );
					shark_rule1 ( pw, i, j, &k, &l );
				}
				//segnalo che l'animale in posizione (k,l) si é già mosso
				pw->plan->dtime[k][l] = -pw->plan->dtime[k][l];
			}
		}
	}
}
Exemplo n.º 2
0
int update_riga (wator_t * pw, int row_num)
{
	int _ncol = pw->plan->ncol;
	int j, k, l;

	if(pw == NULL)
	{
		errno = EINVAL;
		return -1;		
	}

	/*Comincio ad aggiornare la matrice w*/
	for(j=0; j<_ncol; ++j)
	{
		if(servito[row_num][j] == 0)
		{
			switch(pw->plan->w[row_num][j])
			{
				case SHARK:
				{
					shark_rule2(pw, row_num, j, &k, &l);
					servito[k][l] = 1;
					shark_rule1(pw, row_num, j, &k, &l);
					servito[k][l] = 1;
					servito[row_num][j] = 1;
					break;
				}
				case FISH:
				{
					fish_rule4(pw, row_num, j, &k, &l);
					servito[k][l] = 1;
					fish_rule3(pw, row_num, j, &k, &l);
					servito[k][l] = 1;
					servito[row_num][j] = 1;
					break;
				}
				case WATER:
				{
					servito[row_num][j] = 1;
					break;
				}
			}
		}
	}
	return 0;
}
/** calcola un chronon aggiornando tutti i valori della simulazione e il pianeta
   \param pw puntatore al pianeta

   \return 0 se tutto e' andato bene
   \return -1 se si e' verificato un errore (setta errno)

 */
int update_wator (wator_t * pw){
	/* array rispettivamente di shark e fish usati per memorizzare le pos di quelli da muovere!
	 * con rispettive dimensioni
	 * NOTAZIONE : le righe sono i e j, mentre le colonne sono gli animali: ogni animale a due righe*/
	int *stm[2], ns=0;
	int *ftm[2], nf=0;
	int i,j, trash_i, trash_j;
	stm[0]=stm[1]=ftm[0]=ftm[1] = NULL;
	/* alloco e se c'è qualche errore rimuovo tutto. */
	if ( ! pw 
	|| ! ( stm[0] = malloc (sizeof(int)*pw->ns) )
	|| ! ( stm[1] = malloc (sizeof(int)*pw->ns) )
	|| ! ( ftm[0] = malloc (sizeof(int)*pw->nf) ) 
	|| ! ( ftm[1] = malloc (sizeof(int)*pw->nf) ) ) {
		if ( stm[0] ) free ( stm[0] );
		if ( ftm[0] ) free ( ftm[0] );
		if ( stm[1] ) free ( stm[1] );
		if ( ftm[1] ) free ( ftm[1] );
		errno = EFAULT;
		return -1;
	}
	/* la scansione degli animali da muovere avviene prima di muoverli per evitare di muovere due volte lo stesso animale*/
	for (i=0; i<pw->plan->nrow; i++)
		for (j=0; j<pw->plan->ncol; j++)
			switch ( pw->plan->w [i][j] ){
				default : break;
				case SHARK:
					if ( ns == pw->ns ) { /* ATTENZIONE STATO INCONSISTENTE !!! trovati più shark di quanti dovrebbe*/
						free ( stm[0] );
						free ( ftm[0] );
						free ( stm[1] );
						free ( ftm[1] );
						errno = EBADF;
						return -1;
					}
					/* memorizzo le celle */
					stm[0][ ns ] = i;
					stm[1][ ns++ ] = j;
					break;
				case FISH:
					if ( nf == pw->nf ) { /* ATTENZIONE STATO INCONSISTENTE !!! trovati più fish di quanti dovrebbe*/
						free ( stm[0] );
						free ( ftm[0] );
						free ( stm[1] );
						free ( ftm[1] );
						errno = EBADF;
						return -1;
					}
					ftm[0][ nf ] = i;
					ftm[1][ nf++ ] = j;
					break;				
			}
	/* Tutto è andato bene, procedo al movimento dei pezzi. 
	 * Il controllo degli errori delle future chiamate è inutile in quanto già controllato in questa*/
	 /* Applico regola 2. Sovrascrivo stm poichè non ho interesse nel sapere le posizioni da ora in poi */
	for (i = 0; i<ns; i++)
		shark_rule2( pw, stm[0][i], stm[1][i], &trash_i, &trash_j );
	 /* Applico regola 1 */
	for (i = 0; i<ns; i++)
		shark_rule1( pw, stm[0][i], stm[1][i], &trash_i, &trash_j );
	 /* Applico regola 4. Sovrascrivo stm poichè non ho interesse nel sapere le posizioni da ora in poi */
	for (i = 0; i<nf; i++)
		if ( pw->plan->w [ftm[0][i]][ftm[1][i]] == FISH ) 
			fish_rule4( pw, ftm[0][i], ftm[1][i], &trash_i, &trash_j );
	 /* Applico regola 3 */
	for (i = 0; i<nf; i++)
		if ( pw->plan->w [ftm[0][i]][ftm[1][i]] == FISH ) 
			fish_rule3( pw, ftm[0][i], ftm[1][i], &trash_i, &trash_j );
	/* è passato un chronon */
	free ( stm[0] );
	free ( ftm[0] );
	free ( stm[1] );
	free ( ftm[1] );
	return 0;
}
Exemplo n.º 4
0
/**
 * 
 * questo programma testa tutte le funzioni finora implementate
 * 
 * esegue un test alla volta passando il risultato alla funzione print_result
 *  
**/
int main ()
{
	planet_t *pl;
	wator_t *wt;
	cell_t **copia;
	cell_t c;
	int k,l,p,g,n;
	FILE *f1, *f2;
	
	mtrace();
	
	print_result ( cell_to_char(WATER) == 'W' , "cell_to_char(WATER)" );
	print_result ( cell_to_char(FISH) == 'F' , "cell_to_char(FISH)" );
	print_result ( cell_to_char(SHARK) == 'S' , "cell_to_char(SHARK)" );
	print_result ( char_to_cell('W') == WATER , "char_to_cell('W')" );
	print_result ( char_to_cell('F') == FISH , "char_to_cell('F')" );
	print_result ( char_to_cell('S') == SHARK , "char_to_cell('S')" );
	print_result ( char_to_cell('P') == -1 , "char_to_cell('P')" );
	print_result ( char_to_cell( cell_to_char (WATER) ) == WATER , "char_to_cell( cell_to_char (WATER) )" );
	print_result ( cell_to_char( char_to_cell ('F') ) == 'F' , "cell_to_char( char_to_cell ('F') )" );
	test_matrice_generica (10,40);
	test_matrice_generica (1000,40000);
	pl=new_planet (10,10);
	print_result (  ( pl!=NULL && pl->nrow==10 && pl->ncol==10 ) || errno==ENOMEM, "new_planet (10,10)" );
	free_planet (pl);
	
	contatore_pesci=contatore_squali=0;
	print_result ( controlla_formato_e_conta_squali_e_pesci ('W', ' ', 0, &c ) && c == WATER , "controlla_formato_e_conta_squali_e_pesci ('W', ' ', 0, &c )" );
	print_result ( controlla_formato_e_conta_squali_e_pesci ('F', ' ', 0, &c ) && c == FISH && contatore_pesci==1 , "controlla_formato_e_conta_squali_e_pesci ('F', ' ', 0, &c )" );
	print_result ( controlla_formato_e_conta_squali_e_pesci ('S', '\n', 1, &c ) && c == SHARK && contatore_squali==1 , "controlla_formato_e_conta_squali_e_pesci ('S', '\\n', 1, &c )" );
	print_result ( ! controlla_formato_e_conta_squali_e_pesci ('M', '\n', 1, &c ), "controlla_formato_e_conta_squali_e_pesci ('M', '\\n', 1, &c )" );
	print_result ( ! controlla_formato_e_conta_squali_e_pesci ('W', '\n', 0, &c ), "controlla_formato_e_conta_squali_e_pesci ('W', '\\n', 0, &c )" );
	print_result ( ! controlla_formato_e_conta_squali_e_pesci ('W', 'a', 0, &c ), "controlla_formato_e_conta_squali_e_pesci ('W', 'a', 0, &c )" );
	
	print_result ( test_load_store ("custom_test/planet1.dat", 1), "test_load_store(\"custom_test/planet1.dat\")" );
	print_result ( test_load_store ("custom_test/planet2.dat", 1), "test_load_store(\"custom_test/planet2.dat\")" );
	print_result ( test_load_store ("custom_test/planet3.dat", 0), "test_load_store(\"custom_test/planet3.dat\")" );
	print_result ( test_load_store ("custom_test/planet4.dat", 0), "test_load_store(\"custom_test/planet4.dat\")" );
	print_result ( test_load_store ("custom_test/planet5.dat", 0), "test_load_store(\"custom_test/planet5.dat\")" );
	
	wt = malloc ( sizeof(wator_t) );
	wt->plan = NULL;
	system ("rm -f wator.conf");
	print_result (  read_wator_conf (wt) == -1, "read_wator_conf without wator.conf"  );
	system ("cp custom_test/wator.conf.1 wator.conf");
	print_result (  read_wator_conf (wt) == 1 &&  wt->sd == 100  && wt->sb == 20  &&  wt->fb == 15 , "read_wator_conf wator.conf.1"  );
	system ("cp custom_test/wator.conf.2 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.2"  );
	system ("cp custom_test/wator.conf.3 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.3"  );
	system ("cp custom_test/wator.conf.4 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.4"  );
	system ("cp custom_test/wator.conf.5 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.5"  );
	system ("cp custom_test/wator.conf.6 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.6"  );
	system ("cp custom_test/wator.conf.7 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.7"  );
	system ("cp custom_test/wator.conf.8 wator.conf");
	print_result ( ! read_wator_conf(wt)  &&  errno == ERANGE , "read_wator_conf wator.conf.8"  );
	free_wator (wt);
	
	system ("cp custom_test/wator.conf.1 wator.conf");
	wt=new_wator ("custom_test/planet1.dat");
	print_result ( wt != NULL &&  wt->nf == 701  &&  wt->ns == 300  &&  wt->sd == 100  &&  wt->sb == 20  && wt->fb == 15  && wt->plan != NULL  &&  wt->plan->nrow == 200  &&  wt->plan->ncol == 130  &&  wt->plan->w[3][7] == SHARK , "new_wator");	
	
	k=7;
	l=6;
	print_result ( ricerca_cella (wt->plan, &k, &l, FISH) && k==7 && l==7 , "ricerca_cella (wt->pl, 7, 6, FISH)" ); 
	k=l=0;
	print_result ( ricerca_cella (wt->plan, &k, &l, SHARK) && k==199 && l==0 , "ricerca_cella (wt->pl, 0, 0, SHARK)" ); 
	k=1;
	l=3;
	print_result ( ! ricerca_cella (wt->plan, &k, &l, SHARK) && k==1 && l==3 , "ricerca_cella (wt->pl, 1, 3, SHARK)" ); 
	k=5;
	l=2;
	print_result ( ricerca_cella (wt->plan, &k, &l, FISH) && k==5 && ( l==3 || l== 1 ), "ricerca_cella (wt->pl, 5, 2, SHARK)" ); 
	k=20;
	l=25;
	print_result ( ricerca_cella (wt->plan, &k, &l, WATER) && ( (k==19 && l==25 ) ||  ( k== 20 && l== 24 ) ), "ricerca_cella (wt->pl, 20, 25, WATER)" ); 
	
	print_result ( shark_rule1 ( wt, 0, 2, &k, &l ) == -1 , "shark_rule_1 (0,2)" );
	print_result ( shark_rule1 ( wt, 1, 14, &k, &l ) == MOVE, "shark_rule_1 (1,15)" );
	print_result ( shark_rule1 ( wt, 7, 6, &k, &l ) == EAT && k==7 && l==7, "shark_rule_1 (7,7)" );
	
	//resetto il pianeta
	free_wator (wt);
	wt=new_wator ("custom_test/planet1.dat");
	
	n=10;
	wt->plan->btime[1][14] = 3;
	print_result ( ! riproduzione_generica (wt->plan, 1, 14, &k, &l, 4, &n ) && k==1 && l==14 && n==10, "riproduzione_generica (wt->plan, 1, 14) - nothing" );
	p=1;
	g=14;
	print_result ( riproduzione_generica (wt->plan, 1, 14, &k, &l, 4, &n ) && ricerca_cella (wt->plan, &p, &g, SHARK) && p==k && g==l && n==11, "riproduzione_generica (wt->plan, 1, 14) - born" );
	
	wt->plan->dtime[3][7] = wt->sd;
	print_result ( shark_rule2 ( wt, 3, 7, &k, &l ) == DEAD && wt->plan->w[3][7] == WATER, "shark_rule2 ( wt, 3, 7) - died" );
	print_result ( shark_rule2 ( wt, 7, 6, &k, &l ) == ALIVE && wt->plan->btime[7][6] == 1 , "shark_rule2 ( wt, 7, 6) - nothing" );
	wt->plan->btime[7][6] = wt->sb;
	p=7;
	g=6;
	print_result ( shark_rule2 ( wt, 7, 6, &k, &l ) == ALIVE && ricerca_cella ( wt->plan, &p, &g, SHARK ) && p==k && g==l , "shark_rule2 ( wt, 7, 6) - born" );
	
	//resetto il pianeta
	free_wator (wt);
	wt=new_wator ("custom_test/planet1.dat");
		
	p=0;
	g=12;
	print_result ( fish_rule3 ( wt, 0, 12, &k, &l ) == MOVE && wt->plan->w[0][12] == WATER && ricerca_cella (wt->plan,&p,&g,FISH) && p==k && g==l , "fish_rule_3 ( wt, 0, 12) - moved" );
	
	print_result ( ! fish_rule4 (wt, 5, 70, &k, &l ) && wt->plan->btime[5][70] == 1 , "fish_rule4 (wt, 5, 70) - nothing" );
	print_result ( conta_generico ( wt->plan, FISH ) == 701 , "conta_generico ( wt->plan, FISH )" );
	print_result ( conta_generico ( wt->plan, SHARK ) == 300 , "conta_generico ( wt->plan, SHARK )" );
	print_result ( conta_generico ( wt->plan, WATER ) == wt->plan->ncol*wt->plan->nrow - 701 - 300 , "conta_generico ( wt->plan, WATER )" );
	wt->plan->btime[5][70] = wt->fb;
	p=5;
	g=70;
	print_result ( ! fish_rule4 (wt, 5, 70, &k, &l ) && wt->plan->btime[5][70] == 0 && ricerca_cella (wt->plan, &p,&g,FISH) && p==k && g==l , "fish_rule4 (wt, 5, 70) - born" );
	
	//quello in più rispetto a 701 del test di prima è appena nato. Auguri!
	print_result ( fish_count ( wt->plan ) == wt->nf , "fish_count ( wt->plan ) == wt->nf");
	print_result ( shark_count ( wt->plan ) == wt->ns , "fish_count ( wt->plan ) == wt->ns");
			
	//copio il pianeta e lo scrivo sul file. Poi confronto i due file, per vedere se sono uguali
	copia = (cell_t**) alloca_matrice_generica ( wt->plan->nrow, wt->plan->ncol, sizeof (cell_t), sizeof (cell_t *) ); 
	copia_pianeta (copia, wt->plan->w, wt->plan->nrow, wt->plan->ncol);
	
	pl = malloc ( sizeof (planet_t) );
	pl->w = copia;
	pl->nrow = wt->plan->nrow;
	pl->ncol = wt->plan->ncol;
	pl->btime = NULL;
	pl->dtime = NULL;
	
	f1 = fopen ( "custom_test/result_wt.dat", "w" );
	f2 = fopen ( "custom_test/result_copia.dat", "w" );
	print_planet ( f1, wt->plan );
	print_planet ( f2, pl );
	fclose (f1);
	fclose (f2);
	
	print_result ( confronto_file ( f1,f2 ), "copia_pianeta ()" ); 
	
	free_wator (wt);
	free_planet (pl);
	
	wt = new_wator ("custom_test/planet1.dat");
		
	free_wator (wt);
	
	muntrace();
	
	return 0;
}