This repository was archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheatmap.cpp
More file actions
185 lines (165 loc) · 4.81 KB
/
Copy pathheatmap.cpp
File metadata and controls
185 lines (165 loc) · 4.81 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***************************************************************************//**
* @file
*
* @brief Contains functions for the Heatmap class
******************************************************************************/
#include "heatmap.h"
#include <iomanip>
/*************************************************************************//**
* @author William Doering
*
* @par Description:
* Generates the heatmap
*
* @param[in] map - adjacency map of the game board
* @param[in] grid - map of game board
****************************************************************************/
void Heatmap::generateMap(Graph &map, const int *grid)
{
//Queue setup
std::queue<int> curr;
curr.push(head_pos);
bool visited[SIZE] = {false};
std::set<int> adj_set;
int curr_heat = 0;
heat[head_pos] = 0;
visited[head_pos] = true;
while(!curr.empty())
{
adj_set = map.adj(curr.front());
curr_heat = heat[curr.front()];
curr.pop();
for(auto i : adj_set)
if(!visited[i])
{
heat[i] = curr_heat + 1;
curr.push(i);
visited[i] = true;
}
}
for(int i=0; i<SIZE; i++)
if(visited[i] == false)
heat[i] = -1;
}
/*************************************************************************//**
* @author William Doering
*
* @par Description:
* Constructor for the Heatmap class
*
* @param[in] map - adjacency map of the game board
* @param[in] pf - Playfield class for snake
****************************************************************************/
Heatmap::Heatmap(Graph &map, const Playfield *pf)
{
std::set<int> adj;
const int *grid = pf->getGrid();
head_pos = findItem(grid, HEAD_VALUE);
generateMap(map, grid);
}
/*************************************************************************//**
* @author William Doering
*
* @par Description:
* Deconstructor for the Heatmap class
****************************************************************************/
Heatmap::~Heatmap() {}
/*************************************************************************//**
* @author William Doering
*
* @par Description:
* Finds the best move to get to the target.
*
* @param[in] grid - Map of playing field
* @param[in] pos - target destination
*
* @returns move - current best move to get to target location
* @returns NONE - no path was found
****************************************************************************/
ValidMove Heatmap::moveTowards(const int *grid, int pos)
{
int best_adj = -1;
int new_pos;
//If target is adjacent to head
if(heat[pos] == 1)
{
if(head_pos <= (pos-PLAYFIELD_WIDTH))
return UP;
if(head_pos == (pos-1))
return RIGHT;
if(head_pos == (pos+1))
return LEFT;
if(head_pos >= (pos+PLAYFIELD_WIDTH))
return DOWN;
return NONE;
}
//Recurse until target is adjacent to head
//Check bottom
if((pos-PLAYFIELD_WIDTH) >= 0 && heat[pos-PLAYFIELD_WIDTH] != -1)
{
best_adj = heat[pos-PLAYFIELD_WIDTH];
new_pos = pos-PLAYFIELD_WIDTH;
}
//Check left
if((pos%PLAYFIELD_WIDTH) > 0)
if(checkHeat(pos-1) || best_adj == -1)
{
best_adj = heat[pos-1];
new_pos = pos-1;
}
//Check right
if((pos+1) < SIZE && ((pos+1)%PLAYFIELD_WIDTH) > 0)
if(checkHeat(pos+1) || best_adj == -1)
{
best_adj = heat[pos+1];
new_pos = pos+1;
}
//Check top
if(((pos/PLAYFIELD_WIDTH)+1) < PLAYFIELD_HEIGHT)
if(checkHeat(pos+PLAYFIELD_WIDTH) || best_adj == -1)
{
best_adj = heat[pos+PLAYFIELD_WIDTH];
new_pos = pos+PLAYFIELD_WIDTH;
}
//No path
if(best_adj == -1)
return NONE;
//Recurse and return result
return moveTowards(grid, new_pos);
}
/*************************************************************************//**
* @author William Doering
*
* @par Description:
* Finds an item in the playfield
*
* @param[in] grid - map of game board
* @param[in] item - item searched for
*
* @returns food - location of item in array
* @returns -1 - item not found
****************************************************************************/
int Heatmap::findItem(const int *grid, int item)
{
for(int i=0; i<SIZE; i++)
if(grid[i] == item)
return i;
return -1;
}
/*************************************************************************//**
* @author William Doering
*
* @par Description:
* Prints out heatmap
****************************************************************************/
void Heatmap::print()
{
for(int row=PLAYFIELD_HEIGHT-1; row>=0; row--)
{
for(int col=0; col<PLAYFIELD_WIDTH; col++)
{
std::cout << std::setw(3) << heat[row*PLAYFIELD_WIDTH+col] << ' ';
}
std::cout << std::endl;
}
}