Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunmag committed Jan 20, 2020
1 parent e04007f commit adf5b55
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ fn main() {
let mut field = Field::new(FIELD_SIZE, MINES_DENSITY);
let mut sapper = Sapper::new();
let mut terminal = BufferedTerminal::new(new_terminal(Capabilities::new_from_env().unwrap()).unwrap()).unwrap();
let mut render = true;
let mut update_screen = true;
let mut update_cursor = true;

terminal.terminal().set_raw_mode().unwrap();

loop {
if render {
if update_screen {
terminal.draw_from_screen(&field.render(&sapper), 0, 0);
}

if render || update_cursor {
if update_screen || update_cursor {
if !sapper.is_alive {
terminal.add_change("\r\nSorry, but you've taken the wrong step. Game over, press Esc to exit.");
}
Expand All @@ -49,7 +49,7 @@ fn main() {
});

terminal.flush().unwrap();
render = false;
update_screen = false;
update_cursor = false;
}

Expand Down Expand Up @@ -80,8 +80,8 @@ fn main() {
update_cursor = true;
}
InputEvent::Key(KeyEvent {key: KeyCode::Char('m'), ..}) => {
field.mark(sapper.position);
render = true;
field.cells[sapper.position].toggle_mark();
update_screen = true;
field.update_is_cleaned();
}
InputEvent::Key(KeyEvent {key: KeyCode::Char(' '), ..}) => {
Expand All @@ -92,7 +92,7 @@ fn main() {
sapper.is_alive = false;
}

render = true;
update_screen = true;
}
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions src/models/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Cell {
};
}

pub fn mark(&mut self) {
pub fn toggle_mark(&mut self) {
match self.state {
CellState::Undiscovered => self.state = CellState::Marked,
CellState::Marked => self.state = CellState::Undiscovered,
Expand All @@ -33,7 +33,7 @@ impl Cell {
let mut mark = match self.state {
CellState::Marked => 'M',
CellState::Undiscovered => '.',
CellState::Discovered => char::from_digit(field.count_mines(position), 10).unwrap_or('+'),
CellState::Discovered => char::from_digit(field.count_mines_around(position), 10).unwrap_or('+'),
};

if mark == '0' {
Expand Down
8 changes: 2 additions & 6 deletions src/models/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Field {
} else {
cell.discover();

if cell.state == CellState::Discovered && self.count_mines(position) == 0 {
if cell.state == CellState::Discovered && self.count_mines_around(position) == 0 {
for i in self.around(position) {
if self.cells[i].state == CellState::Undiscovered {
self.discover(i);
Expand All @@ -65,11 +65,7 @@ impl Field {
}
}

pub fn mark(&mut self, position: usize) {
self.cells[position].mark();
}

pub fn count_mines(&self, position: usize) -> u32 {
pub fn count_mines_around(&self, position: usize) -> u32 {
let mut mines = 0;

for i in self.around(position) {
Expand Down

0 comments on commit adf5b55

Please sign in to comment.