game state and win/loose message

This commit is contained in:
Guilleag01
2023-08-09 16:45:51 +02:00
parent 296fe3da6a
commit 80024609ef
3 changed files with 50 additions and 21 deletions

View File

@@ -129,6 +129,14 @@ impl Component for App {
flagsignal={self.link.callback(|cell| Msg::Flag{cell})} flagsignal={self.link.callback(|cell| Msg::Flag{cell})}
board={b}/> board={b}/>
</div> </div>
<div class="gamestate">
if self.game.get_gamestate() == 1 {
<h1>{"You have lost"}</h1>
} else if self.game.get_gamestate() == 2 {
<h1>{"You win!"}</h1>
}
</div>
</main> </main>
} }
} }
@@ -157,11 +165,14 @@ impl Component for App {
match msg { match msg {
Msg::Discover { cell } => { Msg::Discover { cell } => {
if self.game.get_gamestate() == 0 {
if !self.game.get_fist_interaction() { if !self.game.get_fist_interaction() {
self.game.start_board(cell.get_pos()); self.game.start_board(cell.get_pos());
self.game.set_fist_interaction(true); self.game.set_fist_interaction(true);
} }
self.game.show(cell.get_pos()); self.game.show(cell.get_pos());
self.game.update_state();
}
} }
Msg::Flag { cell } => { Msg::Flag { cell } => {
self.game.set_flag( self.game.set_flag(

View File

@@ -12,6 +12,7 @@ use rand::Rng;
pub struct Game { pub struct Game {
board: Board, board: Board,
first_interaction: bool, first_interaction: bool,
gamestate: usize,
} }
impl Game { impl Game {
@@ -19,24 +20,11 @@ impl Game {
Self { Self {
board: Board::new(height, width, num_mines), board: Board::new(height, width, num_mines),
first_interaction: false, first_interaction: false,
gamestate: 0,
} }
} }
pub fn start_board(&mut self, init_pos: (usize, usize)) { 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 rng = rand::thread_rng();
let mut possible_pos: Vec<(usize, usize)> = Vec::new(); 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 { pub fn get_height(&self) -> usize {
self.board.get_height() self.board.get_height()
} }
@@ -153,4 +163,8 @@ impl Game {
pub fn set_fist_interaction(&mut self, first_interaction: bool) { pub fn set_fist_interaction(&mut self, first_interaction: bool) {
self.first_interaction = first_interaction self.first_interaction = first_interaction
} }
pub fn get_gamestate(&self) -> usize {
self.gamestate
}
} }

View File

@@ -33,6 +33,7 @@ button {
border-radius: 8px; border-radius: 8px;
border: 1px solid transparent; border: 1px solid transparent;
margin: 2px; margin: 2px;
outline: none;
} }
.upper-menu { .upper-menu {
@@ -215,7 +216,10 @@ button {
height: 70%; height: 70%;
} }
input, .gamestate {
button { display: flex;
outline: none; justify-content: center;
align-items: center;
color: #ffffff;
font-size: 25px;
} }