-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisBoard.cpp
More file actions
152 lines (129 loc) · 3.26 KB
/
TetrisBoard.cpp
File metadata and controls
152 lines (129 loc) · 3.26 KB
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
#include "TetrisBoard.h"
#include "GoToXY.h"
int TetrisBoard::checkPos(const Shape* current, int direction){
int x, y;
switch (direction){
case Shape::LEFT:
for (int i = 0; i < current->SIZE;i++) {
x = current->shape[i].getX() - 1;
y = current->shape[i].getY();
if (x == 0)
return BOTTOM_ENCOUNTER;
else if(Board[y - Board_Gap][x - 1] != 0 && current->getShape()!=Shape::JOKER)
return SHAPE_ENCOUNTER;
}
break;
case Shape::RIGHT:
for (int i = 0; i < current->SIZE;i++) {
x = current->shape[i].getX() + 1;
y = current->shape[i].getY();
if (x == 11)
return BOTTOM_ENCOUNTER;
else if (Board[y - Board_Gap][x - 1] != 0 && current->getShape() != Shape::JOKER)
return SHAPE_ENCOUNTER;
}
break;
case Shape::DOWN:
for (int i = 0; i < current->SIZE;i++) {
x = current->shape[i].getX();
y = current->shape[i].getY() + 1;
if (y == ROWS+Board_Gap)
return BOTTOM_ENCOUNTER;
else if(Board[y - Board_Gap][x - 1] != 0)
return SHAPE_ENCOUNTER;
}
break;
}
return FREE_SPACE;
}
bool TetrisBoard::checkBoard(int _x, int _y) const
{
if (getCoord(_x, _y))
return TRUE;
else
return FALSE;
}
bool TetrisBoard::checkLine(int currentY){
for (int x = 1; x <= COLUMNS; x++){
if (!checkBoard(x, currentY))
return false;
}
return true;
}
int TetrisBoard::deleteLines(const Shape* current, int minY, int maxY){
int currentY = minY, howManyDel = 0, temp;
while (currentY - maxY >= 0){
if (checkLine(currentY)) // need to delete the relatively lowest line for the shape
{
gotoxy(1, currentY);
for (int i = 1; i <= COLUMNS; i++){
setCoord(i, currentY, 0);
cout << " ";
}
for (int y = currentY; y > Board_Gap; y--) // updating the board
{
for (int x = 1; x <= COLUMNS; x++){
temp = getCoord(x, y-1);
setCoord(x, y, temp);
}
}
Sleep(250);
printBoard(currentY, current);
howManyDel++;
maxY++;
}
else // check lines above the relatively lowest line
currentY--;
}
return howManyDel;
}
void TetrisBoard::setBoard() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
Board[i][j] = 0;
}
}
}
int TetrisBoard::getCoord(int _x, int _y) const
{
if (_y < Board_Gap || _x > ROWS + Board_Gap)
return 0;
else
return Board[_y - Board_Gap][_x - 1];
}
bool TetrisBoard::checkEndGame() {
for (int i = 0; i < ROWS; i++) {
if (Board[0][i] != 0)
return TRUE;
}
return FALSE;
}
void TetrisBoard::updateBoard(Shape* current) {
int x, y;
for (int i = 0; i < current->SIZE; i++) {
x = current->shape[i].getX();
y = current->shape[i].getY();
Board[y - Board_Gap][x - 1] = current->getShape();
}
}
void TetrisBoard::printBoard(int currentY, const Shape* current) {
int texture;
for (int y = currentY; y > Board_Gap; y--) {
for (int x = 1; x <= COLUMNS; x++) {
gotoxy(x, y);
texture = getCoord(x, y);
setTextColor(current->whichColor(texture));
switch (texture) {
case Shape::JOKER:
cout << "X";
break;
case Shape::LINE : case Shape::CUBE : case Shape::GUN: case Shape::ZIGZAG : case Shape::TEE:
cout << "%";
break;
default:
cout << " ";
break;
}
}
}
}