-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.cpp
77 lines (69 loc) · 1.53 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
#include "globals.h"
#include "Robot.h"
#include "Player.h"
#include "Arena.h"
#include "Game.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
///////////////////////////////////////////////////////////////////////////
// Robot implementation
///////////////////////////////////////////////////////////////////////////
Robot::Robot(Arena* ap, int r, int c)
{
if (ap == nullptr)
{
cout << "***** A robot must be in some Arena!" << endl;
exit(1);
}
if (r < 1 || r > ap->rows() || c < 1 || c > ap->cols())
{
cout << "***** Robot created with invalid coordinates (" << r << ","
<< c << ")!" << endl;
exit(1);
}
m_arena = ap;
m_row = r;
m_col = c;
m_health = 2;
}
int Robot::row() const
{
return m_row;
}
int Robot::col() const
{
return m_col;
}
void Robot::move()
{
// Attempt to move in a random direction; if we can't move, don't move
switch (rand() % 4)
{
case UP:
if (m_row > 1 && m_row <= m_arena->rows())
m_row --;
break;
case DOWN:
if (m_row > 0 && m_row < m_arena->rows())
m_row++;
break;
case LEFT:
if (m_col > 1 && m_col <= m_arena->cols())
m_col--;
break;
case RIGHT:
if (m_col > 0 && m_col < m_arena->cols())
m_col ++;
break;
}
}
bool Robot::takeDamageAndLive()
{
m_health--;
if (m_health == 0)
return false;
return true;
}