diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index ac0f60a..6b01dd6 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.72.0-bullseye +FROM mcr.microsoft.com/vscode/devcontainers/rust:buster ENV DEBIAN_FRONTEND noninteractive diff --git a/src/bin/client.rs b/src/bin/client.rs index bf81547..23d5162 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -26,7 +26,7 @@ async fn main() { debug: &debug, }; let mut open_settings = false; - let mut game = Game::new(); + let mut game = Game::default(); loop { let block_size_temp = (screen_height() / (game.board.positions.len() as f32 * 1.25)) diff --git a/src/core/board.rs b/src/core/board.rs index 1cae224..516f7cc 100644 --- a/src/core/board.rs +++ b/src/core/board.rs @@ -10,6 +10,8 @@ use super::{ generator::Generator, }; +pub type POSITIONS = Vec>>; + #[derive(Clone)] pub struct Board { pub game_state: State, @@ -19,7 +21,7 @@ pub struct Board { generator: Generator, pub preview_pieces: [Tetromino; 7], // (0, 0) represents the bottom left corner, (WIDTH, HEIGHT) represents the top right corner - pub positions: Vec>>, + pub positions: POSITIONS, pub last_action: Option, } @@ -28,13 +30,13 @@ impl Display for Board { let mut board_string = String::new(); for row in self.positions.iter() { for element in row.iter().rev() { - if let Some(_) = element { - board_string.insert_str(0, "x"); + if element.is_some() { + board_string.insert(0, 'x'); } else { - board_string.insert_str(0, " "); + board_string.insert(0, ' '); } } - board_string.insert_str(0, "\n"); + board_string.insert(0, '\n'); } write!(f, "{}", board_string) } @@ -45,13 +47,13 @@ impl Debug for Board { let mut board_string = String::new(); for row in self.positions.iter() { for element in row.iter().rev() { - if let Some(_) = element { - board_string.insert_str(0, "x"); + if element.is_some() { + board_string.insert(0, 'x'); } else { - board_string.insert_str(0, " "); + board_string.insert(0, ' '); } } - board_string.insert_str(0, "\n"); + board_string.insert(0, '\n'); } write!(f, "{}", board_string) } @@ -70,7 +72,7 @@ impl Board { generator, preview_pieces: tetrominos, // https://stackoverflow.com/a/53930630 - positions: positions, + positions, last_action: None, }; board.set_next_tetromino_as_active_piece(); @@ -252,8 +254,7 @@ impl Board { self.rotate_tetromino_90(!clockwise, false); return true; } - - return false; + false } pub fn rotate_tetromino_180(&mut self, should_offset: bool) -> bool { @@ -288,7 +289,7 @@ impl Board { self.rotate_tetromino_180(false); return true; } - return false; + false // self.rotate_tetromino(clockwise, true) } @@ -431,7 +432,7 @@ impl Board { } fn clear_line(&mut self, row_index: usize) { - for y in row_index..self.positions.len() as usize - 1 { + for y in row_index..self.positions.len() - 1 { self.positions[y] = self.positions[y + 1].clone(); self.positions[y + 1] = vec![None; self.positions[0].len()]; } diff --git a/src/core/consts.rs b/src/core/consts.rs index b2ad32f..ecf99e1 100644 --- a/src/core/consts.rs +++ b/src/core/consts.rs @@ -104,8 +104,9 @@ pub const TETROMINO_TYPES: [Tetromino; 7] = [ ]; // https://en.wikipedia.org/wiki/Tetromino -#[derive(Clone, Copy, Debug)] +#[derive(Default, Clone, Copy, Debug)] pub enum Tetromino { + #[default] I, J, L, @@ -115,11 +116,11 @@ pub enum Tetromino { Z, } -impl Default for Tetromino { - fn default() -> Self { - Tetromino::I - } -} +// impl Default for Tetromino { +// fn default() -> Self { +// Tetromino::I +// } +// } impl Tetromino { // RGB value of color @@ -187,28 +188,28 @@ impl Tetromino { pub fn find_offset_row_90(first: i8, second: i8) -> usize { if (first, second) == (0, 1) { // 0->R - return 0; + 0 } else if (first, second) == (1, 0) { // R->0 - return 1; + 1 } else if (first, second) == (1, 2) { // R->2 - return 2; + 2 } else if (first, second) == (2, 1) { // 2->R - return 3; + 3 } else if (first, second) == (2, 3) { // 2->L - return 4; + 4 } else if (first, second) == (3, 2) { // L->2 - return 5; + 5 } else if (first, second) == (3, 0) { // L->0 - return 6; + 6 } else if (first, second) == (0, 3) { // 0->L - return 7; + 7 } else { println!("first: {}, second: {}", first, second); unimplemented!(); @@ -364,16 +365,16 @@ impl Tetromino { pub fn find_offset_row_180(first: i8, second: i8) -> usize { if (first, second) == (0, 2) { // 0->L (0>>2) - return 0; + 0 } else if (first, second) == (1, 3) { // L->0 (1>>3) - return 1; + 1 } else if (first, second) == (2, 0) { // R->3 (2>>0) - return 2; + 2 } else if (first, second) == (3, 1) { // 3->R (3>>1) - return 3; + 3 } else { println!("first: {}, second: {}", first, second); unimplemented!(); diff --git a/src/drawer.rs b/src/drawer.rs index c434497..4afc1f0 100644 --- a/src/drawer.rs +++ b/src/drawer.rs @@ -8,7 +8,7 @@ use macroquad::{ }; use crate::core::{ - board::Board, + board::{Board, POSITIONS}, consts::{vec2, Piece, Tetromino, Vec2, GRAY}, }; @@ -140,7 +140,7 @@ impl<'a> Drawer<'a> { }); } - pub fn draw_tetrominos(&self, positions: &Vec>>) { + pub fn draw_tetrominos(&self, positions: &POSITIONS) { let block_size = self.block_size.get(); let bottom_left_corner = self.bottom_left_corner.get(); let debug = self.debug.get(); diff --git a/src/game.rs b/src/game.rs index f325b72..ecaf486 100644 --- a/src/game.rs +++ b/src/game.rs @@ -7,8 +7,8 @@ pub struct Game { pub input: Input, } -impl Game { - pub fn new() -> Self { +impl Default for Game { + fn default() -> Self { Self { board: Box::new(Board::new(now() as usize)), input: Input::default(), diff --git a/web.sh b/web.sh index 2d9b834..b4ba1e7 100755 --- a/web.sh +++ b/web.sh @@ -50,7 +50,5 @@ headers.Strict-Transport-Security = \"max-age=0; includeSubDomains; preload\"" > if [ "$1" == "online" ] then caddy file-server --listen :8080 - # cargo install --git https://github.com/static-web-server/static-web-server - # echo "Running application on http://localhost:8080" - # static-web-server --config-file config.toml --log-level "trace" --port 8080 --root . + # caddy file-server --browse --listen :8080 fi \ No newline at end of file