clippy
This commit is contained in:
35
src/main.rs
35
src/main.rs
@@ -3,7 +3,7 @@ use crossterm::{
|
|||||||
poll, read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, MouseButton,
|
poll, read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, MouseButton,
|
||||||
MouseEventKind,
|
MouseEventKind,
|
||||||
},
|
},
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, size},
|
terminal::{disable_raw_mode, enable_raw_mode},
|
||||||
ExecutableCommand,
|
ExecutableCommand,
|
||||||
};
|
};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
@@ -27,7 +27,6 @@ fn main() {
|
|||||||
print!("\x1b[2J\x1b[H");
|
print!("\x1b[2J\x1b[H");
|
||||||
let mut board = Board::new(MINE_COUNT);
|
let mut board = Board::new(MINE_COUNT);
|
||||||
board.calculate();
|
board.calculate();
|
||||||
let size = size().unwrap();
|
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
loop {
|
loop {
|
||||||
board.render();
|
board.render();
|
||||||
@@ -109,13 +108,12 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let (x, y) = (x as usize, y as usize);
|
|
||||||
changes.push((Position { x, y }, n));
|
changes.push((Position { x, y }, n));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (pos, n) in changes {
|
for (pos, n) in changes {
|
||||||
if let CellKind::MineCount(c) = &mut self.0[pos.y as usize][pos.x as usize].kind {
|
if let CellKind::MineCount(c) = &mut self.0[pos.y][pos.x].kind {
|
||||||
*c = n;
|
*c = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,13 +123,11 @@ impl Board {
|
|||||||
if validate_pos(x, y) {
|
if validate_pos(x, y) {
|
||||||
let cell = self.0[y as usize][x as usize];
|
let cell = self.0[y as usize][x as usize];
|
||||||
if let CellKind::MineCount(n) = cell.kind {
|
if let CellKind::MineCount(n) = cell.kind {
|
||||||
if cell.visibility == Visibility::Hidden {
|
if cell.visibility == Visibility::Hidden && n == 0 {
|
||||||
if n == 0 {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,8 +135,7 @@ impl Board {
|
|||||||
for y in 0..HEIGHT {
|
for y in 0..HEIGHT {
|
||||||
for x in 0..WIDTH {
|
for x in 0..WIDTH {
|
||||||
let cell = self.0[y][x];
|
let cell = self.0[y][x];
|
||||||
if cell.visibility == Visibility::Visible {
|
if cell.visibility == Visibility::Visible && cell.kind == CellKind::MineCount(0) {
|
||||||
if cell.kind == CellKind::MineCount(0) {
|
|
||||||
for (test_x, test_y) in TEST_POS {
|
for (test_x, test_y) in TEST_POS {
|
||||||
if let Some(pos) = test_new_pos(&Position { x, y }, (test_x, test_y)) {
|
if let Some(pos) = test_new_pos(&Position { x, y }, (test_x, test_y)) {
|
||||||
if let CellKind::MineCount(_) = self.0[pos.y][pos.x].kind {
|
if let CellKind::MineCount(_) = self.0[pos.y][pos.x].kind {
|
||||||
@@ -152,7 +147,6 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn fill(&mut self, x: i8, y: i8) {
|
fn fill(&mut self, x: i8, y: i8) {
|
||||||
if !self.inside(x, y) {
|
if !self.inside(x, y) {
|
||||||
@@ -162,8 +156,7 @@ impl Board {
|
|||||||
s.push((x, x, y, 1));
|
s.push((x, x, y, 1));
|
||||||
s.push((x, x, y - 1, -1));
|
s.push((x, x, y - 1, -1));
|
||||||
|
|
||||||
while !s.is_empty() {
|
while let Some((mut x1, x2, y, dy)) = s.pop() {
|
||||||
let (mut x1, x2, y, dy) = s.pop().unwrap();
|
|
||||||
let mut x = x1;
|
let mut x = x1;
|
||||||
if self.inside(x, y) {
|
if self.inside(x, y) {
|
||||||
while self.inside(x - 1, y) {
|
while self.inside(x - 1, y) {
|
||||||
@@ -202,7 +195,7 @@ impl Board {
|
|||||||
match cell.visibility {
|
match cell.visibility {
|
||||||
Visibility::Visible => match cell.kind {
|
Visibility::Visible => match cell.kind {
|
||||||
CellKind::Mine => s.push('*'),
|
CellKind::Mine => s.push('*'),
|
||||||
CellKind::MineCount(n) => s.push((n + '0' as u8) as char),
|
CellKind::MineCount(n) => s.push((n + b'0') as char),
|
||||||
},
|
},
|
||||||
Visibility::Hidden => s.push('#'),
|
Visibility::Hidden => s.push('#'),
|
||||||
Visibility::Flag => s.push('F'),
|
Visibility::Flag => s.push('F'),
|
||||||
@@ -223,10 +216,7 @@ fn input() -> Input {
|
|||||||
if let Event::Mouse(event) = r {
|
if let Event::Mouse(event) = r {
|
||||||
if let MouseEventKind::Down(button) = event.kind {
|
if let MouseEventKind::Down(button) = event.kind {
|
||||||
if button == MouseButton::Left || button == MouseButton::Right {
|
if button == MouseButton::Left || button == MouseButton::Right {
|
||||||
let (y, x) = (
|
let (y, x) = (HEIGHT - event.row as usize - 1, event.column as usize);
|
||||||
HEIGHT as usize - event.row as usize - 1,
|
|
||||||
event.column as usize,
|
|
||||||
);
|
|
||||||
return Input {
|
return Input {
|
||||||
button,
|
button,
|
||||||
pos: Position { x, y },
|
pos: Position { x, y },
|
||||||
@@ -244,21 +234,16 @@ fn input() -> Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_input(input: &Input, board: &mut Board) {
|
fn handle_input(input: &Input, board: &mut Board) {
|
||||||
if (input.pos.x as usize) < WIDTH && (input.pos.y as usize) < HEIGHT {
|
if (input.pos.x) < WIDTH && (input.pos.y) < HEIGHT {
|
||||||
if input.button == MouseButton::Left {
|
if input.button == MouseButton::Left {
|
||||||
board.fill(input.pos.x as i8, input.pos.y as i8);
|
board.fill(input.pos.x as i8, input.pos.y as i8);
|
||||||
board.pad_fill();
|
board.pad_fill();
|
||||||
let cell = &mut board.0[input.pos.y as usize][input.pos.x as usize];
|
let cell = &mut board.0[input.pos.y][input.pos.x];
|
||||||
if cell.visibility == Visibility::Hidden {
|
if cell.visibility == Visibility::Hidden {
|
||||||
cell.visibility = Visibility::Visible;
|
cell.visibility = Visibility::Visible;
|
||||||
}
|
}
|
||||||
if let CellKind::MineCount(n) = cell.kind {
|
|
||||||
if n != 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if input.button == MouseButton::Right {
|
} else if input.button == MouseButton::Right {
|
||||||
let cell = &mut board.0[input.pos.y as usize][input.pos.x as usize];
|
let cell = &mut board.0[input.pos.y][input.pos.x];
|
||||||
if cell.visibility == Visibility::Hidden {
|
if cell.visibility == Visibility::Hidden {
|
||||||
cell.visibility = Visibility::Flag;
|
cell.visibility = Visibility::Flag;
|
||||||
} else if cell.visibility == Visibility::Flag {
|
} else if cell.visibility == Visibility::Flag {
|
||||||
|
Reference in New Issue
Block a user