-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnt.cpp
77 lines (69 loc) · 1.93 KB
/
Ant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "Ant.h"
#include "State.h"
Ant::Ant(void)
{
}
Ant::~Ant(void)
{
}
Ant::Ant(Location loc, State & state)
{
location = loc;
idleTurns = 0;
}
void Ant::chooseMove(State & state)
{
int chosenD = 4;
state.bug << "ant at " << location.row << " " << location.col << ": " << "FoodStrength"
<< state.grid[state.getLocation(location, chosenD).row][state.getLocation(location, chosenD).row].foodStrength << std::endl;
for(int d=0; d<TDIRECTIONS; d++)
{
Location testLoc = state.getLocation(location, d);
Location bestLoc = state.getLocation(location, chosenD);
//state.bug << "ant " << ant << ": "
// << "Row" << testLoc.row << " Col" << testLoc.col << " FoodStrength" << state.grid[testLoc.row][testLoc.col].foodStrength << endl;
if (calculateValue(testLoc, state) > calculateValue(bestLoc, state))
{
chosenD = d;
}
}
if ((chosenD != 4) && state.isSafeMove(location, chosenD))
{
state.makeMove(location, chosenD);
location = state.getLocation(location, chosenD);
idleTurns = 0;
}
else if (idleTurns > 1)
{
chosenD = rand() % 4;
if (state.isSafeMove(location, chosenD))
{
state.makeMove(location, chosenD);
location = state.getLocation(location, chosenD);
}
idleTurns++;
}
else
{
idleTurns++;
}
}
int Ant::calculateValue(Location & loc, State & state)
{
int value = 0;
value += state.grid[loc.row][loc.col].foodStrength;
value += state.grid[loc.row][loc.col].neverSeenStrength;
value += state.grid[loc.row][loc.col].enemyHillStrength;
value += state.grid[loc.row][loc.col].unseenStrength;
//value -= state.grid[loc.row][loc.col].myHillStrength / 4;
//value -= state.grid[loc.row][loc.col].enemyStrength / 4;
if (state.grid[loc.row][loc.col].myHillStrength > 20)
{
value += state.grid[loc.row][loc.col].enemyStrength * 2;
}
else if ((state.grid[loc.row][loc.col].enemyStrength * 4) >= state.grid[loc.row][loc.col].myAntsStrength)
{
value = 0;
}
return value;
};