Compare commits
No commits in common. "6a780efa4a3ac02529baaee9a69899a38c624456" and "773d7d2cc562c11c742b673d6d21494a4c341bd0" have entirely different histories.
6a780efa4a
...
773d7d2cc5
@ -6,9 +6,6 @@ use iced::{
|
|||||||
use iced_native::{event, subscription, Event};
|
use iced_native::{event, subscription, Event};
|
||||||
use rand::distributions::{Distribution, Standard};
|
use rand::distributions::{Distribution, Standard};
|
||||||
|
|
||||||
const START_LOCATION: (u8, u8) = (5, 1);
|
|
||||||
type PieceBlocks = [(i8, i8); 4];
|
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
Tetris::run(Settings::default())
|
Tetris::run(Settings::default())
|
||||||
}
|
}
|
||||||
@ -18,7 +15,6 @@ struct Tetris {
|
|||||||
blocks: BlockGrid,
|
blocks: BlockGrid,
|
||||||
ticks: usize,
|
ticks: usize,
|
||||||
paused: bool,
|
paused: bool,
|
||||||
lines_removed: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tetris {
|
impl Tetris {
|
||||||
@ -28,7 +24,6 @@ impl Tetris {
|
|||||||
blocks: BlockGrid::new(),
|
blocks: BlockGrid::new(),
|
||||||
ticks: 0,
|
ticks: 0,
|
||||||
paused: false,
|
paused: false,
|
||||||
lines_removed: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,9 +49,7 @@ impl Application for Tetris {
|
|||||||
self.ticks += 1
|
self.ticks += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::Up => {
|
Message::Up => (),
|
||||||
self.blocks.rotate_active_piece();
|
|
||||||
}
|
|
||||||
Message::Down => {
|
Message::Down => {
|
||||||
self.blocks.move_active_piece(MoveDirection::HardDrop);
|
self.blocks.move_active_piece(MoveDirection::HardDrop);
|
||||||
}
|
}
|
||||||
@ -79,9 +72,6 @@ impl Application for Tetris {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let lines_removed = self.blocks.clear_pieces();
|
|
||||||
self.lines_removed += lines_removed;
|
|
||||||
Command::none()
|
Command::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +164,6 @@ struct BlockGrid {
|
|||||||
struct ActivePiece {
|
struct ActivePiece {
|
||||||
location: (u8, u8),
|
location: (u8, u8),
|
||||||
tetromino: Tetromino,
|
tetromino: Tetromino,
|
||||||
orientation: Orientation,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ActivePiece {
|
impl ActivePiece {
|
||||||
@ -183,7 +172,6 @@ impl ActivePiece {
|
|||||||
let (cur_x, cur_y) = self.location;
|
let (cur_x, cur_y) = self.location;
|
||||||
ActivePiece {
|
ActivePiece {
|
||||||
tetromino: self.tetromino,
|
tetromino: self.tetromino,
|
||||||
orientation: self.orientation,
|
|
||||||
location: match direction {
|
location: match direction {
|
||||||
Left => (cur_x.checked_sub(1).unwrap_or(0), cur_y),
|
Left => (cur_x.checked_sub(1).unwrap_or(0), cur_y),
|
||||||
Right => (cur_x + 1, cur_y),
|
Right => (cur_x + 1, cur_y),
|
||||||
@ -192,19 +180,6 @@ impl ActivePiece {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rotate_piece(&self) -> ActivePiece {
|
|
||||||
ActivePiece {
|
|
||||||
tetromino: self.tetromino,
|
|
||||||
location: self.location,
|
|
||||||
orientation: match self.orientation {
|
|
||||||
Orientation::A => Orientation::B,
|
|
||||||
Orientation::B => Orientation::C,
|
|
||||||
Orientation::C => Orientation::D,
|
|
||||||
Orientation::D => Orientation::A,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockGrid {
|
impl BlockGrid {
|
||||||
@ -220,12 +195,11 @@ impl BlockGrid {
|
|||||||
fn drop_piece(&mut self, tetromino: Tetromino) -> bool {
|
fn drop_piece(&mut self, tetromino: Tetromino) -> bool {
|
||||||
if let None = self.active_piece {
|
if let None = self.active_piece {
|
||||||
let piece = ActivePiece {
|
let piece = ActivePiece {
|
||||||
location: START_LOCATION,
|
location: (4, 0),
|
||||||
tetromino,
|
tetromino,
|
||||||
orientation: Orientation::A,
|
|
||||||
};
|
};
|
||||||
let piece_blocks = Self::piece_blocks(&piece);
|
let piece_blocks = Self::piece_blocks(&piece);
|
||||||
if self.piece_blocks_in_bounds(piece_blocks) {
|
if self.piece_blocks_in_bounds(&piece_blocks) {
|
||||||
self.active_piece = Some(piece);
|
self.active_piece = Some(piece);
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@ -236,11 +210,11 @@ impl BlockGrid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn piece_blocks_in_bounds(&self, piece_blocks: PieceBlocks) -> bool {
|
fn piece_blocks_in_bounds(&self, piece_blocks: &[(u8, u8); 4]) -> bool {
|
||||||
piece_blocks.iter().all(|(x, y)| {
|
piece_blocks.iter().all(|(x, y)| {
|
||||||
let x = *x;
|
let x = *x;
|
||||||
let y = *y;
|
let y = *y;
|
||||||
x >= 0 && y >= 0 && x < 10 && y < 20 && self.state[x as usize][y as usize].is_none()
|
x < 10 && y < 20 && self.state[x as usize][y as usize].is_none()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,7 +227,7 @@ impl BlockGrid {
|
|||||||
(Some(piece), MoveDirection::Left | MoveDirection::Right) => {
|
(Some(piece), MoveDirection::Left | MoveDirection::Right) => {
|
||||||
let new_piece = piece.move_piece(&direction);
|
let new_piece = piece.move_piece(&direction);
|
||||||
let new_blocks = Self::piece_blocks(&new_piece);
|
let new_blocks = Self::piece_blocks(&new_piece);
|
||||||
if self.piece_blocks_in_bounds(new_blocks) {
|
if self.piece_blocks_in_bounds(&new_blocks) {
|
||||||
self.active_piece = Some(new_piece);
|
self.active_piece = Some(new_piece);
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@ -265,7 +239,7 @@ impl BlockGrid {
|
|||||||
loop {
|
loop {
|
||||||
let p = new_piece.move_piece(&MoveDirection::SoftDrop);
|
let p = new_piece.move_piece(&MoveDirection::SoftDrop);
|
||||||
let new_blocks = Self::piece_blocks(&p);
|
let new_blocks = Self::piece_blocks(&p);
|
||||||
if self.piece_blocks_in_bounds(new_blocks) {
|
if self.piece_blocks_in_bounds(&new_blocks) {
|
||||||
new_piece = p;
|
new_piece = p;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -279,7 +253,7 @@ impl BlockGrid {
|
|||||||
(Some(ref piece), MoveDirection::SoftDrop) => {
|
(Some(ref piece), MoveDirection::SoftDrop) => {
|
||||||
let new_piece = piece.move_piece(&MoveDirection::SoftDrop);
|
let new_piece = piece.move_piece(&MoveDirection::SoftDrop);
|
||||||
let new_blocks = Self::piece_blocks(&new_piece);
|
let new_blocks = Self::piece_blocks(&new_piece);
|
||||||
if self.piece_blocks_in_bounds(new_blocks) {
|
if self.piece_blocks_in_bounds(&new_blocks) {
|
||||||
self.active_piece = Some(new_piece);
|
self.active_piece = Some(new_piece);
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@ -290,22 +264,6 @@ impl BlockGrid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rotate_active_piece(&mut self) -> bool {
|
|
||||||
let active = self.active_piece;
|
|
||||||
if let Some(piece) = active {
|
|
||||||
let new_piece = piece.rotate_piece();
|
|
||||||
let new_blocks = Self::piece_blocks(&new_piece);
|
|
||||||
if self.piece_blocks_in_bounds(new_blocks) {
|
|
||||||
self.active_piece = Some(new_piece);
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove the currently active piece and place its blocks onto the board.
|
/// Remove the currently active piece and place its blocks onto the board.
|
||||||
fn place_active_piece(&mut self) {
|
fn place_active_piece(&mut self) {
|
||||||
if let Some(piece) = self.active_piece {
|
if let Some(piece) = self.active_piece {
|
||||||
@ -319,82 +277,29 @@ impl BlockGrid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn piece_blocks(piece: &ActivePiece) -> PieceBlocks {
|
fn piece_blocks(piece: &ActivePiece) -> [(u8, u8); 4] {
|
||||||
use Orientation::*;
|
|
||||||
use Tetromino::*;
|
use Tetromino::*;
|
||||||
let (x, y) = piece.location;
|
let (x, y) = piece.location;
|
||||||
let (x, y) = (x as i8, y as i8);
|
|
||||||
|
|
||||||
// These use the "Original Rotation System" cf. Tetris wiki
|
match piece.tetromino {
|
||||||
match (piece.tetromino, piece.orientation) {
|
I => [(x, y), (x, y + 1), (x, y + 2), (x, y + 3)],
|
||||||
(I, A | C) => [(x - 2, y), (x - 1, y), (x, y), (x + 1, y)],
|
J => [(x, y), (x, y + 1), (x, y + 2), (x - 1, y + 2)],
|
||||||
(I, B | D) => [(x, y - 1), (x, y), (x, y + 1), (x, y + 2)],
|
L => [(x, y), (x, y + 1), (x, y + 2), (x + 1, y + 2)],
|
||||||
(J, A) => [(x - 1, y), (x, y), (x + 1, y), (x + 1, y + 1)],
|
O => [(x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)],
|
||||||
(J, B) => [(x, y), (x + 1, y), (x, y + 1), (x, y + 2)],
|
S => [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 2, y)],
|
||||||
(J, C) => [(x - 1, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1)],
|
T => [(x, y), (x + 1, y), (x + 2, y), (x + 1, y + 1)],
|
||||||
(J, D) => [(x, y), (x, y + 1), (x, y + 2), (x - 1, y + 2)],
|
Z => [(x, y), (x + 1, y), (x + 1, y + 1), (x + 2, y + 1)],
|
||||||
(L, A) => [(x - 1, y + 1), (x - 1, y), (x, y), (x + 1, y)],
|
|
||||||
(L, B) => [(x, y), (x, y + 1), (x, y + 2), (x + 1, y + 2)],
|
|
||||||
(L, C) => [(x - 1, y + 1), (x, y + 1), (x + 1, y + 1), (x + 1, y)],
|
|
||||||
(L, D) => [(x - 1, y), (x, y), (x, y + 1), (x, y + 2)],
|
|
||||||
(O, _) => [(x, y), (x, y + 1), (x - 1, y), (x - 1, y + 1)],
|
|
||||||
(S, A | C) => [(x - 1, y + 1), (x, y + 1), (x, y), (x + 1, y)],
|
|
||||||
(S, B | D) => [(x, y), (x, y + 1), (x + 1, y + 1), (x + 1, y + 2)],
|
|
||||||
(T, A) => [(x - 1, y), (x, y), (x + 1, y), (x, y + 1)],
|
|
||||||
(T, B) => [(x, y), (x, y + 1), (x + 1, y + 1), (x, y + 2)],
|
|
||||||
(T, C) => [(x, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1)],
|
|
||||||
(T, D) => [(x - 1, y + 1), (x, y), (x, y + 1), (x, y + 2)],
|
|
||||||
(Z, A | C) => [(x - 1, y + 1), (x, y + 1), (x, y + 2), (x + 1, y + 2)],
|
|
||||||
(Z, B | D) => [(x, y), (x, y + 1), (x + 1, y), (x + 1, y - 1)],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns number of lines removed
|
fn active_piece_blocks(&self) -> Option<(Tetromino, [(u8, u8); 4])> {
|
||||||
fn clear_pieces(&mut self) -> u32 {
|
self.active_piece
|
||||||
let mut new_state = [[None; 20]; 10];
|
.as_ref()
|
||||||
let mut offset = 0;
|
.map(|piece| (piece.tetromino, Self::piece_blocks(&piece)))
|
||||||
for row in (0..=19).rev() {
|
|
||||||
let mut all_filled = true;
|
|
||||||
let mut all_empty = true;
|
|
||||||
for col in 0..10 {
|
|
||||||
if self.state[col][row].is_none() {
|
|
||||||
all_filled = false;
|
|
||||||
}
|
|
||||||
if self.state[col][row].is_some() {
|
|
||||||
all_empty = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if all_filled && !all_empty {
|
|
||||||
offset += 1;
|
|
||||||
} else {
|
|
||||||
if offset != 0 {
|
|
||||||
println!("Offset {} row {}", offset, row);
|
|
||||||
}
|
|
||||||
for col in 0..10 {
|
|
||||||
new_state[col][row+offset] = self.state[col][row];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.state = new_state;
|
|
||||||
println!("OFFSET: {}", offset);
|
|
||||||
offset as u32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter<'a>(&'a self) -> BlockGridIter<'a> {
|
fn iter<'a>(&'a self) -> BlockGridIter<'a> {
|
||||||
let active_piece_blocks = self.active_piece.as_ref().map(|piece| {
|
BlockGridIter::new(&self.state, self.active_piece_blocks())
|
||||||
let b = Self::piece_blocks(piece);
|
|
||||||
let blocks = [
|
|
||||||
(b[0].0 as u8, b[0].1 as u8),
|
|
||||||
(b[1].0 as u8, b[1].1 as u8),
|
|
||||||
(b[2].0 as u8, b[2].1 as u8),
|
|
||||||
(b[3].0 as u8, b[3].1 as u8),
|
|
||||||
];
|
|
||||||
|
|
||||||
(piece.tetromino, blocks)
|
|
||||||
});
|
|
||||||
BlockGridIter::new(&self.state, active_piece_blocks)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -450,7 +355,7 @@ struct Block {
|
|||||||
source: Tetromino,
|
source: Tetromino,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
enum MoveDirection {
|
enum MoveDirection {
|
||||||
Left,
|
Left,
|
||||||
Right,
|
Right,
|
||||||
@ -458,14 +363,6 @@ enum MoveDirection {
|
|||||||
SoftDrop,
|
SoftDrop,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
enum Orientation {
|
|
||||||
A,
|
|
||||||
B,
|
|
||||||
C,
|
|
||||||
D,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
enum Tetromino {
|
enum Tetromino {
|
||||||
I,
|
I,
|
||||||
|
Loading…
Reference in New Issue
Block a user