-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.cpp
80 lines (70 loc) · 2.17 KB
/
robot.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
78
79
80
#include "robot.h"
namespace planet{
size_t Robot::X(size_t x){
return (x + ground[0].capacity()) % ground[0].capacity();
}
size_t Robot::Y(size_t y){
return (y + ground.size()) % ground.size();
}
void Robot::confirmServerStep(){
if(ground[yGround][xGround] == BOMB){
ground[yGround][xGround] = EMPTY;
server.updatedMap[Y(yServer)][X(xServer)] = EMPTY;
}
if(ground[yGround][xGround] == ROCK)
server.send(xServer, yServer, id, robotStatus::DIE);
else
server.send(xServer, yServer, id);
}
void Robot::moveRight(){
xGround = X(xGround + 1);
xServer = xServer + 1;
confirmServerStep();
}
void Robot::moveLeft(){
xGround = X(xGround - 1);
xServer = xServer - 1;
confirmServerStep();
}
void Robot::moveDown(){
yGround = Y(yGround + 1);
yServer = yServer + 1;
confirmServerStep();
}
void Robot::moveUp(){
yGround = Y(yGround - 1);
yServer = yServer - 1;
confirmServerStep();
}
Robot::Robot(std::vector<vectorItems>& ground, data& server) : ground(ground), server(server){
srand(time(NULL));
xGround = size_t(rand()) % ground.size();
yGround = size_t(rand()) % ground[0].capacity();
while (ground[yGround][xGround] == ROCK)
{
xGround = size_t(rand()) % ground.size();
yGround = size_t(rand()) % ground[0].capacity();
}
xServer = server.xFromFirstLanding(xGround);
yServer = server.yFromFirstLanding(yGround);
id = server.getId();
server.send(xServer, yServer, ground[yGround][xGround]);
confirmServerStep();
}
void Robot::move(Direction way){
switch(way){
case Direction::UP:
moveUp();
break;
case Direction::DOWN:
moveDown();
break;
case Direction::LEFT:
moveLeft();
break;
case Direction::RIGHT:
moveRight();
break;
}
}
}