Skip to content

Commit f5f6de6

Browse files
committed
code organize
1 parent 1b82d2f commit f5f6de6

File tree

7 files changed

+42
-41
lines changed

7 files changed

+42
-41
lines changed

.devcontainer/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.72.0-bullseye
1+
FROM mcr.microsoft.com/vscode/devcontainers/rust:buster
22

33
ENV DEBIAN_FRONTEND noninteractive
44

src/bin/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn main() {
2626
debug: &debug,
2727
};
2828
let mut open_settings = false;
29-
let mut game = Game::new();
29+
let mut game = Game::default();
3030

3131
loop {
3232
let block_size_temp = (screen_height() / (game.board.positions.len() as f32 * 1.25))

src/drawer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use macroquad::{
88
};
99

1010
use crate::tetris::{
11-
board::Board,
11+
board::{Board, POSITIONS},
1212
consts::{vec2, Piece, Tetromino, Vec2, GRAY},
1313
};
1414

@@ -140,7 +140,7 @@ impl<'a> Drawer<'a> {
140140
});
141141
}
142142

143-
pub fn draw_tetrominos(&self, positions: &Vec<Vec<Option<(u8, u8, u8)>>>) {
143+
pub fn draw_tetrominos(&self, positions: &POSITIONS) {
144144
let block_size = self.block_size.get();
145145
let bottom_left_corner = self.bottom_left_corner.get();
146146
let debug = self.debug.get();

src/game.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub struct Game {
77
pub input: Input,
88
}
99

10-
impl Game {
11-
pub fn new() -> Self {
10+
impl Default for Game {
11+
fn default() -> Self {
1212
Self {
1313
board: Box::new(Board::new(now() as usize)),
1414
input: Input::default(),

src/tetris/board.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use super::{
1010
generator::Generator,
1111
};
1212

13+
pub type POSITIONS = Vec<Vec<Option<(u8, u8, u8)>>>;
14+
1315
#[derive(Clone)]
1416
pub struct Board {
1517
pub game_state: State,
@@ -19,7 +21,7 @@ pub struct Board {
1921
generator: Generator,
2022
pub preview_pieces: [Tetromino; 7],
2123
// (0, 0) represents the bottom left corner, (WIDTH, HEIGHT) represents the top right corner
22-
pub positions: Vec<Vec<Option<(u8, u8, u8)>>>,
24+
pub positions: POSITIONS,
2325
pub last_action: Option<Action>,
2426
}
2527

@@ -28,13 +30,13 @@ impl Display for Board {
2830
let mut board_string = String::new();
2931
for row in self.positions.iter() {
3032
for element in row.iter().rev() {
31-
if let Some(_) = element {
32-
board_string.insert_str(0, "x");
33+
if element.is_some() {
34+
board_string.insert(0, 'x');
3335
} else {
34-
board_string.insert_str(0, " ");
36+
board_string.insert(0, ' ');
3537
}
3638
}
37-
board_string.insert_str(0, "\n");
39+
board_string.insert(0, '\n');
3840
}
3941
write!(f, "{}", board_string)
4042
}
@@ -45,13 +47,13 @@ impl Debug for Board {
4547
let mut board_string = String::new();
4648
for row in self.positions.iter() {
4749
for element in row.iter().rev() {
48-
if let Some(_) = element {
49-
board_string.insert_str(0, "x");
50+
if element.is_some() {
51+
board_string.insert(0, 'x');
5052
} else {
51-
board_string.insert_str(0, " ");
53+
board_string.insert(0, ' ');
5254
}
5355
}
54-
board_string.insert_str(0, "\n");
56+
board_string.insert(0, '\n');
5557
}
5658
write!(f, "{}", board_string)
5759
}
@@ -70,7 +72,7 @@ impl Board {
7072
generator,
7173
preview_pieces: tetrominos,
7274
// https://stackoverflow.com/a/53930630
73-
positions: positions,
75+
positions,
7476
last_action: None,
7577
};
7678
board.set_next_tetromino_as_active_piece();
@@ -112,7 +114,7 @@ impl Board {
112114
tetromino
113115
.get_structure()
114116
.iter()
115-
.map(|pos| vec2(pos.x + board_width as f32 / 2.0, board_height - 2.0 - pos.y))
117+
.map(|pos| vec2(pos.x + board_width / 2.0, board_height - 2.0 - pos.y))
116118
.collect()
117119
}
118120

@@ -249,7 +251,7 @@ impl Board {
249251
self.rotate_tetromino_90(!clockwise, false);
250252
return true;
251253
}
252-
return false;
254+
false
253255
}
254256

255257
pub fn rotate_tetromino_180(&mut self, should_offset: bool) -> bool {
@@ -284,7 +286,7 @@ impl Board {
284286
self.rotate_tetromino_180(false);
285287
return true;
286288
}
287-
return false;
289+
false
288290

289291
// self.rotate_tetromino(clockwise, true)
290292
}
@@ -427,7 +429,7 @@ impl Board {
427429
}
428430

429431
fn clear_line(&mut self, row_index: usize) {
430-
for y in row_index..self.positions.len() as usize - 1 {
432+
for y in row_index..self.positions.len() - 1 {
431433
self.positions[y] = self.positions[y + 1].clone();
432434
self.positions[y + 1] = vec![None; self.positions[0].len()];
433435
}

src/tetris/consts.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ pub const TETROMINO_TYPES: [Tetromino; 7] = [
104104
];
105105

106106
// https://en.wikipedia.org/wiki/Tetromino
107-
#[derive(Clone, Copy, Debug)]
107+
#[derive(Default, Clone, Copy, Debug)]
108108
pub enum Tetromino {
109+
#[default]
109110
I,
110111
J,
111112
L,
@@ -115,11 +116,11 @@ pub enum Tetromino {
115116
Z,
116117
}
117118

118-
impl Default for Tetromino {
119-
fn default() -> Self {
120-
Tetromino::I
121-
}
122-
}
119+
// impl Default for Tetromino {
120+
// fn default() -> Self {
121+
// Tetromino::I
122+
// }
123+
// }
123124

124125
impl Tetromino {
125126
// RGB value of color
@@ -187,28 +188,28 @@ impl Tetromino {
187188
pub fn find_offset_row_90(first: i8, second: i8) -> usize {
188189
if (first, second) == (0, 1) {
189190
// 0->R
190-
return 0;
191+
0
191192
} else if (first, second) == (1, 0) {
192193
// R->0
193-
return 1;
194+
1
194195
} else if (first, second) == (1, 2) {
195196
// R->2
196-
return 2;
197+
2
197198
} else if (first, second) == (2, 1) {
198199
// 2->R
199-
return 3;
200+
3
200201
} else if (first, second) == (2, 3) {
201202
// 2->L
202-
return 4;
203+
4
203204
} else if (first, second) == (3, 2) {
204205
// L->2
205-
return 5;
206+
5
206207
} else if (first, second) == (3, 0) {
207208
// L->0
208-
return 6;
209+
6
209210
} else if (first, second) == (0, 3) {
210211
// 0->L
211-
return 7;
212+
7
212213
} else {
213214
println!("first: {}, second: {}", first, second);
214215
unimplemented!();
@@ -353,16 +354,16 @@ impl Tetromino {
353354
pub fn find_offset_row_180(first: i8, second: i8) -> usize {
354355
if (first, second) == (0, 2) {
355356
// 0->L (0>>2)
356-
return 0;
357+
0
357358
} else if (first, second) == (1, 3) {
358359
// L->0 (1>>3)
359-
return 1;
360+
1
360361
} else if (first, second) == (2, 0) {
361362
// R->3 (2>>0)
362-
return 2;
363+
2
363364
} else if (first, second) == (3, 1) {
364365
// 3->R (3>>1)
365-
return 3;
366+
3
366367
} else {
367368
println!("first: {}, second: {}", first, second);
368369
unimplemented!();

web.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,5 @@ headers.Strict-Transport-Security = \"max-age=0; includeSubDomains; preload\"" >
5050
if [ "$1" == "online" ]
5151
then
5252
caddy file-server --listen :8080
53-
# cargo install --git https://github.com/static-web-server/static-web-server
54-
# echo "Running application on http://localhost:8080"
55-
# static-web-server --config-file config.toml --log-level "trace" --port 8080 --root .
53+
# caddy file-server --browse --listen :8080
5654
fi

0 commit comments

Comments
 (0)