diff --git a/src/app.rs b/src/app.rs
index c3ad15d..ad42036 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -129,6 +129,14 @@ impl Component for App {
flagsignal={self.link.callback(|cell| Msg::Flag{cell})}
board={b}/>
+
+
+ if self.game.get_gamestate() == 1 {
+
{"You have lost"}
+ } else if self.game.get_gamestate() == 2 {
+ {"You win!"}
+ }
+
}
}
@@ -157,11 +165,14 @@ impl Component for App {
match msg {
Msg::Discover { cell } => {
- if !self.game.get_fist_interaction() {
- self.game.start_board(cell.get_pos());
- self.game.set_fist_interaction(true);
+ if self.game.get_gamestate() == 0 {
+ if !self.game.get_fist_interaction() {
+ self.game.start_board(cell.get_pos());
+ self.game.set_fist_interaction(true);
+ }
+ self.game.show(cell.get_pos());
+ self.game.update_state();
}
- self.game.show(cell.get_pos());
}
Msg::Flag { cell } => {
self.game.set_flag(
diff --git a/src/minesweeper/mod.rs b/src/minesweeper/mod.rs
index 21cd85b..3bc8e3f 100644
--- a/src/minesweeper/mod.rs
+++ b/src/minesweeper/mod.rs
@@ -12,6 +12,7 @@ use rand::Rng;
pub struct Game {
board: Board,
first_interaction: bool,
+ gamestate: usize,
}
impl Game {
@@ -19,24 +20,11 @@ impl Game {
Self {
board: Board::new(height, width, num_mines),
first_interaction: false,
+ gamestate: 0,
}
}
pub fn start_board(&mut self, init_pos: (usize, usize)) {
- // TODO: make a better implementation
- // let mut added_mines = 0;
-
- // while added_mines < self.board.get_num_mines() {
- // let pos = (
- // rng.gen_range(0..self.board.get_height()),
- // rng.gen_range(0..self.board.get_width()),
- // );
- // if !self.board.is_mine(pos) {
- // self.board.set_mine(pos, true);
- // added_mines += 1;
- // }
- // }
-
let mut rng = rand::thread_rng();
let mut possible_pos: Vec<(usize, usize)> = Vec::new();
@@ -122,6 +110,28 @@ impl Game {
}
}
+ pub fn update_state(&mut self) -> usize {
+ let mut unfinished = false;
+ for i in 0..self.board.get_height() {
+ for j in 0..self.board.get_width() {
+ let cell = self.get_cell((i, j));
+ if !cell.is_mine() && cell.is_hidden() {
+ unfinished = true;
+ }
+ if cell.is_mine() && !cell.is_hidden() {
+ self.gamestate = 1;
+ return 1; // player has lost
+ }
+ }
+ }
+ if unfinished {
+ self.gamestate = 0;
+ return 0;
+ }
+ self.gamestate = 2;
+ 2 // player has won
+ }
+
pub fn get_height(&self) -> usize {
self.board.get_height()
}
@@ -153,4 +163,8 @@ impl Game {
pub fn set_fist_interaction(&mut self, first_interaction: bool) {
self.first_interaction = first_interaction
}
+
+ pub fn get_gamestate(&self) -> usize {
+ self.gamestate
+ }
}
diff --git a/styles.css b/styles.css
index 9f83d11..e15a9d3 100644
--- a/styles.css
+++ b/styles.css
@@ -33,6 +33,7 @@ button {
border-radius: 8px;
border: 1px solid transparent;
margin: 2px;
+ outline: none;
}
.upper-menu {
@@ -215,7 +216,10 @@ button {
height: 70%;
}
-input,
-button {
- outline: none;
+.gamestate {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ color: #ffffff;
+ font-size: 25px;
}
\ No newline at end of file