-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.hpp
191 lines (176 loc) · 4.9 KB
/
block.hpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef BLOCK_HPP
#define BLOCK_HPP
#include <list>
#include "movable.hpp"
#include "vectorable.hpp"
class Block : public Movable, public Vectorable {
public:
typedef enum {RED, BLUE, GREEN,WHITE, MAX_COLOR} color_t;
static int getMaxColorNum() {
return MAX_COLOR;
}
static color_t getEnumByNum(int n) {
return (color_t)(n%MAX_COLOR);
}
Block() :x_(0), y_(0), color_(RED){dy_ = 1;dx_ = 0;}
Block(int x, int y, color_t color=RED ) :x_(x), y_(y), color_(color){dy_ = 1;dx_ =0;}
Block(int x, int y, int dx, int dy, color_t color=RED ) :x_(x), y_(y), color_(color),dy_(dy),dx_(dx){}
Block(Block && other ) {
this->x_ = other.x_;
this->y_ = other.y_;
this->dy_ = other.dy_;
this->dx_ = other.dx_;
this->color_ = other.color_;
}
Block& operator=(const Block& other) {
this->x_ = other.x_;
this->y_ = other.y_;
this->dy_ = other.dy_;
this->dx_ = other.dx_;
this->color_ = other.color_;
return *this;
}
void setColor(color_t c) {color_ =c ;}
color_t getColor() const {return color_;}
void setDxDy(int dx, int dy) {dx_ =dx; dy_ =dy;}
int getX() const {
return x_;
}
int getY() const {
return y_;
}
int getZ() const {
return 0;
}
int setX(const int x){
return x_ = x;
}
int setY(const int y){
return y_ = y;
}
int setZ(__attribute__((unused))int z){
return 0;
}
void set(GameBoard & gb, const int x, const int y) {
unmark(gb);
x_ = x;
y_ = y;
mark(gb);
}
void unmark(GameBoard & gb) {
gb.clear(x_,y_);
}
void mark(GameBoard & gb) {
gb.set(x_,y_,'o');
}
bool canMove(const GameBoard & gb) const {
if ((1 == dy_ && y_ == gb.maxy()-1) ||
(-1 == dy_ && y_ == 0) ||
(1 == dx_ && x_ == gb.maxx()-1) ||
(-1 == dx_ && x_ == 0) ||
!gb.isClear(dx_ + x_,y_ + dy_)){
return false;
}
return true;
}
void move(GameBoard & gb){
unmark(gb);
if (canMove(gb)) {
uncheckedMove();
}else {
stopMoving();
}
mark(gb);
}
void down(GameBoard & gb){
unmark(gb);
if (canMoveDown(gb)) {
uncheckedMoveDown();
}else {
if (1 == dy_)
stopMoving();
}
mark(gb);
}
void up(GameBoard & gb){
unmark(gb);
if (canMoveUp(gb)) {
uncheckedMoveUp();
}else {
if (-1 == dy_)
stopMoving();
}
mark(gb);
}
bool canMoveUp(GameBoard & gb) const {
return (!done_moving() && y_ > 0 && gb.isClear(x_,y_-1));
}
bool canMoveDown(GameBoard & gb) const {
if (y_ == gb.maxy()-1 || !gb.isClear(x_,y_+1)){
return false;
}
return true;
}
bool canMoveLeft(GameBoard & gb) const {
return (!done_moving() && x_ > 0 && gb.isClear(x_-1,y_));
}
void stopMoving() {
dx_= dy_ = 0;
}
void uncheckedMoveLeft() {
x_--;
}
void uncheckedMove() {
x_+= dx_;
y_+= dy_;
}
bool left(GameBoard & gb) {
if (canMoveLeft(gb)) {
uncheckedMoveLeft();
return true;
} else {
if (-1 == dx_)
stopMoving();
}
return false;
}
bool canMoveRight(GameBoard & gb) const {
return (!done_moving() && x_ < gb.maxx()&& gb.isClear(x_+1,y_));
}
void uncheckedMoveRight() {
x_++;
}
void uncheckedMoveDown() {
y_ += 1;
}
void uncheckedMoveUp() {
y_ -= 1;
}
bool right(GameBoard & gb) {
if (canMoveRight(gb)) {
uncheckedMoveRight();
return true;
} else {
if (1 == dx_)
stopMoving();
}
return false;
}
bool done_moving() const {
return dy_ == 0 && dx_ == 0;
}
int x_;
int y_;
int getDy() {
return dy_;
}
int getDx() {
return dx_;
}
private:
color_t color_;
int dy_;
int dx_;
};
typedef std::list<Block> blist;
#endif