-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.cpp
207 lines (199 loc) · 4.35 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Created by kali on 2/28/22.
//
#include <random>
#include <chrono>
#include "Shape.h"
constexpr static Shape::Square ROTATIONS[Shape::Piece::SIZE/*Piece*/][/*Rotation*/4][Shape::N_SQUARES] = {
{
{
{0, 0},
{1, 0},
{0, 1},
{1, 1}
}
},{
{
{-2,0},
{-1,0},
{0,0},
{1,0},
},
{
{0, -2},
{0,-1},
{0,0},
{0,1}
},
{
{-2, -1},
{-1,-1},
{0,-1},
{1,-1}
},
{
{-1, -2},
{-1,-1},
{-1,0},
{-1,1}
},
}
};
Shape::Piece randomPiece()
{
// stack overflow said to use this.
auto random(std::mt19937(std::chrono::steady_clock::now().time_since_epoch().count()));
std::uniform_int_distribution<int> distribution(Shape::Piece::O, Shape::Piece::T);
return static_cast<Shape::Piece>(distribution(random));
}
Shape::Shape()
: Shape( randomPiece() )
{}
Shape::Square Shape::getStartingPos() const {
Square min = {4,4};
for(int i=0; i<Shape::N_SQUARES;i++) {
Shape::Square s = shape[i];
if(s.x < min.x) min.x = s.x;
if(s.y < min.y) min.y = s.y;
}
min.x *= -1;
min.y *= -1;
return min;
}
// COLOR PATTERN
// 001 O
// 010 I
// 011 J
// 100 L
// 101 Z
// 110 S
// 111 Ts
Shape::Shape(Piece type)
: piece(type)
{
// define either a set number of rotations that are manually defined, or the initial shape
switch(type)
{
// /(^ㅅ^)\ Square
case O:
{
color = 0xFFFF00;
nRotations = 1;
break;
}
// /(^ㅅ^)\ Line
case I:
{
color = 0xFF7B31;
nRotations = 4;
break;
}
// /(^ㅅ^)\ J
case J:
{
color = 0x00EEFF;
/* X O X
X */
shape = new Square[4] {
{-1, 0},
{0, 0},
{1, 0},
{1, 1}
};
break;
}
// /(^ㅅ^)\ L
case L:
{
color = 0xFF0000;
// X O X
// X
shape = new Square[4]{
{-1,+1},
{-1,+0},
{+0,+0},
{+1,+0}
};
break;
}
// /(^ㅅ^)\ Z
case Z:
{
color = 0xFFA508;
shape = new Square[4] {
{-1, -1},
{0, -1},
{0, 0},
{1, 0},
};
break;
}
// /(^ㅅ^)\ S
case S:
{
color = 0xFF00FF;
shape = new Square[4] {
{-1, 0},
{0, 0},
{0, -1},
{1, -1},
};
break;
}
// /(^ㅅ^)\ T
case T:
{
color = 0x69FF69;
shape = new Square[4] {
{-1, 0},
{0, 0},
{0, -1},
{1, 0},
};
break;
}
}
if(nRotations > 0) {
shape = new Square[4];
for(int r = 0; r < nRotations; r++)
{
for(int s = 0; s < N_SQUARES; s++)
{
rotations[r][s] = ROTATIONS[type][r][s];
if(r == 0) shape[s] = rotations[r][s];
}
}
}
}
bool Shape::move(int dx, int dy) {
x += dx;
y += dy;
if ( isInvalidState() ){
x -= dx;
y -= dy;
return false;
}
return true;
}
bool Shape::rotate(int _x, int _y, bool force) {
if(nRotations == 0) {
for(int i=0; i < N_SQUARES; i++) {
shape[i] = {
_x * shape[i].y % N_SQUARES,
_y * shape[i].x % N_SQUARES
};
}
} else {
nRotation += _x + nRotations;
nRotation %= nRotations;
auto rm = rotations[nRotation];
for(int i = 0; i < N_SQUARES; i++)
shape[i] = rm[i];
}
if(!force && isInvalidState()) {
// note that if the initial state was invalid this will crash the game (exit code 11)
rotate(-_x,-_y, true);
return false;
}
return true;
}