Skip to content

Commit

Permalink
Better AI (now uses snakes)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchampeimont committed Dec 14, 2013
1 parent f395ca4 commit d3969c2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 39 deletions.
108 changes: 74 additions & 34 deletions project/src/AI.cpp
Expand Up @@ -27,11 +27,13 @@

#include "Program.h"

AI::AI(int p, Maze *m) : dijkstraQ(m->width * m->height * 4) {
AI::AI(int p, Maze *m, Game *g) : dijkstraQ(m->width * m->height * 4) {
player = p;
maze = m;
maze = m;
game = g;
homeI = 0;
homeJ = 0;
homeJ = 0;
snakeMode = false;
}

AI::~AI() {
Expand Down Expand Up @@ -74,13 +76,36 @@ void AI::updateGraph() {
}
}


// Where is the player's house?
for (unsigned k=0; k<maze->houses.size(); k++) {
if (maze->houses[k].player == player) {
homeI = maze->houses[k].i;
homeJ = maze->houses[k].j;
}

if (snakeMode) {
// choose a victim
int victim = -1;
int bestScore = 0;
for (int k=0; k<game->players; k++) {
if (k == player) {
// Do not choose myself as the victim
continue;
}
if (victim < 0 || game->scores[k] > bestScore) {
victim = k;
bestScore = game->scores[k];
}
}
// Where is the victim's house?
for (unsigned k=0; k<maze->houses.size(); k++) {
if (maze->houses[k].player == victim) {
homeI = maze->houses[k].i;
homeJ = maze->houses[k].j;
}
}
} else {
// Where is the player's house?
for (unsigned k=0; k<maze->houses.size(); k++) {
if (maze->houses[k].player == player) {
homeI = maze->houses[k].i;
homeJ = maze->houses[k].j;
}
}
}

// We cannot cross walls
Expand Down Expand Up @@ -169,7 +194,7 @@ void AI::updateGraph() {


// Take in account other's players houses:
// A mouse can't "escape" such a cell.
// An animal can't "escape" such a cell.
for (unsigned k=0; k<maze->houses.size(); k++) {
House *house = &maze->houses[k];
if (house->player != player) {
Expand Down Expand Up @@ -316,35 +341,50 @@ void AI::computeDistances() {


// Allow AI to place one arrow
void AI::play() {
void AI::play() {
// Decide between two strategies: gathering mice or sending snakes on others
snakeMode = maze->snakes.size() >= maze->mice.size()/10;

// Make a graph that represents the maze
updateGraph();

// Compute distances to house from vertex in the graph
computeDistances();

vector<AnimalWithDistance> sortedAnimals;
if (snakeMode) {
sortedAnimals.reserve(maze->snakes.size());
for (unsigned m=0; m<maze->snakes.size(); m++) {
Animal *animal = &maze->snakes[m];
int i, j;
animal->getFutureCell(&i, &j);
int vertexIndex = vertexIndexFromCoords(i, j, animal->direction);
int dist = getDistance(vertexIndex);
sortedAnimals.push_back(AnimalWithDistance(animal, dist));
}
} else {
// Sort mice by their distance
sortedAnimals.reserve(maze->mice.size());
for (unsigned m=0; m<maze->mice.size(); m++) {
Animal *animal = &maze->mice[m];
int i, j;
animal->getFutureCell(&i, &j);
int vertexIndex = vertexIndexFromCoords(i, j, animal->direction);
int dist = getDistance(vertexIndex);
sortedAnimals.push_back(AnimalWithDistance(animal, dist));
// debug: color mouse by distance
/*
{
float rdist = dist / 50.0f;
if (rdist > 1) rdist = 1;
mouse->color.r = rdist;
mouse->color.g = 1 - rdist;
mouse->color.b = 0;
}
*/
}
}

// Sort mice by their distance
vector<AnimalWithDistance> sortedAnimals;
sortedAnimals.reserve(maze->mice.size());
for (unsigned m=0; m<maze->mice.size(); m++) {
Mouse *mouse = &maze->mice[m];
int i, j;
mouse->getFutureCell(&i, &j);
int vertexIndex = vertexIndexFromCoords(i, j, mouse->direction);
int dist = getDistance(vertexIndex);
sortedAnimals.push_back(AnimalWithDistance(mouse, dist));

// debug: color mouse by distance
/*
{
float rdist = dist / 50.0f;
if (rdist > 1) rdist = 1;
mouse->color.r = rdist;
mouse->color.g = 1 - rdist;
mouse->color.b = 0;
}
*/
}
sort(sortedAnimals.begin(), sortedAnimals.end());

for (unsigned m=0; m<sortedAnimals.size(); m++) {
Expand Down
8 changes: 5 additions & 3 deletions project/src/AI.h
Expand Up @@ -38,17 +38,19 @@ using namespace std;

class AI {
public:
Maze *maze;
Maze *maze;
Game *game;
int player;
vector<AIVertex> graph;
int homeI, homeJ;
int homeI, homeJ;
bool snakeMode;

// Dijkstra's algo stuff
vector<int> pathToHome;
vector<bool> dijkstraVisited;
AIVertexHeap dijkstraQ;
vector<AnimalWithDistance> sortedAnimals;
AI(int, Maze*);
AI(int, Maze*, Game*);
~AI();
void init();
void play();
Expand Down
2 changes: 1 addition & 1 deletion project/src/Game.cpp
Expand Up @@ -194,7 +194,7 @@ void Game::run() {

for (int p=0; p<4; p++) {
if (MenuPlayers::playerControls[p] == 2) {
AIs.push_back(AI(p, &maze));
AIs.push_back(AI(p, &maze, this));
}
}

Expand Down
2 changes: 1 addition & 1 deletion project/src/Program.cpp
Expand Up @@ -362,7 +362,7 @@ void Program::run() {
sound = new Sound();
sound->init();

// Set FPS limit depending if battery/AC
// Set FPS limit
fps = new FPS();
fps->decideLimitFPS();

Expand Down
1 change: 1 addition & 0 deletions project/src/Sound.cpp
Expand Up @@ -58,6 +58,7 @@ Mix_Music *Sound::loadMusic(string filename) {
void Sound::playMusic(int musicIndex) {
if (Program::getInstance()->config.music) {
if (musicPlaying != musicIndex) {
Mix_VolumeMusic(64);
if (Mix_FadeInMusic(music[musicIndex], -1, 1000) != 0) {
Functions::error(Mix_GetError());
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d3969c2

Please sign in to comment.