-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPanel.cpp
177 lines (151 loc) · 3.26 KB
/
Panel.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Panel.h"
#include <cassert>
#include <cmath>
#include <cstdlib>
#include "shapes/Shape.h"
#include "shapes/ShapeFactory.h"
Panel::Panel()
: m_level(0)
, m_score(0)
, m_shape(0)
, m_isFinished(false)
{
Reset();
}
Panel::~Panel()
{
delete m_shape;
}
void Panel::Reset()
{
m_level = 0;
m_score = 0;
m_isFinished = false;
_ClearOccupied();
_GenerateShape();
}
void Panel::Shift()
{
m_shape->Shift(this);
}
void Panel::MoveDown()
{
if (!m_shape->MoveDown(this)) {
m_shape->Clamp(this);
_EraseLine(m_shape->GetMinX(), m_shape->GetMaxX());
if (!m_isFinished) {
delete m_shape;
_GenerateShape();
}
}
}
void Panel::MoveLeft()
{
m_shape->MoveLeft(this);
}
void Panel::MoveRight()
{
m_shape->MoveRight(this);
}
void Panel::Occupy(const Grid& grid)
{
assert(!m_occupied[grid.x][grid.y]);
m_occupied[grid.x][grid.y] = true;
if (grid.x < PANEL_RESERVE) {
m_isFinished = true;
}
}
bool Panel::IsBlank(const Grid& grid) const
{
if (grid.x < 0 || grid.x >= PANEL_HEIGHT ||
grid.y < 0 || grid.y >= PANEL_WIDTH) {
return false;
}
return !m_occupied[grid.x][grid.y];
}
bool Panel::IsBlank(const Grid& start, const Grid& end) const
{
assert(start.x <= end.x);
assert(start.y <= end.y);
if (start.x < 0 || end.x >= PANEL_HEIGHT ||
start.y < 0 || end.y >= PANEL_WIDTH) {
return false;
}
for (int x = start.x; x <= end.x; ++x) {
for (int y = start.y; y <= end.y; ++y) {
if (m_occupied[x][y]) {
return false;
}
}
}
return true;
}
bool Panel::IsFinished() const
{
return m_isFinished;
}
bool Panel::IsDirty(int iRow, int iColumn) const
{
iRow += PANEL_RESERVE;
return m_occupied[iRow][iColumn] || m_shape->IsBlock(iRow, iColumn);
}
int Panel::GetLevel() const
{
return m_level;
}
int Panel::GetScore() const
{
return m_score;
}
void Panel::_ClearOccupied()
{
for (int i = 0; i < PANEL_HEIGHT; ++i) {
for (int j = 0; j < PANEL_WIDTH; ++j) {
m_occupied[i][j] = false;
}
}
}
void Panel::_GenerateShape()
{
m_shape = ShapeFactory::CreateRandomShape();
int shift_time = ::rand() % 4;
while (shift_time-- > 0) {
Shift();
}
}
void Panel::_EraseLine(int minx, int maxx)
{
int count = 0;
for (int checkx = minx; checkx <= maxx; ++checkx) {
bool iserase = true;
for (int y = 0; y < PANEL_WIDTH; ++y) {
if (!m_occupied[checkx][y]) {
iserase = false;
break;
}
}
if (!iserase) {
continue;
}
++count;
for (int x = checkx - 1; x >= 0; --x) {
int isempty = true;
for (int y = 0; y < PANEL_WIDTH; ++y) {
if (m_occupied[x][y]) {
isempty = false;
}
m_occupied[x + 1][y] = m_occupied[x][y];
}
if (isempty) {
break;
}
}
}
if (count <= 0) {
return;
}
m_score += (7 * count + pow(3, count));
if (m_score >= ((m_level + 1) * 100 + 50 * (pow(2, m_level) - 1))) {
++m_level;
}
}