aboutsummaryrefslogtreecommitdiff
path: root/src/tetris
diff options
context:
space:
mode:
authorcurly <curlybryce@protonmail.com>2022-08-15 16:28:00 -0600
committercurly <curlybryce@protonmail.com>2022-08-15 16:28:00 -0600
commit02515663dd89994dbc987958b19d707a4cd44864 (patch)
tree08d12a78bed40dedd33e0180b0c1d70815ca8ec7 /src/tetris
parent462c1f8bf195a1e30b6c05be7a0ec60ee89d230c (diff)
downloadtetris-02515663dd89994dbc987958b19d707a4cd44864.tar.gz
tetris-02515663dd89994dbc987958b19d707a4cd44864.tar.bz2
tetris-02515663dd89994dbc987958b19d707a4cd44864.zip
Piece.area needs a rewrite again
Diffstat (limited to 'src/tetris')
-rw-r--r--src/tetris/piece.rs116
1 files changed, 85 insertions, 31 deletions
diff --git a/src/tetris/piece.rs b/src/tetris/piece.rs
index ca93c59..520de01 100644
--- a/src/tetris/piece.rs
+++ b/src/tetris/piece.rs
@@ -1,5 +1,5 @@
use rand::Rng;
-use std::ops::{AddAssign, Add};
+use std::ops::{AddAssign, Sub, Add};
use crate::tetris::Tetris;
#[derive(Copy)]
@@ -25,8 +25,17 @@ impl Add for Pos {
}
}
}
+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 UP: Pos = Pos(-1,0);
const DOWN: Pos = Pos(1,0);
const LEFT: Pos = Pos(0,-1);
const RIGHT: Pos = Pos(0,1);
@@ -36,8 +45,8 @@ pub enum Rotate {
Right
}
+#[derive(PartialEq)]
pub enum Dir {
- Up,
Down,
Left,
Right,
@@ -45,7 +54,6 @@ pub enum Dir {
impl Dir {
fn get(&self) -> Pos {
match self {
- Dir::Up => UP,
Dir::Down => DOWN,
Dir::Left => LEFT,
Dir::Right => RIGHT,
@@ -53,12 +61,49 @@ impl Dir {
}
}
-const NORMALL: [[i8; 3]; 3] = [[0,1,0],[0,1,0],[0,1,1]];
-const REVERSEL: [[i8; 3]; 3] = [[0,1,0],[0,1,0],[1,1,0]];
-const CUBE: [[i8; 3]; 3] = [[0,1,1],[0,1,1],[0,0,0]];
-const TEE: [[i8; 3]; 3] = [[0,1,0],[1,1,1],[0,0,0]];
-const DIAG: [[i8; 3]; 3] = [[0,1,1],[1,1,0],[0,0,0]];
-const REVERSEDIAG: [[i8; 3]; 3] = [[1,1,0],[0,1,1],[0,0,0]];
+const NORMALL: [[i8; 4]; 4] = [
+ [0,0,0,0],
+ [1,0,0,0],
+ [1,0,0,0],
+ [1,1,0,0],
+];
+const REVERSEL: [[i8; 4]; 4] = [
+ [0,0,0,0],
+ [0,0,0,1],
+ [0,0,0,1],
+ [0,0,1,1],
+];
+const CUBE: [[i8; 4]; 4] = [
+ [0,0,0,0],
+ [0,1,1,0],
+ [0,1,1,0],
+ [0,0,0,0],
+];
+const TEE: [[i8; 4]; 4] = [
+ [0,0,0,0],
+ [0,0,0,0],
+ [0,1,0,0],
+ [1,1,1,0],
+];
+const DIAG: [[i8; 4]; 4] = [
+ [0,0,0,0],
+ [0,0,0,0],
+ [1,1,0,0],
+ [0,1,1,0],
+];
+const REVERSEDIAG: [[i8; 4]; 4] = [
+ [0,0,0,0],
+ [0,0,0,0],
+ [0,0,1,1],
+ [0,1,1,0],
+];
+const STRAIGHT: [[i8; 4]; 4] = [
+ [1,0,0,0],
+ [1,0,0,0],
+ [1,0,0,0],
+ [1,0,0,0],
+];
+
pub enum Pieces {
NormalL,
@@ -67,23 +112,25 @@ pub enum Pieces {
Tee,
Diag,
ReverseDiag,
+ Straight,
}
impl Pieces {
pub fn random() -> Pieces {
use Pieces::*;
let mut rand = rand::thread_rng();
- match rand.gen_range(0..6) {
+ match rand.gen_range(0..7) {
0 => NormalL,
1 => ReverseL,
2 => Cube,
3 => Tee,
4 => Diag,
5 => ReverseDiag,
+ 6 => Straight,
_ => NormalL,
}
}
- fn get(&self) -> [[i8; 3]; 3] {
+ fn get(&self) -> [[i8; 4]; 4] {
use Pieces::*;
match &self {
NormalL => NORMALL,
@@ -92,46 +139,51 @@ impl Pieces {
Tee => TEE,
Diag => DIAG,
ReverseDiag => REVERSEDIAG,
+ Straight => STRAIGHT,
}
}
fn get_origin(&self) -> Pos {
use Pieces::*;
match &self {
- NormalL => Pos(0,0),
- ReverseL => Pos(0,0),
- Cube => Pos(0,0),
- Tee => Pos(0,0),
- Diag => Pos(0,0),
- ReverseDiag => Pos(0,0),
+ NormalL => Pos(3,0),
+ ReverseL => Pos(3,3),
+ Cube => Pos(1,1),
+ Tee => Pos(3,1),
+ Diag => Pos(2,1),
+ ReverseDiag => Pos(2,2),
+ Straight => Pos(3,0),
}
}
}
pub struct Piece {
- area: [[i8; 3]; 3], // A static 3x3 area
+ area: [[i8; 4]; 4], // A static 3x3 area
position: Pos,
origin: Pos,
alive: bool,
}
impl Piece {
- pub fn new(p: Pieces) -> Piece {
+ pub fn new(p: Pieces, pos: Pos) -> Piece {
+ let origin = p.get_origin();
Piece{
area: p.get(),
- position: Pos(0,5),
- origin: p.get_origin(),
+ position: pos + origin,
+ origin: origin,
alive: true,
}
}
// Return a random piece
// out of the enum Pieces
- pub fn random() -> Piece {
+ pub fn random(pos: Pos) -> Piece {
let piece = Pieces::random();
+ let origin = piece.get_origin();
+ dbg!(pos - origin);
Piece{
area: piece.get(),
- position: Pos(0,4),
- origin: piece.get_origin(),
+ position: pos - origin,
+ origin: origin,
alive: true,
}
}
@@ -145,10 +197,12 @@ impl Piece {
for pos in self.get_bits_pos() {
let new_pos = pos + dir.get();
- // Detect blocks and kill
- if grid.get_grid_pos(new_pos) == 1 {
+ // 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(())
@@ -195,7 +249,7 @@ impl Piece {
self.position
}
- pub fn get_area(&self) -> [[i8; 3]; 3] {
+ pub fn get_area(&self) -> [[i8; 4]; 4] {
self.area
}
@@ -224,7 +278,7 @@ mod tests {
#[test]
fn apply_down() {
- let mut p = Piece::new(Pieces::Cube);
+ let mut p = Piece::new(Pieces::Cube, Pos(0,0));
let d = Dir::Down;
p.apply_dir(&d);
@@ -235,7 +289,7 @@ mod tests {
#[test]
fn hit_test() {
- let mut p = Piece::new(Pieces::Cube);
+ let mut p = Piece::new(Pieces::Cube, Pos(0,0));
let d = Dir::Down;
let tetris = Tetris::new();
@@ -249,7 +303,7 @@ mod tests {
}
#[test]
fn hit_nothing_test() {
- let mut p = Piece::new(Pieces::Cube);
+ let mut p = Piece::new(Pieces::Cube, Pos(0,0));
let d = Dir::Down;
let tetris = Tetris::new();