/
board.cpp
122 lines (107 loc) · 2.07 KB
/
board.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
#include "board.h"
Board::Board ()
{
Board::SetSize ();
}
Board::~Board ()
{
board.clear ();
}
void Board::SetSize (int height, int width)
{
BoardHeight = height;
BoardWidth = width;
board.resize (BoardHeight);
for (int i = 0; i < BoardHeight; ++i)
board[i].resize (BoardWidth, 0);
}
void Board::DeleteFull (int number)
{
for (int i = number; i > 0; --i)
for (int j = 0; j < BoardWidth; ++j)
swap (board[i][j], board[i - 1][j]);
board[0].assign (BoardWidth, 0);
}
void Board::FindFull ()
{
bool full;
for (int i = 0; i < BoardHeight; ++i)
{
full = true;
for (int j = 0; j < BoardWidth; ++j)
if(board[i][j] == 0) {
full = false;
break;
}
if (full)
DeleteFull (i);
}
}
bool Board::CreateNewFigure ()
{
current.SetFigure (current.RandomType ());
curY = BoardWidth / 2;
curX = -current.MinX ();
if (!CheckMove (current, curX, curY)) {
finished = true;
return false;
}
else {
finished = false;
return true;
}
}
void Board::LineDown ()
{
if (CheckMove (current, curX+1, curY))
curX++;
else {
AddToBoard ();
}
}
void Board::Drop ()
{
while(!finished)
LineDown ();
}
bool Board::Finished ()
{
return finished;
}
bool Board::CheckMove (const Figure & figure, int newX, int newY)
{
for (int i=0; i<4; ++i) {
int x = newX + figure.X(i);
int y = newY + figure.Y(i);
if (x < 0 || x >= BoardHeight || y < 0 || y >= BoardWidth || board[x][y] != 0)
return false;
}
return true;
}
void Board::AddToBoard ()
{
for (int i = 0; i < 4; ++i)
board[curX + current.X(i)][curY + current.Y(i)] = current.Color ();
}
void Board::RotateCCW ()
{
current.RotateCCW ();
if (!CheckMove(current, curX, curY))
current.RotateCW ();
}
void Board::RotateCW ()
{
current.RotateCW ();
if (!CheckMove(current, curX, curY))
current.RotateCCW ();
}
void Board::MoveLeft ()
{
if (CheckMove(current, curX, curY-1))
--curY;
}
void Board::MoveRight ()
{
if (CheckMove(current, curX, curY+1))
++curY;
}