int main(){
		//person population[STARTSIZE];
		person* population=(person*)malloc(2*sizeof(person*));
		int populationsize;
		int populationsize_old;//to compare value before and after time_passes
		int continue_variable;
		int i,j;//incides for loop
		int time=0;

		populationsize=STARTSIZE;
		continue_variable=0;
		creation(population,&populationsize);
		printf("%s and %s were just created\n",population[0].name,population[1].name);
		
		//time passes
		while(time<20){
				populationsize_old=populationsize;
				draw_world(population,populationsize);
				time_passes(population,&populationsize);

				if(populationsize_old>populationsize){//if someone dies
						for(i=populationsize_old;i>0;i--){
								if( population[i].gender == 0 && ( i != populationsize )){
										//so if someone is dead and not the last person in the population-array
										for(j=i;j<populationsize;j++){
												population[j]=population[j+1];
										}
								}
								population= (person*) malloc(populationsize*sizeof(person));
								//dynamically allocates memory to fit the  size of the population	
						}
				}
					
				printf("populationsize is \d", populationsize);
				sleep(1);
				time++;
		}
		free(population);

		return 0;
}
Ejemplo n.º 2
0
/**
 * Perform actions or checks determined by the moment.
 * Also advances the time by one step.
 * @param runtime: scenario runtime information.
 */
static void
do_moment_and_advance(struct replay_runtime* runtime)
{
	struct replay_moment* mom;
	if(!runtime->now) {
		advance_moment(runtime);
		return;
	}
	log_info("testbound: do STEP %d %s", runtime->now->time_step, 
		repevt_string(runtime->now->evt_type));
	switch(runtime->now->evt_type) {
	case repevt_nothing:
		advance_moment(runtime);
		break;
	case repevt_front_query:
		/* advance moment before doing the step, so that the next
		   moment which may check some result of the mom step
		   can catch those results. */
		mom = runtime->now;
		advance_moment(runtime);
		fake_front_query(runtime, mom);
		break;
	case repevt_front_reply:
		if(runtime->answer_list) 
			log_err("testbound: There are unmatched answers.");
		fatal_exit("testbound: query answer not matched");
		break;
	case repevt_timeout:
		mom = runtime->now;
		advance_moment(runtime);
		expon_timeout_backoff(runtime);
		fake_pending_callback(runtime, mom, NETEVENT_TIMEOUT);
		break;
	case repevt_back_reply:
		mom = runtime->now;
		advance_moment(runtime);
		fake_pending_callback(runtime, mom, NETEVENT_NOERROR);
		break;
	case repevt_back_query:
		/* Back queries are matched when they are sent out. */
		log_err("No query matching the current moment was sent.");
		fatal_exit("testbound: back query not matched");
		break;
	case repevt_error:
		mom = runtime->now;
		advance_moment(runtime);
		fake_pending_callback(runtime, mom, NETEVENT_CLOSED);
		break;
	case repevt_time_passes:
		time_passes(runtime, runtime->now);
		advance_moment(runtime);
		break;
	case repevt_autotrust_check:
		autotrust_check(runtime, runtime->now);
		advance_moment(runtime);
		break;
	case repevt_assign:
		moment_assign(runtime, runtime->now);
		advance_moment(runtime);
		break;
	case repevt_traffic:
		advance_moment(runtime);
		break;
	case repevt_infra_rtt:
		do_infra_rtt(runtime);
		advance_moment(runtime);
		break;
	default:
		fatal_exit("testbound: unknown event type %d", 
			runtime->now->evt_type);
	}
}
int main(){
		//printf("at the beginning of main");
		person* firstperson;
		person* personlist;
		person* current;
		int intmap[HEIGHT][WIDTH];
		int clock=0;
		int i,j;
		srand(time(NULL));

		//create firstperson
		//printf("before first creation");
		//all names will be overwritten by getname() in createperson()
		firstperson=create_person(-110,10,' ',"God", 0,0);	
		create_person_after(firstperson,0,10,'E',"Eve",2,5);
		create_person_after(firstperson,0,10,'F',"empty",4,5);
		create_person_after(firstperson,0,10,'F',"empty",4,5);
		create_person_after(firstperson,0,10,'F',"empty",4,5);
		create_person_after(firstperson,0,1,'M',"empty",4,5);
		create_person_after(firstperson,0,1,'M',"empty",4,5);
		create_person_after(firstperson,0,1,'M',"empty",4,5);


		while(clock<TIME){
				
				//initialize intmap as empty
				for(i=0;i<HEIGHT;i++){
						for(j=0;j<WIDTH;j++){
								intmap[i][j]=0;
						}
				}
				personlist=firstperson;
				while(personlist){
						intmap[personlist->y][personlist->x]+=personlist->gender;
						//printf("value of intmap is %d\n",intmap[personlist->y][personlist->x]);
						if(intmap[personlist->y][personlist->x]==11&&personlist->fertility){
								//because women are after men in the linked-list this should ensure that only womeng get pregnant.
								//if a pregnant woman has sex, her child will be born sooner... i guess that's a feature then
								personlist->pregnancy+=1;
								printf("%s just got pregnant\n",personlist->name);
						}
						printf("%s has gender %d\n",personlist->name,personlist->gender);

						personlist=personlist->next;
				}
				
		//printf("intmap for starting point is %d", intmap[2][4]);

				printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
				draw_world(firstperson,intmap);
				//printf("before time passes\n");
				time_passes(firstperson);
				//printf("after time passes\n");
				sleep(SLEEP);
				//printf("after sleep\n");
				clock++;
				if(CONTROL!=0){
						printf("there is no one left alive i'm afraid\n you...won i guess?");
						break;
				}
				
				printf("the time is %d\n",clock);
		}




		personlist=firstperson->next;
		free(firstperson);
		while(personlist){
				printf("%s with gender=%d\n",personlist->name,personlist->gender);
				free(personlist);
				personlist=personlist->next;
		}
	
		return 0;
}