-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.cc
136 lines (126 loc) · 2.53 KB
/
block.cc
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
#include "block.h"
#include "posn.h"
#include <iostream>
using namespace std;
Block::Block(char type,int difficulty):type(type){
this->difficulty=difficulty;
blockNum=4;
p = new Posn[4];
if(type=='I'){
p[0] = Posn(3,0);
p[1] = Posn(3,1);
p[2] = Posn(3,2);
p[3] = Posn(3,3);
}
else if(type=='J'){
p[0] = Posn(3,0);
p[1] = Posn(4,0);
p[2] = Posn(4,1);
p[3] = Posn(4,2);
}
else if(type=='L'){
p[0] = Posn(3,2);
p[1] = Posn(4,0);
p[2] = Posn(4,1);
p[3] = Posn(4,2);
}
else if(type=='O'){
p[0] = Posn(3,0);
p[1] = Posn(3,1);
p[2] = Posn(4,0);
p[3] = Posn(4,1);
}
else if(type=='S'){
p[0] = Posn(3,1);
p[1] = Posn(3,2);
p[2] = Posn(4,0);
p[3] = Posn(4,1);
}
else if(type=='Z'){
p[0] = Posn(3,0);
p[1] = Posn(3,1);
p[2] = Posn(4,1);
p[3] = Posn(4,2);
}
else{
p[0] = Posn(4,1);
p[1] = Posn(3,0);
p[2] = Posn(3,1);
p[3] = Posn(3,2);
}
}
Block::~Block(){
delete []p;
}
char Block::getType(){ return type; }
void Block::move(char dir){
for(int i=0; i<4; i++){
if(dir == 'l') p[i].y -= 1;
else if(dir == 'r') p[i].y += 1;
else p[i].x += 1;
}
}
bool Block::isCleared(){
if (blockNum==0){
return true;
} else {
return false;
}
}
void Block::notify(){
blockNum--;
}
Posn* Block::getP(){ return p; }
Posn* Block::rotatedP(string dir){
int xMaxBefore = -100;
int yMinBefore = 100;
int xMaxAfter = -100;
int yMinAfter = 100;
int temp, xDist, yDist;
Posn *newP = new Posn[4];
for(int i=0; i<4; i++){
newP[i] = p[i];
}
//find xMaxBefore and yMinBefore:
for(int i=0; i<4; i++){
xMaxBefore = max(newP[i].x, xMaxBefore);
yMinBefore = min(newP[i].y, yMinBefore);
}
//find new Posns after rotation:
if(dir == "clockwise"){
for(int i=0; i<4; i++){
temp = newP[i].x;
newP[i].x = newP[i].y;
newP[i].y = -temp;
}
}
else{
for(int i=0; i<4; i++){
temp = newP[i].x;
newP[i].x = -newP[i].y;
newP[i].y = temp;
}
}
//find xMaxAfter and yMinAfter:
for(int i=0; i<4; i++){
xMaxAfter = max(newP[i].x, xMaxAfter);
yMinAfter = min(newP[i].y, yMinAfter);
}
//compute xDist and yDist:
xDist = xMaxAfter - xMaxBefore;
yDist = yMinAfter - yMinBefore;
//put the Posns back to the correct place:
for(int i=0; i<4; i++){
newP[i].x -= xDist;
newP[i].y -= yDist;
}
return newP;
}
void Block::setP(Posn *newPosn){
for(int i=0; i<4; i++){
p[i] = newPosn[i];
}
}
int Block::getDifficulty(){
return difficulty;
}