diff --git a/src/app.rs b/src/app.rs index 573b433..cc5a343 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; // use serde_wasm_bindgen::to_value; use wasm_bindgen::prelude::*; -// use web_sys::EventTarget; +use web_sys::AddEventListenerOptions; // use wasm_bindgen_futures::spawn_local; use yew::{prelude::*, html::Scope}; @@ -19,7 +19,8 @@ extern "C" { } pub enum Msg { - Discover{cell: Cell} + Discover{ cell: Cell }, + Flag{ cell: Cell } } pub struct App { @@ -33,10 +34,12 @@ impl Component for App { type Properties = (); fn create(ctx: &Context) -> Self { - let mut game = Game::new(10, 10, 10); - game.start_board(); + let height = 100; + let width = 41; + let num_mines =(height * width / 10) as usize; - // info!("\n{}", game.get_board().to_string()); + let mut game = Game::new(height, width, num_mines); + game.start_board(); Self { link: ctx.link().clone(), @@ -46,10 +49,12 @@ impl Component for App { fn view(&self, ctx: &Context) -> Html { let b = self.game.get_board().clone(); - html!{
- + // Disable context menu + // + +
} } @@ -57,11 +62,10 @@ impl Component for App { fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { Msg::Discover {cell} => { - // info!("Pos (from App): {}", format!("{:?}", cell.get_pos())); self.game.show(cell.get_pos()); - - // info!("celhid (from App): {}", format!("{:?}", self.game.get_cell(cell.get_pos()).is_hidden())); - // info!("\n{}", self.game.get_board().to_string()) + }, + Msg::Flag {cell} => { + self.game.set_flag(cell.get_pos(), !self.game.get_cell(cell.get_pos()).is_flagged()); } } true diff --git a/src/components/board.rs b/src/components/board.rs index a3c0cf8..460fa7b 100644 --- a/src/components/board.rs +++ b/src/components/board.rs @@ -10,17 +10,20 @@ use wasm_bindgen::JsValue; pub struct BoardComponent { link: Scope, board: Board, - onsignal: Callback + onsignal: Callback, + flagsignal: Callback } pub enum Msg { Discover{ cell: Cell }, + Flag{ cell: Cell } } #[derive(Clone, PartialEq, Properties)] pub struct Props { pub board: Board, pub onsignal: Callback, + pub flagsignal: Callback } impl Component for BoardComponent { @@ -32,7 +35,8 @@ impl Component for BoardComponent { Self { link: ctx.link().clone(), board: ctx.props().board.clone(), - onsignal: ctx.props().onsignal.clone() + onsignal: ctx.props().onsignal.clone(), + flagsignal: ctx.props().flagsignal.clone() } } @@ -47,7 +51,7 @@ impl Component for BoardComponent { <> {row.into_iter().map(|c| { html! { - } @@ -56,11 +73,10 @@ impl Component for Button { fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { Msg::Clicked => { - // info!("hid (from cell): {}", format!("{:?}", self.cell.is_hidden())); self.onsignal.emit(self.cell); } Msg::RightClicked => { - self.onsignal.emit(self.cell); + self.flagsignal.emit(self.cell); }, } false diff --git a/src/minesweeper/board.rs b/src/minesweeper/board.rs index ff7d526..2457553 100644 --- a/src/minesweeper/board.rs +++ b/src/minesweeper/board.rs @@ -83,6 +83,10 @@ impl Board { pub fn set_delay(&mut self, pos: (usize, usize), delay: f32) { self.board[pos.0][pos.1].set_delay(delay) } + + pub fn set_flag(&mut self, pos: (usize, usize), flag: bool) { + self.board[pos.0][pos.1].set_flag(flag) + } } impl ToString for Board { diff --git a/src/minesweeper/cell.rs b/src/minesweeper/cell.rs index e80a5b0..7186b4c 100644 --- a/src/minesweeper/cell.rs +++ b/src/minesweeper/cell.rs @@ -42,6 +42,7 @@ impl Cell { pub fn show(&mut self) { self.hidden = false; + self.flagged = false; } pub fn is_hidden(&self) -> bool { @@ -53,6 +54,10 @@ impl Cell { } pub fn set_flag(&mut self, new_flag: bool) { + if !self.hidden { + self.flagged = false; + return; + } self.flagged = new_flag; } @@ -72,11 +77,12 @@ impl ToString for Cell { return " ".to_string(); } if self.is_mine() { - return "*".to_string(); + return " ".to_string(); } if self.value == 0 { return " ".to_string(); } + return self.value.to_string(); } } \ No newline at end of file diff --git a/src/minesweeper/mod.rs b/src/minesweeper/mod.rs index 59a47c4..1c2f009 100644 --- a/src/minesweeper/mod.rs +++ b/src/minesweeper/mod.rs @@ -34,7 +34,6 @@ impl Game { } } - // Set values for i in 0..self.board.get_height() { for j in 0..self.board.get_width() { self.board.set_value((i, j), self.board.calculate_value((i, j))) @@ -52,10 +51,11 @@ impl Game { cells_to_show.push(init_pos); + self.board.get_cell(init_pos).set_delay(0.0); + let mut added_cells = 1; while added_cells > 0 { - // info!("{:?}", cells_to_show.len() - added_cells); let new_cells = cells_to_show.len() - added_cells; added_cells = 0; @@ -77,51 +77,41 @@ impl Game { cells_to_show.push(new_pos); added_cells += 1; } - self.board.set_delay(new_pos, f32::sqrt(((init_pos.0 as isize - new_pos.0 as isize).pow(2) + (init_pos.1 as isize - new_pos.1 as isize).pow(2)) as f32) * 0.05); + + // let delay = f32::sqrt(((init_pos.0 as isize - new_pos.0 as isize).pow(2) + (init_pos.1 as isize - new_pos.1 as isize).pow(2)) as f32) * 0.05; + + let delay = self.board.get_cell(pos).get_delay() + 0.05; + + self.board.set_delay(new_pos, delay); self.board.show_cell(new_pos); } } } } - - // for pos in cells_to_show { - // self.board.show_cell(pos); - // } - - - - - // self.board.show_cell(pos); - // if self.board.get_value(pos) == 0 { - // for i in -1..=1 { - // for j in -1..=1 { - // if pos.0 as isize + i >= 0 && - // pos.0 as isize + i < self.get_height() as isize && - // pos.1 as isize + j >= 0 && - // pos.1 as isize + j < self.get_width() as isize && - // self.board.get_cell(((pos.0 as isize + i) as usize , (pos.1 as isize + j) as usize)).is_hidden() { - // self.show(((pos.0 as isize + i) as usize , (pos.1 as isize + j) as usize)) - // } - // } - // } - // } - - } pub fn get_height(&self) -> usize { - return self.board.get_height(); + self.board.get_height() } pub fn get_width(&self) -> usize { - return self.board.get_width(); + self.board.get_width() } pub fn get_board(&self) -> &Board { - return &self.board; + &self.board } pub fn get_cell(&self, pos: (usize, usize)) -> Cell { self.board.get_cell(pos) } -} \ No newline at end of file + + pub fn is_flagged(&self, pos: (usize, usize)) -> bool { + self.board.get_cell(pos).is_flagged() + } + + pub fn set_flag(&mut self, pos: (usize, usize), flag: bool) { + self.board.set_flag(pos, flag); + } + +} diff --git a/styles.css b/styles.css index 9f13c04..2fe83cd 100644 --- a/styles.css +++ b/styles.css @@ -4,7 +4,7 @@ line-height: 24px; font-weight: 400; /* Verdana , Avenir, Helvetica, Arial, sans-serif */ - color: #0f0f0f; + color: #0f0f0f98; background-color: #f6f6f6; font-synthesis: none; @@ -67,8 +67,9 @@ button { text-align: center; border-radius: 8px; border: 1px solid transparent; - font-weight: 500; - color: #0f0f0f98; + /* font-weight: 500; */ + font-size: 25px; + color: rgba(15, 15, 15, 0.0); background-color: #0f0f0f98; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2); height: 50px; @@ -84,9 +85,9 @@ button { text-align: center; border-radius: 8px; border: 1px solid transparent; - font-size: 20px; - font-weight: 500; - /* color: #2f2f2f; */ + font-size: 25px; + font-weight: 700; + color: #0f0f0f98; background-color: #2f2f2f; height: 50px; width: 50px;