-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.cpp
76 lines (68 loc) · 1.76 KB
/
entity.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
#include "entity.h"
#include <vector>
#include "utils.h"
Entity::Entity(char symbol, Color c)
{
this->symbol = symbol;
this->color = c;
entityPos.first = -1;
entityPos.second = -1;
}
//function that tries to place the entity in a position of the board, as efficiently as possible.
bool Entity::randomizePos(Board &board)
{
//recreate the map in binary form
int pathCels = 0;
std::vector< std::vector<bool> > binaryMap(board.getBoardRows(), std::vector<bool>(board.getBoardCols(), false));
//true if the position is a path and false if it is
for (int i = 0; i < board.getBoardRows(); i++)
{
for (int j = 0; j < board.getBoardCols(); j++)
{
if (board.posIsChar(i, j, '.'))
{
//if the position is a path increase the counter
binaryMap[i][j] = true;
pathCels++;
}
}
}
//if there are no path cells return false
if (pathCels == 0)
return false;
//convert the positions to 1d array
Position *pos = new Position[pathCels];
int index = 0;
for (int i = 0; i < board.getBoardRows(); i++)
{
for (int j = 0; j < board.getBoardCols(); j++)
{
if (binaryMap[i][j])
{
pos[index].first = i;
pos[index].second = j;
index++;
}
}
}
//find a random position from the free previous positions array
entityPos = pos[utils::randNum(0, pathCels - 1)];
delete[] pos;
return true;
}
void Entity::setPos(Position p)
{
this->entityPos = p;
}
Position Entity::getPos()
{
return this->entityPos;
}
char Entity::getSymbol()
{
return this->symbol;
}
Color Entity::getColor()
{
return this->color;
}