This commit is contained in:
2024-05-03 23:29:17 +02:00
parent e11197cdf4
commit a82a3ad566

View File

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