-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cpp
More file actions
152 lines (118 loc) · 3.59 KB
/
State.cpp
File metadata and controls
152 lines (118 loc) · 3.59 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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
/*
This pattern makes use of Composite, in order to manage states without a moltitude of nested if-else loops, which lesser the maintenability
You must manage 3 lanes system. The player can switch from a lane to another: 1 to 2, 2 to 3, 3 to 2, 2 to 1.
While switching lane won't be accepted new movement commands.
If the player rotates left while in lane1, he will rotate 90 degrees left
If the player rotates right while in lane3, he will rotate 90 degrees right
*/
enum class LanePosition {LEFT, MIDDLE, RIGHT};
class ILaneState{
protected:
LanePosition currentLanePosition;
public:
virtual void moveLeft() = 0;
virtual void moveRight() = 0;
};
//forward defined this class, the declaration is after the definition of the children of ILaneState
class Player{
private:
string name;
public:
float rotation;
ILaneState *state;
ILaneState *leftState;
ILaneState *middleState;
ILaneState *rightState;
Player();
void moveLeft();
void moveRight();
};
class LeftLaneState : public ILaneState{
private:
Player *player = nullptr;
public:
LeftLaneState(Player *player){
this->player = player;
currentLanePosition = LanePosition::LEFT;
}
void moveLeft() override {
cout << "You can't go left from left lane" << endl;
cout << "Turn 90 degrees left" << endl;
player->rotation -= 90;
}
void moveRight() override {
cout << "Going middle from left lane" << endl;
player->state = player->middleState;
}
};
class MiddleLaneState : public ILaneState{
private:
Player *player = nullptr;
public:
MiddleLaneState(Player *player){
this->player = player;
currentLanePosition = LanePosition::MIDDLE;
}
void moveLeft() override {
cout << "Going left from middle lane" << endl;
player->state = player->leftState;
}
void moveRight() override {
cout << "Going right from middle lane" << endl;
player->state = player->rightState;
}
};
class RightLaneState : public ILaneState{
private:
Player *player = nullptr;
public:
RightLaneState(Player *player){
this->player = player;
currentLanePosition = LanePosition::RIGHT;
}
void moveLeft() override {
cout << "Going middle from right lane" << endl;
player->state = player->middleState;
}
void moveRight() override {
cout << "You can't go right from right lane" << endl;
cout << "Turn 90 degrees right" << endl;
player->rotation += 90;
}
};
Player::Player(){
name = "player";
rotation = 0;
leftState = new LeftLaneState(this);
middleState = new MiddleLaneState(this);
rightState = new RightLaneState(this);
state = middleState;
}
void Player::moveLeft(){
state->moveLeft();
}
void Player::moveRight(){
state->moveRight();
}
void listenKeyboardWindows(Player* player){
string key = "";
while(key != "exit"){
cin >> key;
if(key == "a"){
player->moveLeft();
}else if(key == "d"){
player->moveRight();
}
cout << "Player current rotation: " << player->rotation << endl;
}
return ;
}
int main(){
Player *player = new Player();
listenKeyboardWindows(player);
return 0;
}