This program is a simulation of an ant colony, inspired by SimAnt. It doesn't do it off the batch; there must be code that instructs the ants to follow scent tiles in particular and react to the food.
main.cpp
It's the main file that must be modified to put more/less ants, more/less food/ants (generators or not).
#include "tile.cpp"
#include "foodFactory.cpp"
#include "scent.cpp"
#include "tile.cpp"
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
foodFactory a(43, 12);
a.splatFood();
antFactory b(12, 95);
b.makeAnt();
b.makeAnt();
b.makeAnt();
bool exit = false;
while(exit == false)
{
b.tickAnts();
sleep(500).
if (cin.good())
{
exit = true;
}
}
return 0;
}
antFactory.cpp
Despite the filename, this contains the ant code and the ant factory code. The ant's class code must be modified so they grab food and wander to find food and move to the food and follow scent and the general AI & behaviour.
#pragma once
#include "scent.cpp"
#include <iostream>
#include <vector>
using namespace std;
class ant
{
private:
int x;
int y;
int index;
antScent newDropScent;
bool foundFood;
public:
ant(int nx, int ny, int i)
{
x = nx;
y = ny;
index = i;
}
pair<int, int> moveThisAnt(pair<bool, bool> dir, pair<bool, bool> move)
{
if (move.first)
{
if (dir.first)
x += 1;
else
x -= 1;
}
if (move.second)
{
if (dir.second)
y += 1;
else
y -= 1;
}
pair<int, int> result;
result.first = x;
result.second = y;
return result;
}
void tickThisAnt() {
cout << "Function is not completed!";
}
};
class antFactory
{
private:
int x;
int y;
vector<ant> antGenerated;
public:
ant* makeAnt()
{
ant z(x, y, antGenerated.size() + 1);
antGenerated.push_back(z);
return &antGenerated[antGenerated.size() - 1];
}
antFactory(int nx, int ny) {
x = nx;
y = ny;
}
ant delAnt(int antindex)
{
ant theresult = antGenerated[antindex];
antGenerated.erase(antGenerated.begin() + antindex);
return theresult;
}
void tickAnts()
{
signed int i = 0;
for (i = 0; i < antGenerated.size(); i++)
{
antGenerated[i].tickThisAnt;
}
}
};
food.cpp
This is the food's class definition. Not much :/
using namespace std;
class food {
private:
short foodSize;
int x;
int y;
int index;
public:
food(int nx, int ny, int i, short ns) {
x = nx;
y = ny;
index = i;
foodSize = ns;
}
short beTaken(short biteSize) {
foodSize -= biteSize;
return foodSize;
}
};
foodFactory.cpp
This is the factory that creates and splats the food the ants are going to take.
#include <vector>
#include <cstdlib>
#include "food.cpp"
using namespace std;
class foodFactory {
private:
int x;
int y;
vector<food> foodGenerated;
public:
foodFactory(int nx, int ny) {
x = nx;
y = ny;
}
food* splatFood() {
x = -200 + (rand() % (int)(200 - -200 + 1));
y = -200 + (rand() % (int)(200 - -200 + 1));
food z(x, y, (foodGenerated.size() + 1), (5 + (rand() % (int)(50 - 5 + 1))));
foodGenerated.push_back(z);
return &foodGenerated[foodGenerated.size()];
}
};
scent.cpp
The class definition of the scent that guides ants.
#pragma once
using namespace std;
class antScent {
private:
short power;
short angle;
short scentType;
public:
short addPower() {
power += 1;
return power;
}
short affectAngle(short vel) {
angle = (vel + angle) / 2;
return angle;
}
void changeType(short newType) {
scentType = newType;
}
short decreasePower() {
power -= 1;
return power;
}
};
tile.cpp
An reserved class that I should have replaced the coordinate system of everything with variables x
and y
with.
#pragma once
#include "antFactory.cpp"
using namespace std;
class tile
{
private:
int x;
int y;
antScent tileScent;
public:
tile(int nx, int ny)
{
x = nx;
y = ny;
}
tile()
{
x = 0;
y = 0;
}
void changeScent(antScent newscent)
{
tileScent = newscent;
}
};
GitHub has a more recent dev version. It is possible that it might be done right off the bat if someone can contribute and do the ant AI there...
a
andb
\$\endgroup\$