-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.cc
executable file
·137 lines (113 loc) · 2.8 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
#include "Block.h"
#include "Board.h"
#include "Coords.h"
#include <vector>
#include <iostream>
using namespace std;
Block::Block(Board* b, int level):theBoard{b}, level{level},
presentConfig{1} {}
Block::~Block() {}
void Block::updateBoard(){ // update cells on the board and notify dispalys
for(int i = 0; i < theCoords.size(); ++i){
theBoard->updateCell(theCoords[i].getX(),theCoords[i].getY(), blockType);
}
}
void Block::moveLeft(){
int len = theCoords.size();
// clear its present position from the board
clearCoordsOnBoard();
if(isLeftFree()){
// move it left
for(int i = 0; i < len; ++i){
theCoords[i].decY();
}
}
updateBoard();
}
void Block::moveRight(){
int len = theCoords.size();
// clear its present position from the board
clearCoordsOnBoard();
if(isRightFree()){
// move it right
for(int i = 0; i < len; ++i){
theCoords[i].incY();
}
}
updateBoard();
}
void Block::moveDown(){
int len = theCoords.size();
// clear its present position from the board
clearCoordsOnBoard();
if(isDownFree()){
// move it left
for(int i = 0; i < len; ++i){
theCoords[i].incX();
}
}
updateBoard();
}
void Block::drop(){
clearCoordsOnBoard();
while(true){
if(!isDownFree()) break;
for(int i = 0; i < theCoords.size(); ++i){
theCoords[i].incX();
}
}
updateBoard();
theBoard->addToAliveBlocks(this);
}
char Block::getBlockType() { return blockType; }
// methods to check whether cells at specific coordinates on board are
// free or not
bool Block::isLeftFree(){
int len = theCoords.size();
for(int i = 0; i < len; ++i){
Coords c {theCoords[i].getX(), theCoords[i].getY()-1};
if(!(theBoard->isFree(c))) return false;
}
return true;
}
bool Block::isRightFree(){
int len = theCoords.size();
for(int i = 0; i < len; ++i){
Coords c {theCoords[i].getX(), theCoords[i].getY()+1};
if(!(theBoard->isFree(c))) return false;
}
return true;
}
bool Block::isDownFree(){
int len = theCoords.size();
for(int i = 0; i < len; ++i){
Coords c {theCoords[i].getX()+1, theCoords[i].getY()};
if(!(theBoard->isFree(c))) return false;
}
return true;
}
// remove old coordinates of the block from the vector
void Block::removeCoord(Coords toRemove) {
for(int i = 0; i < theCoords.size(); ++i){
if(toRemove == theCoords[i]){
theCoords.erase(theCoords.begin() + i);
break;
}
}
}
int Block::numCoordsOccupied() { return theCoords.size(); }
bool Block::isCoordsFreeOnBoard() {
int len = theCoords.size();
for(int i = 0; i < len; ++i){
if(!theBoard->isFree(theCoords[i])) return false;
}
return true;
}
void Block::clearCoordsOnBoard(){
int len = theCoords.size();
for(int i = 0; i < len; ++i){
theBoard->updateCell(theCoords[i].getX(), theCoords[i].getY(), ' ');
}
}
int Block::getBlockLevel(){ return level; }
void Block::removeItFromBoard(){ clearCoordsOnBoard(); }