summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs70
1 files changed, 28 insertions, 42 deletions
diff --git a/src/main.rs b/src/main.rs
index f8002f0..763568c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -136,13 +136,7 @@ impl Field {
for i in 0..FIGURE_SIZE {
let f_x = (i % FIGURE_SIZE_COLS) as isize + x;
let f_y = (i / FIGURE_SIZE_ROWS) as isize + y;
- if f_x < 0 {
- continue;
- } else if f_x >= FIELD_COLS as isize {
- continue;
- } else if f_y < 0 {
- continue;
- } else if f_y >= FIELD_ROWS as isize {
+ if f_x < 0 || f_x >= FIELD_COLS as isize || f_y < 0 || f_y >= FIELD_ROWS as isize {
continue;
}
let color = figure.cells[i] & !GHOST_MASK;
@@ -159,19 +153,12 @@ impl Field {
let f_y = (i / FIGURE_SIZE_ROWS) as isize + y;
let color = figure.cells[i] & !GHOST_MASK;
if color != 0 {
- if f_x < 0 {
- return true;
- } else if f_x >= FIELD_COLS as isize {
- return true;
- } else if f_y < 0 {
+ if f_x < 0 || f_x >= FIELD_COLS as isize || f_y < 0 || f_y >= FIELD_ROWS as isize {
return true;
- } else if f_y >= FIELD_ROWS as isize {
+ }
+ let fcolor = self.cells[f_x as usize + f_y as usize * FIELD_COLS] & !GHOST_MASK;
+ if fcolor != 0 {
return true;
- } else {
- let fcolor = self.cells[f_x as usize + f_y as usize * FIELD_COLS] & !GHOST_MASK;
- if fcolor != 0 {
- return true;
- }
}
}
}
@@ -299,7 +286,7 @@ impl Game {
self.place_timeout = None
}
fn reset_place_timeout(&mut self) {
- if let Some(_) = self.place_timeout {
+ if self.place_timeout.is_some() {
self.start_place_timeout();
}
}
@@ -380,7 +367,7 @@ impl Game {
} else {
None
};
- if let None = handle_input_result {
+ if handle_input_result.is_none() {
advance_figure_result
} else {
handle_input_result
@@ -388,11 +375,11 @@ impl Game {
}
}
-struct TUI {
+struct Tui {
state: termios::Termios,
}
-impl TUI {
+impl Tui {
fn display_hold(&self, figure: &Figure, y_global: usize) {
let offset = DISPLAY_HOLD_OFFSET_DOWN;
let at_figure = y_global >= (1 + offset) && y_global < FIGURE_SIZE_ROWS + (1 + offset);
@@ -415,26 +402,25 @@ impl TUI {
}
}
- fn display_next(&self, figure: &[Figure; NEXT_COUNT], y_global: usize) {
+ fn display_next(&self, figures: &[Figure; NEXT_COUNT], y_global: usize) {
let offset = DISPLAY_NEXT_OFFSET_DOWN;
if y_global == offset {
- print!(" Next: ");
- } else {
- for i in 0..NEXT_COUNT {
- 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);
- for x in 0..FIGURE_SIZE_COLS {
- let c = figure[i].cells[y * FIGURE_SIZE_COLS + x];
- if c == 0 {
- print!(" ");
- } else {
- print!("██");
- }
+ 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));
+ if at_figure {
+ print!(" ");
+ let y = FIGURE_SIZE_ROWS - 1 - (y_global - 1 - offset - FIGURE_SIZE_ROWS * i);
+ for x in 0..FIGURE_SIZE_COLS {
+ let c = figure.cells[y * FIGURE_SIZE_COLS + x];
+ if c == 0 {
+ print!(" ");
+ } else {
+ print!("██");
}
- break;
}
+ break;
}
}
}
@@ -503,10 +489,10 @@ impl TUI {
}
}
-impl Default for TUI {
+impl Default for Tui {
fn default() -> Self {
let termios_state_initial = termios::Termios::from_fd(0).unwrap();
- let mut termios_state = termios_state_initial.clone();
+ let mut termios_state = termios_state_initial;
let c_lflag = termios_state.c_lflag;
termios_state.c_lflag &= !(termios::ICANON | termios::ECHO);
termios::tcsetattr(0, termios::TCSADRAIN, &termios_state).unwrap();
@@ -518,7 +504,7 @@ impl Default for TUI {
}
}
-impl Drop for TUI {
+impl Drop for Tui {
fn drop(&mut self) {
self.show_cursor();
termios::tcsetattr(0, termios::TCSADRAIN, &self.state).unwrap();
@@ -526,7 +512,7 @@ impl Drop for TUI {
}
fn main() {
- let tui = TUI::default();
+ let tui = Tui::default();
let mut game = Game::default();
tui.display_game(&game);
let mut noblock_stdin = NonBlockingReader::from_fd(std::io::stdin()).unwrap();