-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.cpp
More file actions
185 lines (155 loc) · 3.81 KB
/
Copy pathblock.cpp
File metadata and controls
185 lines (155 loc) · 3.81 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "block.h"
#include "gamegrid.h"
using namespace std;
const char piece_L[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', '*', ' '},
{' ', ' ', ' ', ' ', ' '}
};
const char piece_J[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', '*', '*', ' ', ' '},
{' ', ' ', ' ', ' ', ' '}
};
const char piece_I[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', ' ', ' '}
};
const char piece_T[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', '*', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', ' ', ' ', ' '}
};
const char piece_O[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', '*', ' '},
{' ', ' ', '*', '*', ' '},
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' '}
};
const char piece_S[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', ' ', '*', '*', ' '},
{' ', ' ', ' ', '*', ' '},
{' ', ' ', ' ', ' ', ' '}
};
const char piece_Z[5][5] = {
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', '*', ' ', ' '},
{' ', '*', '*', ' ', ' '},
{' ', '*', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' '}
};
Block::Block(int _y, int _x, int pieceId) {
y = _y;
x = _x;
switch (pieceId) {
case 0:
copy_pieces(data, piece_L);
break;
case 1:
copy_pieces(data, piece_J);
break;
case 2:
copy_pieces(data, piece_I);
break;
case 3:
copy_pieces(data, piece_T);
break;
case 4:
copy_pieces(data, piece_O);
break;
case 5:
copy_pieces(data, piece_S);
break;
case 6:
copy_pieces(data, piece_Z);
break;
}
}
void Block::copy_pieces(char (&dest)[5][5], const char (&src)[5][5]) {
for (int i=0; i < 5; i++){
for (int j=0; j < 5; j++){
data[i][j] = src[i][j];
}
}
}
char Block::get(int row, int col) {
return data[row][col];
}
void Block::rotateCW() {
char temp_block[5][5];
for (int i =0;i < 5; i++){
for (int j =0; j < 5; j++){
temp_block[j][4-i]= data[i][j] ;
}
}
for (int i =0;i < 5; i++){
for (int j =0; j < 5; j++){
data[i][j] = temp_block[i][j] ;
}
}
}
void Block::rotateCCW() {
//create a tempoary array here bc u want it destroyed after fuction is done
char temp_block[5][5];
//rotating block to a temp block
for (int i =0; i < 5; i++){
for (int j =0; j < 5; j++){
temp_block[4-j][i]= data[i][j] ;
}
}
//copying tempblock to block
for (int i =0; i < 5; i++){
for (int j =0; j < 5; j++){
data[i][j] = temp_block[i][j] ;
}
}
}
void Block::print(ostream& out) {
for ( int i = 0; i < 5; i++ ) {
for ( int j = 0; j < 5; j++ ) {
out << data[i][j] << " ";
}
out << endl;
}
}
bool Block::hasCollisionOccurred(GameGrid& grid ){
//has a collision occured
for (int j = 0;j < 5 ; j++)
{
for (int i = 0;i < 5;i++)
{
if (data[j][i] == '*' && i+x < 0)
{
return true;
}
if (data [j][i] == '*' && j+y < 0)
{
return true;
}
if ( grid.data[j+y][i+x] == '*' && data[j][i] == '*') {
return true;
}
if (data[j][i] == '*' && i+x > 9)
{
return true;
}
if (data[j][i] == '*' && j+y > 19)
{
return true;
}
}
}
return false;
}