Ejemplo n.º 1
0
Food Food::parseElement( const QDomElement &element, bool *ok )
{
  if ( element.tagName() != "food" ) {
    qCritical() << "Expected 'food', got '" <<element.tagName() << "'.";
    if ( ok ) *ok = false;
    return Food();
  }

  Food result = Food();

  QDomNode n;
  for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    QDomElement e = n.toElement();
    if ( e.tagName() == "name" ) {
      result.setName( e.text() );
    }
    else if ( e.tagName() == "taste" ) {
      result.setTaste( e.text() );
    }
  }


  if ( ok ) *ok = true;
  return result;
}
Ejemplo n.º 2
0
LongFood::LongFood(int sizeX, int sizeY, int u)
	:Food(sizeX-3, sizeY, u)
{
	time = 0;
	value = 3;
	active = false;
	Food tmp[] = {Food(x,y), Food(x+unit, y), Food(x+2*unit, y), Food(x+3*unit,y)};
	elements.assign(tmp, tmp+4);
}
Ejemplo n.º 3
0
int main()
{
 
    char key;
 
    Print();
 
    system("cls");
 
    load();
 
    length=5;
 
    head.x=25;
 
    head.y=20;
 
    head.direction=RIGHT;
 
    Boarder();
 
    Food(); //to generate food coordinates initially
 
    life=3; //number of extra lives
 
    bend[0]=head;
 
    Move();   //initialing initial bend coordinate
 
    return 0;
 
}
Ejemplo n.º 4
0
/*
 * used to spawn a new piece of Food onto the screen
 * return: a new Food object that the snake can eat
 */
Food Window::create_food()
{
    std::random_device rd; // obtain a random number from hardware
    std::mt19937 eng(rd()); // seed the generator
    std::uniform_int_distribution<> distrx(2, max_x - 2); // define the range
    int x = distrx(eng);
    std::uniform_int_distribution<> distry(2, max_y - 2); // define the range
    int y = distry(eng);

    return Food(Pos(x, y));
}
Ejemplo n.º 5
0
	Gamesession(Board & terminal, int gamespeedi) : gamespeed(gamespeedi), gameover(false), command(0)
	{
		timeout(gamespeed);

		xstart = terminal.xmax/2;
		ystart = terminal.ymax/2;

		Ekans  = Snake(ystart,xstart,RIGHT);
		pidgey = Food(terminal);


	}
Ejemplo n.º 6
0
void initOccluder()
{
	// load MD2 model
	yoshi = MD2MReadMD2 ("yoshi-tris.md2", "yoshi.png");
	MD2MFindDimension  (yoshi, center, size);
	scale = max3 (size[0],size[1],size[2]);

	MD2MSetLoop(yoshi, GL_TRUE); 
	MD2MSetAnimation(yoshi, pose);  // 0: stand
	MD2MSetAnimationSpeed (yoshi, 10); 

	double h = 3.0; // height of foods

	shroom_model = glmReadOBJ("shroom.obj");
	glmUnitize(shroom_model);
	glmFacetNormals(shroom_model);
	glmVertexNormals(shroom_model, 90);

	Food::food_model = shroom_model;

	shroom.push_back(Food(Vec3(2,h,3)));
	shroom.push_back(Food(Vec3(0,h,0)));
	shroom.push_back(Food(Vec3(2,h,-3)));
}
Ejemplo n.º 7
0
int grab(Vec3 point)  // point: (x,0,z)
{
	double eps = 1.5;  // threshold for successful grabbing

	for(int i = 0 ; i < shroom.size() ; i++) {
		double distance = sqrt(pow((point[0] - shroom[i].pos[0]), 2) + pow((point[2] - shroom[i].pos[2]), 2));
		if(distance <= eps && shroom[i].status == 1) {
			Vec3 random_position = Vec3(rand() % 20 - 10, 3, rand() % 20 - 10);
			shroom.push_back(Food(random_position));
			cout << "random = " << random_position << endl;
			return i;
		}
	}
	
	return -1;
}
Ejemplo n.º 8
0
/****************蛇的移动********************
*
*算法需要改进
********************************************/
void SnakeMove(int x,int y)
{
	int i;
	//根据当前移动方向确定下一步的坐标
	
	if(GameOver(x,y))
	{
		GameState = 2;
		Pause = FALSE;
		return;
	}
	//判断是否吃到了食物
	if(SnakeEatFood(x,y))
	{
		for(i =SnakeLength;i>0;i--)
			Snake[i]= Snake[i-1];
		Snake[0].x = x;
		Snake[0].y = y;
		SnakeLength++;
		Food();
	}
	else 
	{
		for(i =SnakeLength-1;i>0;i--)
			Snake[i]= Snake[i-1];
		Snake[0].x = x;
		Snake[0].y = y;
	}
	if(SnakeLength>=20&&SnakeLength<40)
	{
		GameLevel = 2;
		GameSpeed = 100 ;
	}
	else if(SnakeLength>=40&&SnakeLength<80)
	{
		GameLevel = 3;
		GameSpeed = 80;
	}
	else if(SnakeLength>=80)
	{
		GameLevel = 4;
		GameSpeed = 50;
	}
}
Ejemplo n.º 9
0
void Map::flash() {
    sort(ants.begin(), ants.end(), cmp);
    for (int i = 0; i < ants.size(); ) {
        int same = 1;
        while (i + same < ants.size() && ants[i] == ants[i + same])
            same++;
        for (int j = 1; j < same; j++) {
            merge(ants[i].foods, ants[i + j].foods);
        }
        for (int j = 1; j < same; j++) {
            ants[i + j].foods = ants[i].foods;
        }
        i += same;
    }
    int food[x][y];
    for (int i = 0; i < x; i++)
        for (int j = 0; j < y; j++)
            food[i][j] = 0;
    for (int i = 0; i < foods.size(); i++)
        food[foods[i].x][foods[i].y] = foods[i].food;
    for (int i = 0; i < ants.size(); i++) {
        Ant &ant = ants[i];
        if (food[ant.x][ant.y] != 0) {
            merge(ant.foods, std::vector<Food>(1, Food(ant.x, ant.y, food[ant.x][ant.y])));
            if (ant.food == 0) {
                ant.food++;
            }
        }
        if (ant == home) {
            home.food += ant.food;
            ant.food = 0;
        }
        ant.action(*this);
    }
    while (home.food >= antValue) {
        home.food -= antValue;
        ants.push_back(Ant(home.x, home.y));
    }
}
Ejemplo n.º 10
0
void GameEscape::createFood()
{
    foods.push_back(Food(this, getFreeSpace(1), 1, FOOD_RANDMOVE));
}
Ejemplo n.º 11
0
void Move()
{
    int a,i;
 
    do{
 
        Food();
        fflush(stdin);
 
        len=0;
 
        for(i=0;i<30;i++)
 
        {
 
            body[i].x=0;
 
            body[i].y=0;
 
            if(i==length)
 
            break;
 
        }
 
        Delay(length);
 
        Boarder();
 
        if(head.direction==RIGHT)
 
            Right();
 
        else if(head.direction==LEFT)
 
            Left();
 
        else if(head.direction==DOWN)
 
            Down();
 
        else if(head.direction==UP)
 
            Up();
 
        ExitGame();
 
    }while(!kbhit());
 
    a=getch();
 
    if(a==27)
 
    {
 
        system("cls");
 
        exit(0);
 
    }
    key=getch();
 
    if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN))
 
    {
 
        bend_no++;
 
        bend[bend_no]=head;
 
        head.direction=key;
 
        if(key==UP)
 
            head.y--;
 
        if(key==DOWN)
 
            head.y++;
 
        if(key==RIGHT)
 
            head.x++;
 
        if(key==LEFT)
 
            head.x--;
 
        Move();
 
    }
 
    else if(key==27)
 
    {
 
        system("cls");
 
        exit(0);
 
    }
 
    else
 
    {
 
        printf("\a");
 
        Move();
 
    }
}
Ejemplo n.º 12
0
Archivo: main.cpp Proyecto: alobo/ev
int main() {

    sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Main");

    // Setup test creature
    Creature creature = Creature();
    creature.position[0] = 100;
    creature.position[1] = 100;
    // creature.velocity[0] = 20;
    // creature.velocity[1] = 20;
    creature.setRotation(50);
    env.addObject(&creature);

    // Setup creatures
    for (int i = 0; i < NUM_CREATURES; ++i) {
        creatures[i] = Creature();
        creatures[i].position[0] = 50 * (i + 1);
        creatures[i].position[1] = 50 * (i + 1);
        creatures[i].setRotation(0);
        env.addObject(&creatures[i]);
    }

    // Setup food
    for (int i = 0; i < NUM_FOOD; ++i) {
        Food f = Food();
        f.reset(WIDTH, HEIGHT);
        food.push_back(f);
    }

    sf::Clock frame_clock;
    sf::Clock gen_clock;
    int frame_count = 0;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {

                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::KeyPressed:
                    if (event.key.code == sf::Keyboard::W) {
                        creature.moveForward();
                    } else if (event.key.code == sf::Keyboard::S) {
                        printf("%s\n", "S PRESSED");
                    } else if (event.key.code == sf::Keyboard::A) {
                        creature.setRotation(creature.getRotation() + 5);
                    } else if (event.key.code == sf::Keyboard::D) {
                        creature.setRotation(creature.getRotation() - 5);
                    } else if (event.key.code == sf::Keyboard::P) {
                        launchConsole();
                        frame_clock.restart();
                    }
                    break;

                case sf::Event::MouseButtonPressed:
                    if (event.mouseButton.button == sf::Mouse::Right) {
                        sf::Vector2f position = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));
                    }
                    break;

                default:
                    break;
            }
        }

        window.clear();

        for (int i = 0; i < NUM_FOOD; ++i) {
            if (!food[i].isConsumed()) window.draw(food[i]);
        }

        // Debug creature
        creature.draw(&window);
        for (int i = 0; i < NUM_FOOD; ++i) {
            if (creature.isPointInFOV(food[i].getPosition())) {
                // printf("%f\n", creature.distanceToPoint(food[i].getPosition()));
            }
        }

        for (int i = 0; i < NUM_CREATURES; ++i) {
            creatures[i].process(&food);
            creatures[i].draw(&window);
        }

        // Handle generations
        if (gen_clock.getElapsedTime().asSeconds() > GENERATION_LENGTH_SECONDS) {
            std::cout << "Generation: " << ++gen_count << std::endl;
            writeOutData();
            // Reset food
            for (int i = 0; i < NUM_FOOD; ++i) {
                food[i].reset(WIDTH, HEIGHT);
            }

            // Sort by energy level
            std::sort(creatures, creatures + NUM_CREATURES, compareCreatures);

            // Select and mutate the top 50% of creatures
            Mutator mutator = Mutator();
            for (int i = 0; i < NUM_CREATURES/2; ++i) {
                printf("Creature %d  %f\n", i, creatures[i].getEnergy());
                mutator.setTarget(&creatures[i]);
                creatures[i].resetEnergy();
                creatures[i].position[0] = WIDTH / 2;
                creatures[i].position[1] = HEIGHT / 2;

                mutator.mutate(&creatures[NUM_CREATURES - 1 - i]);
                creatures[NUM_CREATURES - 1 - i].resetEnergy();
                creatures[NUM_CREATURES - 1 - i].position[0] = WIDTH / 2;
                creatures[NUM_CREATURES - 1 - i].position[1] = HEIGHT / 2;
            }

            gen_clock.restart();
        }

        window.display();

        sf::Time elapsed = frame_clock.restart();
        env.step(elapsed.asSeconds());
    }

    return 0;
}
Ejemplo n.º 13
0
//Alters the map to the users specifications
void Map::AlterMap (char W, char S, char F, char G, char C, char X, char L, char E, char P)
{
	if (Washrooms(W))
	{
		floorOne[3][3] = 'W'; floorOne[2][11] = 'W'; floorOne[13][45] = 'W'; floorOne[13][51] = 'W';
		floorTwo[13][4] = 'W'; floorTwo[13][12] = 'W'; floorTwo[2][50] = 'W'; floorTwo[5][50] = 'W';
	} else
	{
		floorOne[3][3] = ' '; floorOne[2][11] = ' '; floorOne[13][45] = ' '; floorOne[13][51] = ' ';
		floorTwo[13][4] = ' '; floorTwo[13][12] = ' '; floorTwo[2][50] = ' '; floorTwo[5][50] = ' ';
	}

	if (Store(S))
	{
		floorOne[11][4] = 'S';
		floorTwo[8][3] = 'S';
	} else
	{
		floorOne[11][4] = ' ';
		floorTwo[8][3] = ' ';
	}

	if (Food(F))
	{
		floorTwo[2][4] = 'F'; floorTwo[2][12] = 'F';
	} else
	{
		floorTwo[2][4] = ' '; floorTwo[2][12] = ' ';
	}

	if (Gate(G))
	{
		floorTwo[1][27] = 'G'; floorTwo[1][40] = 'G';
	} else
	{
		floorTwo[1][27] = ' '; floorTwo[1][40] = ' ';
	}

	if (Customs(C))
	{
		floorTwo[9][31] = 'C';
	} else
	{
		floorTwo[9][31] = ' ';
	}

	if (CheckIn(X))
	{
		floorOne[2][38] = 'X';
	} else
	{
		floorOne[2][38] = ' ';
	}

	if (Luggage(L))
	{
		floorOne[2][38] = 'L';
	} else
	{
		floorOne[2][38] = ' ';
	}

	if (Entrance(E))
	{
		floorOne[10][52] = 'E';
	} else
	{
		floorOne[10][52] = ' ';
	}

	if (PickUp(P))
	{
		floorOne[11][28] = 'P'; floorOne[11][37] = 'P';
	} else
	{
		floorOne[11][28] = ' '; floorTwo[11][37] = ' ';
	}
}
Ejemplo n.º 14
0
void LongFood::update()
{
	this->Food::update();
	Food tmp[] = {Food(x,y), Food(x+unit, y), Food(x+2*unit, y), Food(x+3*unit,y)};
	elements.assign(tmp, tmp+4);
}
Ejemplo n.º 15
0
int main()
{
	
	// initialise
	const unsigned int kGridSize = 25;

	const unsigned int kPopulationMax = 30;
	const unsigned int kFoodMax = 50;

	const unsigned int kReproduceEnergy = 120;

	const unsigned int kCellStartEnergy = 100;
	const unsigned int kMaxFoodEnergy = 100;

	Dish petri(kGridSize);

	unsigned int total_population = 15;
	unsigned int total_food = 30;

	std::vector<Cell> population;

	for (unsigned int i = 0; i < total_population; i++) {
		population.push_back(Cell(kGridSize, kCellStartEnergy));
	}

	std::list<Food> supply;

	for (unsigned int i = 0; i < total_food; i++) {
		supply.push_back(Food(kGridSize, kMaxFoodEnergy));
	}

	bool exit = false;
	unsigned int w = 1;

	while (w < 50) {

		/*
		// Process step
		*/
		for (auto cell = population.begin(); cell != population.end(); ++cell) {
			if (cell->alive() == true) {
				// move
				cell->move(kGridSize);

				for (auto food = supply.begin(); food != supply.end(); ++food) {
					if (cell->location() == food->location()) {
						total_food = supply.size();
						cell->consume_food(*food);
						food = supply.erase(food);
					}
				}

				for (auto target = population.begin(); target != population.end(); ++target) {
					switch (can_consume_cell(*cell, *target))
					{
					case false:
						continue;
					case true:
						target = population.erase(target);
						break;
					default:
						break;
					} 
				}
				

				// reproduce
				if ((total_population < kPopulationMax) && (cell->reproduce())) {
					population.push_back(Cell(kGridSize, cell->energy() / 2));
					cell->set_energy(cell->energy() / 2);

					total_population = population.size();
				}



				// death
				if (cell->energy() <= 0) {
					cell->set_alive(false);
				}
			}
		}

		if (total_food < kFoodMax) {
			supply.push_back(Food(kGridSize, kMaxFoodEnergy));

			total_food = supply.size();
		}

		/*
		// Input step
		*/

		// poll user input

		/*
		// Display step
		*/

		// display grid

		// TODO: Optimise this horrible loop
		for (unsigned int i = 0; i < kGridSize; i++) {
			for (unsigned int j = 0; j < kGridSize; j++) { 
				bool occupied = false;
				position display_location;
				display_location.x = i;
				display_location.y = j;
				for (Cell cell : population) {
					if (cell.location() == display_location) {
						if (cell.alive() == true) {
							std::cout << cell.energy();
						}
						else {
							std::cout << "-";
						}
						occupied = true;
					}
				}
				for (Food food : supply) {
					if (food.location() == display_location) {
						std::cout << ".";
						occupied = true;
					}
				}
				if (occupied == false) {
					std::cout << " ";
				}
				else {
					occupied = false;
				}

			}
			std::cout << "\n";
		}
		
		for (unsigned int i = 0; i < kGridSize; i++) {
			std::cout << "=";
		}
		std::cout << "\n" << total_population;

		std::cout.flush();
		Sleep(1500);

		w++;
	}

	population.clear();
	supply.clear();
	
	return 0;
}