summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2023-08-02 01:04:16 +0300
committerOxore <oxore@protonmail.com>2023-08-02 01:04:16 +0300
commit342c684cd5746c8da5b056798f70b1f49a984fc8 (patch)
treec7a2d961fd818dd07d88ac29c7c4b486bf186e81
parent853bd3742f08ae7862258b7bc736ba9b34ea5b85 (diff)
Run cargo fmt
-rw-r--r--src/main.rs97
1 files changed, 66 insertions, 31 deletions
diff --git a/src/main.rs b/src/main.rs
index 763568c..99898b3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
extern crate termios;
-use rand::seq::SliceRandom;
use nonblock::NonBlockingReader;
+use rand::seq::SliceRandom;
const FIELD_COLS: usize = 10;
const FIELD_ROWS_VISIBLE: usize = 20;
@@ -9,9 +9,9 @@ const FIELD_ROWS: usize = FIELD_ROWS_VISIBLE + 20;
const FIELD_SIZE: usize = FIELD_COLS * FIELD_ROWS;
const FIGURE_SIZE_COLS: usize = 4;
const FIGURE_SIZE_ROWS: usize = 4;
-const FIGURE_SIZE: usize = FIGURE_SIZE_COLS*FIGURE_SIZE_ROWS;
+const FIGURE_SIZE: usize = FIGURE_SIZE_COLS * FIGURE_SIZE_ROWS;
const CELLS_I: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0];
-const CELLS_L2:[u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0];
+const CELLS_L2: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0];
const CELLS_L: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0];
const CELLS_B: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0];
const CELLS_S: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0];
@@ -27,8 +27,7 @@ enum StepResult {
Quit,
}
-#[derive(Clone)]
-#[derive(Debug)]
+#[derive(Clone, Debug)]
enum FigureKind {
I,
J,
@@ -56,13 +55,16 @@ impl Default for Timeout {
impl Timeout {
fn from_duration(d: std::time::Duration) -> Self {
- Timeout{
+ Timeout {
beginning: nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap(),
duration: d,
}
}
fn is_elapsed(&self) -> bool {
- std::time::Duration::from(self.beginning) + self.duration <= std::time::Duration::from(nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap())
+ std::time::Duration::from(self.beginning) + self.duration
+ <= std::time::Duration::from(
+ nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap(),
+ )
}
}
@@ -75,13 +77,34 @@ struct Figure {
impl Figure {
fn from_kind(kind: FigureKind) -> Self {
match kind {
- FigureKind::I => Self{ cells: CELLS_I, kind },
- FigureKind::J => Self{ cells: CELLS_L2, kind },
- FigureKind::L => Self{ cells: CELLS_L, kind },
- FigureKind::O => Self{ cells: CELLS_B, kind },
- FigureKind::S => Self{ cells: CELLS_S, kind },
- FigureKind::T => Self{ cells: CELLS_T, kind },
- FigureKind::Z => Self{ cells: CELLS_Z, kind },
+ FigureKind::I => Self {
+ cells: CELLS_I,
+ kind,
+ },
+ FigureKind::J => Self {
+ cells: CELLS_L2,
+ kind,
+ },
+ FigureKind::L => Self {
+ cells: CELLS_L,
+ kind,
+ },
+ FigureKind::O => Self {
+ cells: CELLS_B,
+ kind,
+ },
+ FigureKind::S => Self {
+ cells: CELLS_S,
+ kind,
+ },
+ FigureKind::T => Self {
+ cells: CELLS_T,
+ kind,
+ },
+ FigureKind::Z => Self {
+ cells: CELLS_Z,
+ kind,
+ },
}
}
fn rotate_cw_4x4(&mut self) {
@@ -111,7 +134,7 @@ impl Figure {
FigureKind::I => self.rotate_cw_4x4(),
FigureKind::J => self.rotate_cw_3x3(),
FigureKind::L => self.rotate_cw_3x3(),
- FigureKind::O => {},
+ FigureKind::O => {}
FigureKind::S => self.rotate_cw_3x3(),
FigureKind::T => self.rotate_cw_3x3(),
FigureKind::Z => self.rotate_cw_3x3(),
@@ -127,7 +150,9 @@ struct Field {
impl Default for Field {
#[inline]
fn default() -> Self {
- Self { cells: [0; FIELD_SIZE] }
+ Self {
+ cells: [0; FIELD_SIZE],
+ }
}
}
@@ -141,7 +166,8 @@ impl Field {
}
let color = figure.cells[i] & !GHOST_MASK;
let current = f_x as usize + f_y as usize * FIELD_COLS;
- let ghost_or_empty = (self.cells[current] & GHOST_MASK != 0) || self.cells[current] == 0;
+ let ghost_or_empty =
+ (self.cells[current] & GHOST_MASK != 0) || self.cells[current] == 0;
if color != 0 && ghost_or_empty {
self.cells[current] = figure.cells[i];
}
@@ -166,7 +192,10 @@ impl Field {
}
fn is_line_complete(&self, line: usize) -> bool {
assert!(line < FIELD_ROWS);
- self.cells[FIELD_COLS * line..][..FIELD_COLS].iter().fold(1, |a, e| a & e) != 0
+ self.cells[FIELD_COLS * line..][..FIELD_COLS]
+ .iter()
+ .fold(1, |a, e| a & e)
+ != 0
}
fn kill_line(&mut self, line: usize) {
for row in line..FIELD_ROWS - 1 {
@@ -211,10 +240,7 @@ impl Default for Bag {
fn default() -> Self {
let mut pieces: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
pieces.shuffle(&mut rand::thread_rng());
- Self{
- index: 0,
- pieces
- }
+ Self { index: 0, pieces }
}
}
@@ -242,7 +268,7 @@ impl Default for Game {
Figure::from_kind(bag.next()),
Figure::from_kind(bag.next()),
];
- Self{
+ Self {
field: Field::default(),
figure,
x: 3,
@@ -273,14 +299,19 @@ impl Game {
self.stop_place_timeout();
}
fn hold_figure(&mut self) {
- (self.figure_on_hold, self.figure) = (Figure::from_kind(self.figure.kind.clone()), self.figure_on_hold.clone());
+ (self.figure_on_hold, self.figure) = (
+ Figure::from_kind(self.figure.kind.clone()),
+ self.figure_on_hold.clone(),
+ );
(self.x, self.y) = (3, 17);
}
fn reset_fall_timeout(&mut self) {
self.fall_timeout = Timeout::from_duration(std::time::Duration::from_millis(500));
}
fn start_place_timeout(&mut self) {
- self.place_timeout = Some(Timeout::from_duration(std::time::Duration::from_millis(500)));
+ self.place_timeout = Some(Timeout::from_duration(std::time::Duration::from_millis(
+ 500,
+ )));
}
fn stop_place_timeout(&mut self) {
self.place_timeout = None
@@ -299,7 +330,7 @@ impl Game {
}
self.place_figure();
self.stop_place_timeout();
- },
+ }
'j' => {
if !self.field.has_collision(&self.figure, self.x - 1, self.y) {
self.x -= 1;
@@ -330,7 +361,7 @@ impl Game {
self.reset_fall_timeout();
self.stop_place_timeout();
}
- _ => {},
+ _ => {}
}
self.reset_place_timeout();
Some(StepResult::StateChanged)
@@ -350,7 +381,7 @@ impl Game {
}
fn step(&mut self, c_optional: Option<char>) -> Option<StepResult> {
let handle_input_result = if let Some(c) = c_optional {
- self.handle_input(c)
+ self.handle_input(c)
} else {
None
};
@@ -408,7 +439,8 @@ impl Tui {
return print!(" Next: ");
}
for (i, figure) in figures.iter().enumerate().take(NEXT_COUNT) {
- let at_figure = y_global >= (1 + offset + FIGURE_SIZE_ROWS * i) && y_global < (1 + offset + FIGURE_SIZE_ROWS * (i + 1));
+ let at_figure = y_global >= (1 + offset + FIGURE_SIZE_ROWS * i)
+ && y_global < (1 + offset + FIGURE_SIZE_ROWS * (i + 1));
if at_figure {
print!(" ");
let y = FIGURE_SIZE_ROWS - 1 - (y_global - 1 - offset - FIGURE_SIZE_ROWS * i);
@@ -429,7 +461,8 @@ impl Tui {
print!("|");
for x in 0..FIELD_COLS {
let color = field.cells[(FIELD_ROWS_VISIBLE - 1 - y) * FIELD_COLS + x] & !GHOST_MASK;
- let ghost = field.cells[(FIELD_ROWS_VISIBLE - 1 - y) * FIELD_COLS + x] & GHOST_MASK != 0;
+ let ghost =
+ field.cells[(FIELD_ROWS_VISIBLE - 1 - y) * FIELD_COLS + x] & GHOST_MASK != 0;
if color == 0 {
print!(" ");
} else if ghost {
@@ -497,7 +530,9 @@ impl Default for Tui {
termios_state.c_lflag &= !(termios::ICANON | termios::ECHO);
termios::tcsetattr(0, termios::TCSADRAIN, &termios_state).unwrap();
termios_state.c_lflag = c_lflag;
- let tui = Self{state: termios_state_initial};
+ let tui = Self {
+ state: termios_state_initial,
+ };
tui.clear_screen();
tui.hide_cursor();
tui