-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBlock.cpp
153 lines (144 loc) · 5.25 KB
/
VBlock.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
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
#include <vector>
#include "VBlock.h"
#include "coordinate.h"
using namespace std;
VBlock::VBlock(int levelNum)
{
// Set the block's position to its initial position as defined below
// Vertical numbers: row number, Horizontal numbers: column number
// 1 2 3
// 1
// 2
// 3
// 4 V V
// 5 V V
// 6 V
blockParts_.push_back(shared_ptr<coordinate>(new coordinate(4, 1)));//block part 1, denoted as c1
blockParts_.push_back(shared_ptr<coordinate>(new coordinate(5, 1)));//block part 2, denoted as c2
blockParts_.push_back(shared_ptr<coordinate>(new coordinate(6, 2)));//block part 3, denoted as c3
blockParts_.push_back(shared_ptr<coordinate>(new coordinate(5, 3)));//block part 4, denoted as c4
blockParts_.push_back(shared_ptr<coordinate>(new coordinate(4, 3)));//block part 5, denoted as c5
rotationState_ = 1;
levelGenerated_ = levelNum;
}
char VBlock::getType() const{
return 'V';
}
VBlock::~VBlock() {}
void VBlock::rotate(bool clockwise)
{
// The four rotation states:
// Calling rotate will move the block from its current state an adjacent state
// If clockwise is true, the state numbers will increase
// I.E. Clockwise rotations: State 1 -> State 2 -> State 3 -> State 4 -> State 1
// Counterclockwise rotations: State 1 -> State 4 -> State 3 -> State 2 -> State 1
// Vertical numbers: row number, Horizontal numbers: column number
// The block's default position is used for this illustration
// State 1 State 2 State 3 State 4
// 1 2 3 1 2 3 1 2 3 1 2 3
// 3 3 3 3
// 4 V V 4 V V 4 V 4 V V
// 5 V V 5 V 5 V V 5 V
// 6 V 6 V V 6 V V 6 V V
// Rotatation states with specific block parts (see constructor):
// State 1 State 2 State 3 State 4
// 1 2 3 1 2 3 1 2 3 1 2 3
// 3 3 3 3
// 4 c1 c5 4 c2 c1 4 c3 4 c5 c4
// 5 c2 c4 5 c3 5 c4 c2 5 c3
// 6 c3 6 c4 c5 6 c5 c1 6 c1 c2
if(rotationState_ == 1 && clockwise)//state 1 -> state 2
{
blockParts_[0]->shiftCol(2);
blockParts_[1]->shiftRow(-1);
blockParts_[1]->shiftCol(1);
blockParts_[2]->shiftRow(-1);
blockParts_[2]->shiftCol(-1);
blockParts_[3]->shiftRow(1);
blockParts_[3]->shiftCol(-1);
blockParts_[4]->shiftRow(2);
}
else if(rotationState_ == 2 && clockwise)//state 2 -> state 3
{
blockParts_[0]->shiftRow(2);
blockParts_[1]->shiftRow(1);
blockParts_[1]->shiftCol(1);
blockParts_[2]->shiftRow(-1);
blockParts_[2]->shiftCol(1);
blockParts_[3]->shiftRow(-1);
blockParts_[3]->shiftCol(-1);
blockParts_[4]->shiftCol(-2);
}
else if(rotationState_ == 3 && clockwise)//state 3 -> state 4
{
blockParts_[0]->shiftCol(-2);
blockParts_[1]->shiftRow(1);
blockParts_[1]->shiftCol(-1);
blockParts_[2]->shiftRow(1);
blockParts_[2]->shiftCol(1);
blockParts_[3]->shiftRow(-1);
blockParts_[3]->shiftCol(1);
blockParts_[4]->shiftRow(-2);
}
else if(rotationState_ == 4 && clockwise)//state 4 -> state 1
{
blockParts_[0]->shiftRow(-2);
blockParts_[1]->shiftRow(-1);
blockParts_[1]->shiftCol(-1);
blockParts_[2]->shiftRow(1);
blockParts_[2]->shiftCol(-1);
blockParts_[3]->shiftRow(1);
blockParts_[3]->shiftCol(1);
blockParts_[4]->shiftCol(2);
}
else if(rotationState_ == 1 && !clockwise)//state 1 -> state 4
{
blockParts_[0]->shiftRow(2);
blockParts_[1]->shiftRow(1);
blockParts_[1]->shiftCol(1);
blockParts_[2]->shiftRow(-1);
blockParts_[2]->shiftCol(1);
blockParts_[3]->shiftRow(-1);
blockParts_[3]->shiftCol(-1);
blockParts_[4]->shiftCol(-2);
}
else if(rotationState_ == 2 && !clockwise)//state 2 -> state 1
{
blockParts_[0]->shiftCol(-2);
blockParts_[1]->shiftRow(1);
blockParts_[1]->shiftCol(-1);
blockParts_[2]->shiftRow(1);
blockParts_[2]->shiftCol(1);
blockParts_[3]->shiftRow(-1);
blockParts_[3]->shiftCol(1);
blockParts_[4]->shiftRow(-2);
}
else if(rotationState_ == 3 && !clockwise)//state 3 -> state 2
{
blockParts_[0]->shiftRow(-2);
blockParts_[1]->shiftRow(-1);
blockParts_[1]->shiftCol(-1);
blockParts_[2]->shiftRow(1);
blockParts_[2]->shiftCol(-1);
blockParts_[3]->shiftRow(1);
blockParts_[3]->shiftCol(1);
blockParts_[4]->shiftCol(2);
}
else//state 4 -> state 3
{
blockParts_[0]->shiftCol(2);
blockParts_[1]->shiftRow(-1);
blockParts_[1]->shiftCol(1);
blockParts_[2]->shiftRow(-1);
blockParts_[2]->shiftCol(-1);
blockParts_[3]->shiftRow(1);
blockParts_[3]->shiftCol(-1);
blockParts_[4]->shiftRow(2);
}
changeStateFour(clockwise);
}
VBlockFactory::VBlockFactory() {}
shared_ptr<Block> VBlockFactory::createBlock(int levelNum)
{
return shared_ptr<VBlock>(new VBlock(levelNum));
}