-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.hpp
198 lines (189 loc) · 7.53 KB
/
piece.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
192
193
194
195
196
197
198
#ifndef PIECE_HPP
#define PIECE_HPP
class Piece {
public:
Piece():dir_(HORIZONAL) {}
virtual ~Piece() {}
virtual void construct(blist & blocks,int offset=0) = 0 ;
void unmark(GameBoard &gb) {
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, [&](Block & b) {b.unmark(gb);});
}
void mark(GameBoard &gb) {
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, [&](Block & b) {b.mark(gb);});
}
virtual void move(GameBoard &gb) {
blist::iterator eitr = litr_;
eitr++;
unmark(gb);
if (eitr == std::find_if_not(sitr_, eitr, [&](Block &b) {return b.canMove(gb);})) {
std::for_each(sitr_, eitr, [&](Block & b) {b.uncheckedMove();});
hasMoved = true;
}
else {
sitr_->stopMoving();
}
mark(gb);
}
virtual bool down(GameBoard &gb) {
bool result = false;
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, [&](Block & b) {b.unmark(gb);});
if (eitr == std::find_if_not(sitr_, eitr, [&](Block &b) {return b.canMoveDown(gb);})) {
std::for_each(sitr_, eitr, [&](Block & b) {b.uncheckedMoveDown();});
result = true;
}
std::for_each(sitr_, eitr, [&](Block & b) {b.mark(gb);});
return result;
}
virtual bool up(GameBoard &gb) {
bool result = false;
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, [&](Block & b) {b.unmark(gb);});
if (eitr == std::find_if_not(sitr_, eitr, [&](Block &b) {return b.canMoveUp(gb);})) {
std::for_each(sitr_, eitr, [&](Block & b) {b.uncheckedMoveUp();});
result= true;
}
std::for_each(sitr_, eitr, [&](Block & b) {b.mark(gb);});
return result;
}
virtual bool left(GameBoard &gb) {
bool result = false;
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, [&](Block & b) {b.unmark(gb);});
if (eitr == std::find_if_not(sitr_, eitr, [&](Block &b) {return b.canMoveLeft(gb);})) {
std::for_each(sitr_, eitr, [&](Block & b) {b.uncheckedMoveLeft();});
result =true;
}
std::for_each(sitr_, eitr, [&](Block & b) {b.mark(gb);});
return result;
}
virtual bool right(GameBoard &gb) {
bool result = false;
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, [&](Block & b) {b.unmark(gb);});
if (eitr == std::find_if_not(sitr_, eitr, [&](Block &b) {return b.canMoveRight(gb);})) {
std::for_each(sitr_, eitr, [&](Block & b) {b.uncheckedMoveRight();});
result =true;
}
std::for_each(sitr_, eitr, [&](Block & b) {b.mark(gb);});
return result;
}
bool done_moving() {
bool result = findIf([](Block &b)->bool {return b.done_moving();});
if (result)
forEachBlock([](Block &b){ b.stopMoving(); });
return result;
}
void unmarkAll(GameBoard &gb) {
forEachBlock(std::bind(&Block::unmark, std::placeholders::_1, std::ref(gb)));
}
void markAll(GameBoard &gb) {
forEachBlock(std::bind(&Block::mark, std::placeholders::_1, std::ref(gb)));
}
void forEachBlock(std::function<void(Block&)> func) {
blist::iterator eitr = litr_;
eitr++;
std::for_each(sitr_, eitr, func);
}
bool findIf(std::function<bool(Block&)> func) {
blist::iterator eitr = litr_;
eitr++;
return eitr != std::find_if(sitr_, eitr, func);
}
virtual void rotateClockwise(GameBoard &gb) {
const blist::iterator centerItr = getCenter();
int cx = centerItr->x_;
int cy = centerItr->y_;
unmarkAll(gb);
/*
* -1,-1 -> 1 -1
* 0,-1 -> 1,0
* 1, -1 - >1,1
* 1,0 ->0,1
* 1,1 -> -1,1
* 0,1 -> -1,0
* -1,1 ->-1,-1
* -1,0 ->0,-1
*/
std::vector<Modification> modifications;
if (canMove(gb,cx-1,cy-1,cx+1,cy-1,modifications) &&
canMove(gb,cx,cy-1,cx+1,cy,modifications) &&
canMove(gb,cx+1,cy-1,cx+1,cy+1,modifications) &&
canMove(gb,cx+1,cy,cx,cy+1,modifications) &&
canMove(gb,cx+1,cy+1,cx-1,cy+1,modifications) &&
canMove(gb,cx,cy+1,cx-1,cy,modifications) &&
canMove(gb,cx-1,cy+1,cx-1,cy-1,modifications) &&
canMove(gb,cx-1,cy,cx,cy-1,modifications)) {
std::for_each(modifications.begin(),modifications.end(), [](Modification &m) {m.blockp->x_ = m.nx; m.blockp->y_ = m.ny; });
}
markAll(gb);
return;
}
virtual void rotateCounterClockwise(GameBoard & gb) {
const blist::iterator centerItr = getCenter();
int cx = centerItr->x_;
int cy = centerItr->y_;
unmarkAll(gb);
std::vector<Modification> modifications;
if (canRMove(gb,cx-1,cy-1,cx+1,cy-1,modifications) &&
canRMove(gb,cx,cy-1,cx+1,cy,modifications) &&
canRMove(gb,cx+1,cy-1,cx+1,cy+1,modifications) &&
canRMove(gb,cx+1,cy,cx,cy+1,modifications) &&
canRMove(gb,cx+1,cy+1,cx-1,cy+1,modifications) &&
canRMove(gb,cx,cy+1,cx-1,cy,modifications) &&
canRMove(gb,cx-1,cy+1,cx-1,cy-1,modifications) &&
canRMove(gb,cx-1,cy,cx,cy-1,modifications)) {
std::for_each(modifications.begin(),modifications.end(), [](Modification &m) {m.blockp->x_ = m.nx; m.blockp->y_ = m.ny; });
}
markAll(gb);
return;
}
protected:
struct Modification {
Block * blockp;
int nx;
int ny;
};
bool canRMove(GameBoard & gb, int ix,int iy, int dx, int dy,std::vector<Modification> &mods) const {
return canMove(gb,dx,dy, ix,iy, mods);
}
bool canMove(GameBoard & gb, int ix,int iy, int dx, int dy,std::vector<Modification> &mods) const {
if (dy < 0 || dy > gb.maxy()-1)
return false;
blist::iterator eitr = litr_;
eitr++;
blist::iterator foundItr =std::find_if(sitr_, eitr, [&ix,&iy](Block &b)->bool {return b.x_ == ix && b.y_ == iy;}) ;
if (eitr == foundItr)
return true;
if (gb.isClear(dx,dy)) {
Modification m;
m.blockp = &*foundItr;
m.nx = dx;
m.ny = dy;
mods.push_back(m);
return true;
}
return false;
}
blist::iterator getCenter() {
blist::iterator citr = sitr_;
citr++;
citr++;
return citr;
}
public:
blist::iterator sitr_;
blist::iterator litr_;
bool hasMoved =false;
protected:
enum {HORIZONAL, VERTICAL,UHORIZONAL, RVERTICAL} dir_;
};
#endif