Skip to content

Instantly share code, notes, and snippets.

@phibo23
Last active June 3, 2019 13:45
Show Gist options
  • Save phibo23/392b4dad1f126b28bedfe82ebf7ab6a1 to your computer and use it in GitHub Desktop.
Save phibo23/392b4dad1f126b28bedfe82ebf7ab6a1 to your computer and use it in GitHub Desktop.
Schlangenprogrammiernacht GPN19
// You can use the entire C/C++ standard library, just add the relevant
// #includes. We recommend math.h ;-)
#include <algorithm>
#include <string>
#include "usercode.h"
/*
* This is your bot's startup function. Here you can set your snake's colors,
* set up persistent variables, etc.
*/
bool init(Api *api)
{
// remove the default color
api->clearColors();
// - .... . --. .- -- .
api->addColor(255, 255, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
api->addColor(255, 255, 0);
api->addColor(0, 0, 0);
api->addColor(0, 0, 0);
// indicate successful startup. If anything goes wrong,
// return false and we'll clean you up.
return true;
}
/*
* This function will be called by the framework on every step. Here you decide
* where to move next!
*
* Use the provided Api object to interact with the world and make sure you set
* the following outputs:
*
* - api->angle: Set your relative movement angle
* - api->boost: Set this to true to move faster, but you will loose mass.
*
* The Api object also provides information about the world around you. See the
* documentation for more details.
*/
bool step(Api *api)
{
const IpcSelfInfo *me = api->getSelfInfo();
bool enemyDetected = false;
ipc_real_t evadeAngle = 0.0;
// check for other snakes
for(size_t i = 0; i < api->getSegmentCount(); i++) {
const IpcSegmentInfo &seg = api->getSegments()[i];
if(!seg.is_self && seg.dist - seg.r < me->segment_radius + me->sight_radius * 0.20) {
// you can send log messages to your browser or any other viewer with the
// appropriate Viewer Key.
//api->log(std::to_string(seg.dist).c_str());
enemyDetected = true;
evadeAngle = -1.0 * seg.dir;
break;
}
}
ipc_real_t foodAngle = 0.0;
//ipc_real_t foodValue = 0.0;
size_t maxFoodIndex = 100;
// check for food
//api->log(std::to_string(api->getFoodCount()).c_str());
for (size_t i = 0; i < std::min(api->getFoodCount(), maxFoodIndex); i++) {
const IpcFoodInfo &food = api->getFood()[i];
//foodValue += food.val;
if (food.dist > me->consume_radius) {
//api->log(std::to_string(food.dist).c_str());
foodAngle += food.dir * (food.val / 10.0) * (me->consume_radius / food.dist);
}
}
if (enemyDetected) {
api->angle = evadeAngle;
} else {
api->angle = foodAngle;
}
// Signal that everything is ok. Return false here if anything goes wrong but
// you want to shut down cleanly.
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment