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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
use rand::Rng;
use std::ops::{AddAssign, Sub, Add};
use crate::tetris::Tetris;
#[derive(Copy)]
#[derive(Clone)]
#[derive(PartialEq)]
#[derive(Debug)]
pub struct Pos(pub i8, pub i8);
impl AddAssign for Pos {
fn add_assign(&mut self, other: Self) {
*self = Self {
0: self.0 + other.0,
1: self.1 + other.1,
};
}
}
impl Add for Pos {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self {
0: self.0 + other.0,
1: self.1 + other.1,
}
}
}
impl Sub for Pos {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {
0: self.0 - other.0,
1: self.1 - other.1,
}
}
}
const NONE: Pos = Pos(0,0);
const DOWN: Pos = Pos(1,0);
const LEFT: Pos = Pos(0,-1);
const RIGHT: Pos = Pos(0,1);
#[derive(PartialEq)]
pub enum Rotate {
Left,
Right
}
#[derive(PartialEq)]
pub enum Dir {
None,
Down,
Left,
Right,
}
impl Dir {
fn get(&self) -> Pos {
match self {
Dir::None => NONE,
Dir::Down => DOWN,
Dir::Left => LEFT,
Dir::Right => RIGHT,
}
}
}
pub enum Pieces {
NormalL,
ReverseL,
Cube,
Tee,
Diag,
ReverseDiag,
Straight,
}
impl Pieces {
pub fn random() -> Pieces {
use Pieces::*;
let mut rand = rand::thread_rng();
match rand.gen_range(0..7) {
0 => NormalL,
1 => ReverseL,
2 => Cube,
3 => Tee,
4 => Diag,
5 => ReverseDiag,
6 => Straight,
_ => NormalL,
}
}
fn get(&self) -> Vec<Vec<i8>> {
let l: Vec<Vec<i8>> = vec![
// Outer most ring: top, right, bottom, left
vec![0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
// Inner rign: top, right, bottom, left
vec![0,1,0, 0, 0,1,1, 0],
// Center ring. Is used as the center of rotation
vec![1],
];
let r_l: Vec<Vec<i8>> = vec![
vec![0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
vec![0,1,0, 0, 1,1,0, 0],
vec![1],
];
let cube: Vec<Vec<i8>> = vec![
vec![0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
vec![0,1,1, 1, 0,0,0, 0],
vec![1],
];
let tee: Vec<Vec<i8>> = vec![
vec![0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
vec![0,1,0, 1, 0,0,0, 1],
vec![1],
];
let diag: Vec<Vec<i8>> = vec![
vec![0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
vec![1,1,0, 1, 0,0,0, 0],
vec![1],
];
let r_diag: Vec<Vec<i8>> = vec![
vec![0,0,0,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
vec![0,1,1, 0, 0,0,0, 1],
vec![1],
];
let straight: Vec<Vec<i8>> = vec![
vec![0,0,1,0,0, 0,0,0, 0,0,0,0,0, 0,0,0],
vec![0,1,0, 0, 0,1,0, 0],
vec![1],
];
use Pieces::*;
match &self {
NormalL => l,
ReverseL => r_l,
Cube => cube,
Tee => tee,
Diag => diag,
ReverseDiag => r_diag,
Straight => straight,
}
}
}
pub struct Piece {
area: Vec<Vec<i8>>, // A static 3x3 area
position: Pos,
alive: bool,
}
impl Piece {
// Return a random piece
// out of the enum Pieces
pub fn random(pos: Pos) -> Piece {
let piece = Pieces::random();
Piece{
area: piece.get(),
position: pos,
alive: true,
}
}
// Get area as a 2d array
fn get_area(&self) -> [[&i8; 5]; 5] {
let mut array = [[&0; 5]; 5];
for sections in &self.area {
let len = sections.len();
let mut y = [0; 16];
let mut x = [0; 16];
if len == 16 {
y = [0,0,0,0,0,1,2,3,4,4,4,4,4,3,2,1];
x = [0,1,2,3,4,4,4,4,4,3,2,1,0,0,0,0];
} else if len == 8 {
y = [1,1,1,2,3,3,3,2,0,0,0,0,0,0,0,0];
x = [1,2,3,3,3,2,1,1,0,0,0,0,0,0,0,0];
} else if len == 1 {
y = [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
x = [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
}
for c in 0..len {
array[y[c]][x[c]] = sections.get(c).expect("Invalid");
}
}
return array
}
// Using a grid and a direction;
// See if the new position would hit
// something in the grid
// If so, return Err
// otherwise return Ok
fn hit_detect(&mut self, dir: &Dir, grid: &Tetris) -> Result<(), ()> {
for pos in self.get_bits_pos() {
let new_pos = pos + dir.get();
// Detect blocks and kill only if Dir is down
if grid.get_grid_pos(new_pos) == 1 && *dir == Dir::Down {
self.kill();
return Err(())
} else if grid.get_grid_pos(new_pos) == 1 {
return Err(())
} else if new_pos.0 > 19 { // Detect bottom and kill
self.kill();
return Err(())
} else if new_pos.1 > 9 || new_pos.1 < 0 { // Detect sides
return Err(())
}
}
Ok(())
}
// Return false on kill
pub fn apply_to_grid(&mut self, grid: &mut Tetris) -> bool {
for pos in self.get_bits_pos() {
if pos.0 < 0 {
self.kill();
return false
} else {
grid.set_grid(pos, 1)
}
}
return true
}
fn apply_dir(&mut self, dir: &Dir) {
self.position += dir.get();
}
pub fn get_bits_pos(&self) -> Vec<Pos> {
let mut piece_pos_vec = vec![];
let mut y = -1;
for posy in self.get_area() {
let mut x = -1;
for posx in posy {
x += 1;
if posx == &1 {
piece_pos_vec.push(self.get_pos() + Pos(y, x))
}
}
y += 1;
}
return piece_pos_vec
}
pub fn rotate(&mut self, r: Rotate, tetris: &Tetris) {
let mut area = self.area.clone();
for section in &self.area {
let len = section.len();
let mut range: Vec<usize> = (0..len).collect();
if r == Rotate::Left {
let mut new = vec![];
for bit in range {
new.insert(0, bit);
}
range = new;
}
for c in range {
let mut loop_num = 0;
let mut loop_c = 4;
// Set to the inner loop
if len == 8 {
loop_num = 1;
loop_c = 2;
} else if len == 1 {
continue
}
// Right
if c < loop_c && r == Rotate::Right {
let x = area[loop_num].pop().expect("Out of Range");
area[loop_num].insert(0, x);
// Left
} else if c < loop_c && r == Rotate::Left {
let x = *area[loop_num].get(0).expect("Out of Range");
area[loop_num].remove(0);
area[loop_num].push(x);
}
}
}
let old_area = self.area.clone();
self.area = area;
match self.hit_detect(&Dir::None, tetris) {
Ok(()) => (),
Err(()) => self.area = old_area,
}
}
pub fn get_pos(&self) -> Pos {
self.position
}
pub fn set_pos(&mut self, p: Pos) {
self.position = p;
}
pub fn is_alive(&self) -> bool {
self.alive
}
fn kill(&mut self) {
self.alive = false
}
// Return false if the piece cannot move
pub fn r#move(&mut self, dir: Dir, grid: &Tetris) {
// If a hit is detected, don't move
// Otherwise move
if self.is_alive() {
match self.hit_detect(&dir, &grid) {
Ok(_) => self.apply_dir(&dir),
Err(_) => (),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn apply_down() {
let mut p = Piece::random(Pos(0,0));
let d = Dir::Down;
p.apply_dir(&d);
if p.get_pos() != Pos(1,0) {
panic!("{:?} did not move down properly", p.get_pos())
}
}
#[test]
fn hit_test() {
let mut p = Piece::random(Pos(30,0));
let d = Dir::Down;
let tetris = Tetris::new();
p.r#move(d, &tetris);
if p.is_alive() != false {
panic!("Piece did not die, is_alive == {}", p.is_alive())
}
}
#[test]
fn hit_nothing_test() {
let mut p = Piece::random(Pos(0,0));
let d = Dir::Down;
let tetris = Tetris::new();
p.r#move(d, &tetris);
if p.is_alive() == false {
panic!("Piece died, is_alive == {}", p.is_alive())
}
}
}
|