-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.cpp
123 lines (105 loc) · 3.17 KB
/
Shape.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
#include "GoToXY.h"
#include "TetrisBoard.h"
#include "Cube.h"
#include "Line.h"
int Shape::canTheShapeRotate(const TetrisBoard& board) {
int centerY = shape[2].getY(), centerX = shape[2].getX();
int x_new, y_new, x_old, y_old, center = centerX + centerY;
int left_flag = 0, shape_flag = 0, right_flag = 0;
for (int i = 0; i < SIZE; i++) {
x_old = shape[i].getX();
y_old = shape[i].getY();
x_new = center - y_old;
y_new = centerY - centerX + x_old;
//either a shape was encountered or a game wall
if (board.getCoord(x_new, y_new) || y_new > ROWS + Board_Gap || y_new < Board_Gap - 1) shape_flag++;
else if (x_new > COLUMNS) right_flag++;
else if (x_new < 1) left_flag++;
}
//shapes encounter has higher priority than the walls
if (shape_flag) return CANT_OTHER;
else if (right_flag) return CANT_RIGHT;
else if (left_flag) return CANT_LEFT;
else return CAN_MOVE;
}
int Shape::move(int direction, TetrisBoard& board) {
int degree = getDegree();
int x, check = 1, nextStep, flag = 0;
if (!(board.checkPos(this, direction) == TetrisBoard::FREE_SPACE))
return TetrisBoard::MOVE_FAIL;
for (int j = 0; j < SIZE; j++)
shape[j].draw(BLANK_SPACE);
if (direction != UP) { // the shape won't rotate
for (int j = 0; j < SIZE; j++)
shape[j].move(direction);
}
else {
check = canTheShapeRotate(board);
while (check == CANT_LEFT || check == CANT_RIGHT) {
//a dummy check to see if the shape can be moved in that direction
if (check == CANT_LEFT) {
for (int i = 0; i < SIZE; i++) {
nextStep = board.getCoord(shape[i].getX() + 1, shape[i].getY());
if (nextStep != 0) flag = 1;
}
}
else if (check == CANT_RIGHT) {
for (int i = 0; i < SIZE; i++) {
nextStep = board.getCoord(shape[i].getX() - 1, shape[i].getY());
if (nextStep != 0) flag = 1;
}
}
if (flag) // the shape can't move there because it'll overload another shape
break;
else //moves the shape in the direction so that it can rotate properly
{
if (check == CANT_LEFT) {
for (int i = 0; i < SIZE; i++)
shape[i].setX(shape[i].getX() + 1);
}
else if (check == CANT_RIGHT) {
for (int i = 0; i < SIZE; i++)
shape[i].setX(shape[i].getX() - 1);
}
}
check = canTheShapeRotate(board);
}
if (check==CAN_MOVE)
rotate(degree);
}
setTextColor(whichColor());
for (int j = 0; j < SIZE; j++)
shape[j].draw(getTexture());
return TetrisBoard::MOVED_SUCCESSFULLY;
}
void Shape::getMinMaxShape(int& minY, int& maxY) {
minY = maxY = shape[0].getY();
for (int i = 1; i < SIZE; i++) {
if (shape[i].getY() > minY)
minY = shape[i].getY();
if (shape[i].getY()< maxY)
maxY = shape[i].getY();
}
}
Color Shape::whichColor(int theShapeNum) const {
if (theShapeNum == DEFAULT_OBJ_COLOR)
theShapeNum = shapeType;
switch (theShapeNum) {
case CUBE:
return LIGHTMAGENTA;
case LINE:
return LIGHTCYAN;
case JOKER:
return YELLOW;
case BOMB:
return LIGHTRED;
case ZIGZAG:
return CYAN;
case TEE:
return LIGHTGREEN;
case GUN:
return LIGHTBLUE;
default:
return GREEN;
}
}