-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.h
104 lines (95 loc) · 2.56 KB
/
player.h
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
#include<iostream>
#include<vector>
using namespace std;
typedef enum direction {East,West,North,South} direction;
typedef enum actions {Turn,Run} actions;
typedef struct position{
int x;
int y;
} coordinates;
class Player{
private:
coordinates position;
direction lookingTowards;
string message;
void turnTowards(direction dir)
{
lookingTowards = dir;
message = "I am looking towards " + dir;
}
bool moveForward(int distance)
{
switch(lookingTowards)
{
case direction::East:
position.x += distance;
break;
case direction::West:
position.x -= distance;
break;
case direction::North:
position.y += distance;
break;
case direction::South:
position.y -= distance;
break;
}
message = "I moved to ( " + position.x + ', ' + position.y + ' ).';
return true;
}
direction convertToDirection(string parameter)
{
if(parameter.compare("East") == 0)
return direction::East;
else if(parameter.compare("West") == 0)
return direction::West;
else if(parameter.compare("North") == 0)
return direction::North;
else if(parameter.compare("South") == 0)
return direction::South;
throw new exception();
return direction::East;
}
actions convertToAction(string action)
{
if(action.compare("Turn")==0)
return actions::Turn;
else if(action.compare("Run")==0)
return actions::Run;
throw new exception();
return actions::Run;
}
public:
Player(int x = 0, int y = 0, direction dir = direction::East)
{
position.x = x;
position.y = y;
lookingTowards = dir;
}
bool performAction(string action , string parameter)
{
actions act = convertToAction(action);
switch(act)
{
case actions::Turn:
{
direction dir = convertToDirection(parameter);
turnTowards(dir);
return true;
break;
}
case actions::Run:
{
int distance = stoi(parameter);
moveForward(distance);
return moveForward(distance);;
break;
}
}
return false;
}
string getPlayerMessage()
{
return message;
}
};