示例#1
0
FightAction::MoveStatus AntilopeFight::defend(FightContext &ctx) {
    if (ctx.attacker().hasFlag(friendlyFlag())) {
        breed(ctx);
        return DenyMove;
    }
    if (ctx.defender().strength() < ctx.attacker().strength()) {
        if (rand() % 2 == 0) {
            ctx.defender().kill();
            return AllowMove;
        } else {
            MoveToFree mv;
            TurnContext tctx = ctx.defendersTurn();
            mv.makeTurn(tctx);
            return DenyMove;
            if (!mv.success()) {
                ctx.defender().kill();
                return AllowMove;
            }
        }
    } else if (ctx.defender().strength() > ctx.attacker().strength()) {
        ctx.attacker().kill();
        return DenyMove;
    } else if (rand() % 2 == 0) {
        ctx.defender().kill();
        return AllowMove;
    } else
        ctx.attacker().kill();
    return DenyMove;
}
示例#2
0
// MAIN
long main(void) {
	init();
	
	rank();
	sort(0, NUMCANDIDATES-1);
	while (ranks[0] != TARGETLEN-1) { // Loop till each character in the target sentence is correct
		breed();
		mutate();
		genCount++;
		
		rank();
		sort(0, NUMCANDIDATES-1);
		print();
	}
	printf("Sentence found after %d generations: %s\n", genCount, sentences[0]);
}
示例#3
0
void GenePool::nextGeneration (void)
{
    int *scores = new int[pool_.size()];
    int maxScore = 0;
    EventPool breedingPool;
    int eventCnt = 0;

    /* go through gene pool, assign scores to each item */
    for (EventPool::iterator i = pool_.begin(); i != pool_.end(); ++i)
    {
        scores[eventCnt] = evaluate(*i);
        
        if (scores[eventCnt] > maxScore)
            maxScore = scores[eventCnt];

        eventCnt++;
    }

    if (maxScore == 0)
    {
        generation_++;
        randomize(pool_.size());
        delete scores;

        return;
    }

    /* go through the pool again, and check if the corresponding item in the
       pool has a score ratio that is acceptable to our selection rate; if so,
       then add it to our breedingPool */
    eventCnt = 0;
    for (EventPool::iterator i = pool_.begin(); i != pool_.end(); ++i)
    {
        if ((float)(scores[eventCnt]/maxScore) >= selection_)
        {
            breedingPool.push_back(*i);
        }

        eventCnt++;
    }

    breed(breedingPool);

    delete scores;
}
示例#4
0
void SmallWorld::update() {
	
	if (!finished) {
		
		if (bugs[0].isFinished()) {
			
			evaluate();
			
			bug1 = bugs[0];
			bug2 = bugs[1];
			
			// chech if termination is satisfied
			if (termination()) {
				
				cout << "end bugs life simulation up." << endl;
				finished = true;
				
			} else {
				// do crossover or mutation
				breed();
				
				// reproduction
				initBox2d();
			}
			
		} else {
			
			box2d.update();
			
			// add force every 0.5s
			if (steps > 0 && steps % 15 == 0) {
				
				for (int i = 0; i < bugs.size(); i++) {
					bugs[i].update();
				}
			}
			
			steps++;
		}
	}
}
/*********************************************************************
** Member Function: move
** Description: moves the Doodlebug based on a random direction given
** by rand, also calls the starve and breed functions based on the
** successful movement that is completed.
*********************************************************************/
void Doodlebug::move() {	// virtual
    int movement = rand() % 4 + 1;	/* limits movement to 4 directions
    ** moveNext is set to true initially by the constructor
    ** moveNext is checked by the grid updateLocation to execute this */
    xPrevious = xCurrent;
    yPrevious = yCurrent;
    starveSteps += 1;

    switch (movement) {
    	case 1: if (yCurrent - 1 == -1 || yCurrent - 1 == yMax) {
				moveNext = false;
				starve(gridPtr);
			}
			else if (yCurrent - 1 > -1 || yCurrent - 1 < yMax) {
    			yCurrent -= 1;	/* Up */
    			moveNext = false;
    			count += 1;
    			breed();
    			starve(gridPtr);
			}
			else
				std::cout << "Up move error, else tripped" << std::endl;
    		break;
		
		case 2: if (xCurrent + 1 == -1 || xCurrent + 1 == xMax) {
				moveNext = false;
				starve(gridPtr);
			}
			else if (xCurrent + 1 > -1 || xCurrent + 1 < xMax) {
    			xCurrent += 1;	/* Right */
    			moveNext = false;
    			count += 1;
    			breed();
    			starve(gridPtr);
			}
			else
				std::cout << "Right move error, else tripped" << std::endl;
    		break;

		case 3: if (yCurrent + 1 == -1 || yCurrent + 1 == yMax) {
				moveNext = false;
				starve(gridPtr);
			}
			else if (yCurrent + 1 > -1 || yCurrent + 1 < yMax) {
    			yCurrent += 1;	/* Down */
    			moveNext = false;
    			count += 1;
    			breed();
    			starve(gridPtr);
			}
			else
				std::cout << "Down move error, else tripped" << std::endl;
    		break;

		case 4: if (xCurrent - 1 == -1 || xCurrent - 1 == xMax) {
				moveNext = false;
				starve(gridPtr);
			}
			else if (xCurrent - 1 > -1 || xCurrent - 1 < xMax) {
    			xCurrent -= 1;	/* Left */
    			moveNext = false;
    			count += 1;
    			breed();
    			starve(gridPtr);
			}
			else
				std::cout << "Left move error, else tripped" << std::endl;
    		break;
    }
}
示例#6
0
文件: Wator.c 项目: Paddyd19/A-Team
void moveFish( short x, short y){
	// Check n,e,s,w
	short newElement = 0;
	short randomPath = 0;

	randomPath = rand() % 4;
	switch(randomPath)
	{
		// North
		case(0):
		if( map[x][wrapAround( y - 1 )].m_shark == 0 && map[x][wrapAround( y - 1 )].m_fish == 0)
		{
			newElement = wrapAround( y - 1 );
			// Copy the Shark to the new position.
			map[x][newElement].m_fish = map[x][y].m_fish;
			// Set the old Shark position to null
			map[x][y].m_fish = 0;		
		
			map[x][newElement].m_fish->m_breed -= 1;
			if( map[x][newElement].m_fish->m_breed <= 0)
			{
				breed( 'f' , x, y);
				map[x][newElement].m_fish->m_breed = maxFishBreedTime;
			}		
			// Mark the tile as updated for this turn.
			map[x][newElement].m_updated = true;

		}
		break;
		case(1):
		if( map[ wrapAround( x + 1 ) ][ y ].m_shark == 0 && map[ wrapAround( x + 1 ) ][ y ].m_fish == 0)
		{
			newElement = wrapAround( x + 1 );
			// Copy the Shark to the new position.
			map[newElement][ y].m_fish = map[x][y].m_fish;
			// Set the old Shark position to null
			map[x][y].m_fish = 0;

			map[newElement][ y].m_fish->m_breed -= 1;
			if( map[newElement][ y].m_fish->m_breed <= 0)
			{
				breed( 'f' , x, y);
				map[newElement][ y].m_fish->m_breed = maxFishBreedTime;
			}		
			// Mark the tile as updated for this turn.
			map[newElement][ y].m_updated = true;
		}
		break;
		case(2):
		if( map[ x ][ wrapAround( y + 1 ) ].m_shark == 0 && map[ x ][ wrapAround( y + 1 ) ].m_fish == 0)
		{
			newElement = wrapAround( y + 1 );
			// Copy the Shark to the new position.
			map[x][newElement].m_fish = map[x][y].m_fish;
			// Set the old Shark position to null
			map[x][y].m_fish = 0;
			map[x][newElement].m_fish->m_breed -= 1;
			if( map[x][newElement].m_fish->m_breed <= 0)
			{
				breed( 'f' , x, y);
				map[x][newElement].m_fish->m_breed = maxFishBreedTime;
			}
			// Mark the tile as updated for this turn.
			map[x][newElement].m_updated = true;
		}
		break;
		case(3):
		if( map[ wrapAround( x -1 ) ][ y ].m_shark == 0 && map[ wrapAround( x -1 ) ][ y ].m_fish== 0)
		{
			newElement = wrapAround( x - 1 );
			// Copy the Shark to the new position.
			map[newElement][ y].m_fish = map[x][y].m_fish;
			// Set the old Shark position to null
			map[x][y].m_fish = 0;

			map[newElement][ y].m_fish->m_breed -= 1;
			if( map[newElement][ y].m_fish->m_breed <= 0)
			{
				breed( 'f' , x, y);
				map[newElement][ y].m_fish->m_breed = maxFishBreedTime;
			}
			// Mark the tile as updated for this turn.
			map[newElement][ y].m_updated = true;
		}
		break;
	}

	
}
示例#7
0
文件: Wator.c 项目: Paddyd19/A-Team
bool sharkHunt( short x, short y ){
	bool ate = false;

	// Check n,e,s,w
	short newElement = 0;
	short randomPath = 0;

	randomPath = rand() % 4;
	switch(randomPath)
	{
			// North
			case(0):
			if( map[x][wrapAround( y - 1 )].m_fish != 0 )
			{
				newElement = wrapAround( y - 1 );
				// Copy the Shark to the new position.
				map[x][newElement].m_shark = map[x][y].m_shark;
				// Set the old Shark position to null
				map[x][y].m_shark = 0;
				// Set the old dead fish to 0
				map[x][newElement].m_fish = 0;
				// Update the sharks starve time as he just ate.
				map[x][newElement].m_shark->m_starve = sharkStarveTime;
				map[x][newElement].m_shark->m_breed -= 1;
				if( map[x][newElement].m_shark->m_breed <= 0)
				{
					breed( 's' , x, y);
					map[x][newElement].m_shark->m_breed = maxSharkBreedTime;
				}		
				// Mark the tile as updated for this turn.
				map[x][newElement].m_updated = true;	
				ate = true;						
			}		
			break;
			case(1):	
			if( map[ wrapAround( x + 1 ) ][ y ].m_fish != 0 )
			{
				newElement = wrapAround( x + 1 );
				// Copy the Shark to the new position.
				map[newElement][y].m_shark = map[x][y].m_shark;
				// Set the old Shark position to null
				map[x][y].m_shark = 0;
				// Set the old dead fish to 0
				map[newElement][y].m_fish = 0;
				// Update the sharks starve time as he just ate.
				map[newElement][y].m_shark->m_starve = sharkStarveTime;
				map[newElement][y].m_shark->m_breed -= 1;
				if( map[newElement][y].m_shark->m_breed <= 0)
				{
					breed( 's' , x, y);
					map[newElement][y].m_shark->m_breed = maxSharkBreedTime;
				}	
				// Mark the tile as updated for this turn.
				map[newElement][y].m_updated = true;
				ate = true;
			}	
			break;
			case(2):		
			if( map[ x ][ wrapAround( y + 1 ) ].m_fish != 0 )
			{
				newElement = wrapAround( y + 1 );
				// Copy the Shark to the new position.
				map[x][newElement].m_shark = map[x][y].m_shark;
				// Set the old Shark position to null
				//delete ( map[x][y].m_shark  );
				map[x][y].m_shark = 0;
				// Set the old dead fish to 0
				map[x][newElement].m_fish = 0;
				// Update the sharks starve time as he just ate.
				map[x][newElement].m_shark->m_starve = sharkStarveTime;
				map[x][newElement].m_shark->m_breed -= 1;
				if( map[x][newElement].m_shark->m_breed <= 0)
				{
					breed( 's' , x, y);
					map[x][newElement].m_shark->m_breed = maxSharkBreedTime;
				}	
				// Mark the tile as updated for this turn.
				map[x][newElement].m_updated = true;
				ate = true;
			}
			break;
			case(3):
			if( map[ wrapAround( x -1 ) ][ y ].m_fish != 0 )
			{
				newElement = wrapAround( x - 1 );
				// Copy the Shark to the new position.
				map[newElement][y].m_shark = map[x][y].m_shark;
				// Set the old Shark position to null
				//delete ( map[x][y].m_shark  );
				map[x][y].m_shark = 0;
				// Set the old dead fish to 0
				map[newElement][y].m_fish = 0;
				// Update the sharks starve time as he just ate.
				map[newElement][y].m_shark->m_starve = sharkStarveTime;
				map[newElement][y].m_shark->m_breed -= 1;
				if( map[newElement][y].m_shark->m_breed <= 0)
				{
					breed( 's' , x, y);
					map[newElement][y].m_shark->m_breed = maxSharkBreedTime;
				}	
				// Mark the tile as updated for this turn.
				map[newElement][y].m_updated = true;	
				ate = true;			
			}
			break;
			//else
			//{
			//	ate = false;
			//}
	}
		
		

	return ate;

}
示例#8
0
文件: main.cpp 项目: Mjiig/LD24
int main()
{
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue=NULL;
	ALLEGRO_TIMER *timer=NULL;
	ALLEGRO_FONT *font=NULL;
	bool keys[4]={false, false, false, false};
	bool redraw;
	enum tile map[50][30];
	struct player player;
	struct enemy enemies[100];
	int next;
	int selected_enemy=100;
	int level=1;
	int best=1;

	
	player.power=5;
	player.health=30;
	player.max_health=30;
	player.speed=2;
	player.x=0;
	player.y=0;
	player.turns_missed=0;
	player.kills=0;
	player.upgrades=4;

	if(!init(&display, &event_queue, &timer))
	{
		return 1;
	}

	init_map(map);

	init_enemies(enemies, map, 5);

	font=al_load_font("Xolonium-Regular.otf", 30, 0);

	while(42)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			redraw=true;
			next=select_mover(player, enemies);
			if(next==100)
			{
				if(move_player(&player, keys, map, enemies))
				{
					player.turns_missed=0;
					inc_turns(&player, enemies);
				}
			}
			else
			{
				int ret_x, ret_y;
				search(enemies[next].x, enemies[next].y, map, player.x, player.y, &ret_x, &ret_y);
				if(!is_player(ret_x, ret_y, player) && is_enemy(ret_x, ret_y, enemies)==100)
				{
					enemies[next].x=ret_x;
					enemies[next].y=ret_y;
				}
				if(is_player(ret_x, ret_y, player))
				{
					fight(&player, enemies, next);
				}
				enemies[next].turns_missed=0;
				inc_turns(&player, enemies);
				if(!(rand()%(150/level)))
				{
					breed(enemies, map);
				}
			}

			if(enemies[selected_enemy].health<=0)
			{
				selected_enemy=100;
			}

			if(all_enemies_dead(enemies))
			{
				level++;
				init_map(map);
				init_enemies(enemies, map, level*5);
				player.x=0;
				player.y=0;
				if(level>best)
				{
					best=level;
				}
			}

			if(player.health<=0)
			{
				level=1;
				init_map(map);
				init_enemies(enemies, map, level*5);
				player.power=5;
				player.health=30;
				player.max_health=30;
				player.speed=2;
				player.x=0;
				player.y=0;
				player.turns_missed=0;
				player.kills=0;
				player.upgrades=0;
			}
		}
		else if(ev.type==ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			break;
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					keys[KEY_UP]=true;
					break;
				case ALLEGRO_KEY_DOWN:
					keys[KEY_DOWN]=true;
					break;
				case ALLEGRO_KEY_RIGHT:
					keys[KEY_RIGHT]=true;
					break;
				case ALLEGRO_KEY_LEFT:
					keys[KEY_LEFT]=true;
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					keys[KEY_UP]=false;
					break;
				case ALLEGRO_KEY_DOWN:
					keys[KEY_DOWN]=false;
					break;
				case ALLEGRO_KEY_RIGHT:
					keys[KEY_RIGHT]=false;
					break;
				case ALLEGRO_KEY_LEFT:
					keys[KEY_LEFT]=false;
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
		{
			int old_selected=selected_enemy;

			selected_enemy=is_enemy(ev.mouse.x/18, ev.mouse.y/18, enemies);

			if(selected_enemy==100)
				selected_enemy=old_selected;

			if(ev.mouse.x>900 && player.kills >= (player.upgrades * player.upgrades)/10)
			{
				bool change=false;
				if(ev.mouse.y>10 && ev.mouse.y<40)
				{
					player.max_health+=5;
					player.health=player.max_health;
					change=true;
				}
				else if(ev.mouse.y>50 && ev.mouse.y<80)
				{
					player.power+=2;
					change=true;
				}
				else if(ev.mouse.y>90 && ev.mouse.y<120)
				{
					player.speed+=1;
					change=true;
				}
				
				if(change)
				{
					player.upgrades+=1;
				}
			}
		}

		if(redraw && al_is_event_queue_empty(event_queue))
		{
			redraw=false;
			al_clear_to_color(al_map_rgb(255, 255, 255));
			draw_map(map);
			draw_enemies(enemies);
			al_draw_bitmap(player_img, player.x*18, player.y*18, 0);
			al_draw_textf(font, al_map_rgb(0, 255, 0), 950, 10, ALLEGRO_ALIGN_CENTRE, "%d", player.health);
			al_draw_textf(font, al_map_rgb(0, 255, 0), 950, 50, ALLEGRO_ALIGN_CENTRE, "%d", player.power);
			al_draw_textf(font, al_map_rgb(0, 255, 0), 950, 90, ALLEGRO_ALIGN_CENTRE, "%d", player.speed);
			al_draw_textf(font, al_map_rgb(0, 0, 255), 950, 150, ALLEGRO_ALIGN_CENTRE, "%d", player.kills);
			al_draw_textf(font, al_map_rgb(0, 0, 255), 950, 190, ALLEGRO_ALIGN_CENTRE, "%d", (player.upgrades*player.upgrades)/10);
			if(selected_enemy!=100)
			{
				al_draw_textf(font, al_map_rgb(255, 0, 0), 950, 250, ALLEGRO_ALIGN_CENTRE, "%d", enemies[selected_enemy].health);
				al_draw_textf(font, al_map_rgb(255, 0, 0), 950, 290, ALLEGRO_ALIGN_CENTRE, "%d", enemies[selected_enemy].power);
				al_draw_textf(font, al_map_rgb(255, 0, 0), 950, 330, ALLEGRO_ALIGN_CENTRE, "%d", enemies[selected_enemy].speed);
			}
			al_draw_textf(font, al_map_rgb(0, 0, 0), 950, 390, ALLEGRO_ALIGN_CENTRE, "%d", level);
			al_draw_textf(font, al_map_rgb(0, 0, 0), 950, 430, ALLEGRO_ALIGN_CENTRE, "%d", best);
			al_flip_display();
		}
	}

	return 0;
}