formatting

This commit is contained in:
Guilleag01
2023-08-09 14:31:36 +02:00
parent e5d17b6a7d
commit fbb9081e70
8 changed files with 139 additions and 88 deletions

View File

@@ -3,7 +3,7 @@ use wasm_bindgen::prelude::*;
use web_sys::HtmlInputElement; use web_sys::HtmlInputElement;
// use wasm_bindgen_futures::spawn_local; // use wasm_bindgen_futures::spawn_local;
use yew::{prelude::*, html::Scope}; use yew::{html::Scope, prelude::*};
// use log::info; // use log::info;
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
@@ -36,7 +36,7 @@ pub struct App {
height: usize, height: usize,
width: usize, width: usize,
num_mines: usize, num_mines: usize,
show_settings: bool show_settings: bool,
} }
impl Component for App { impl Component for App {
@@ -58,13 +58,17 @@ impl Component for App {
height, height,
width, width,
num_mines, num_mines,
show_settings: false show_settings: false,
} }
} }
fn view(&self, _ctx: &Context<Self>) -> Html { fn view(&self, _ctx: &Context<Self>) -> Html {
let b = self.game.get_board().clone(); let b = self.game.get_board().clone();
let style = format!("height: {}px; transition: height 1s;", if !self.show_settings {0} else {98}).to_string(); let style = format!(
"height: {}px; transition: height 1s;",
if !self.show_settings { 0 } else { 98 }
)
.to_string();
html! { html! {
<main class="container"> <main class="container">
@@ -133,9 +137,17 @@ impl Component for App {
let d = web_sys::window().unwrap().document().unwrap(); let d = web_sys::window().unwrap().document().unwrap();
let get_mines = || { let get_mines = || {
let text = d.get_element_by_id("mines-input").unwrap().dyn_into::<HtmlInputElement>().unwrap().value(); let text = d
.get_element_by_id("mines-input")
.unwrap()
.dyn_into::<HtmlInputElement>()
.unwrap()
.value();
let re = Regex::new("^((0+)|((0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)))$").unwrap(); let re = Regex::new(
"^((0+)|((0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)))$",
)
.unwrap();
let mut value: usize = self.num_mines; let mut value: usize = self.num_mines;
if re.is_match(&text) { if re.is_match(&text) {
value = text.parse().unwrap(); value = text.parse().unwrap();
@@ -146,21 +158,31 @@ impl Component for App {
match msg { match msg {
Msg::Discover { cell } => { Msg::Discover { cell } => {
self.game.show(cell.get_pos()); self.game.show(cell.get_pos());
}, }
Msg::Flag { cell } => { Msg::Flag { cell } => {
self.game.set_flag(cell.get_pos(), !self.game.get_cell(cell.get_pos()).is_flagged()); self.game.set_flag(
}, cell.get_pos(),
!self.game.get_cell(cell.get_pos()).is_flagged(),
);
}
Msg::Reset => { Msg::Reset => {
self.game = Game::new(self.height, self.width, self.num_mines); self.game = Game::new(self.height, self.width, self.num_mines);
self.game.start_board(); self.game.start_board();
}, }
Msg::ToggleSettings => { Msg::ToggleSettings => {
self.show_settings = !self.show_settings; self.show_settings = !self.show_settings;
}, }
Msg::UpdateHeight => { Msg::UpdateHeight => {
let text = d.get_element_by_id("height-input").unwrap().dyn_into::<HtmlInputElement>().unwrap().value(); let text = d
.get_element_by_id("height-input")
.unwrap()
.dyn_into::<HtmlInputElement>()
.unwrap()
.value();
let re = Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$").unwrap(); let re =
Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$")
.unwrap();
if re.is_match(&text) { if re.is_match(&text) {
self.height = text.parse().unwrap(); self.height = text.parse().unwrap();
let mines = get_mines(); let mines = get_mines();
@@ -168,11 +190,18 @@ impl Component for App {
self.game = Game::new(self.height, self.width, self.num_mines); self.game = Game::new(self.height, self.width, self.num_mines);
self.game.start_board(); self.game.start_board();
} }
}, }
Msg::UpdateWidth => { Msg::UpdateWidth => {
let text = d.get_element_by_id("width-input").unwrap().dyn_into::<HtmlInputElement>().unwrap().value(); let text = d
.get_element_by_id("width-input")
.unwrap()
.dyn_into::<HtmlInputElement>()
.unwrap()
.value();
let re = Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$").unwrap(); let re =
Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$")
.unwrap();
if re.is_match(&text) { if re.is_match(&text) {
self.width = text.parse().unwrap(); self.width = text.parse().unwrap();
let mines = get_mines(); let mines = get_mines();
@@ -180,7 +209,7 @@ impl Component for App {
self.game = Game::new(self.height, self.width, self.num_mines); self.game = Game::new(self.height, self.width, self.num_mines);
self.game.start_board(); self.game.start_board();
} }
}, }
Msg::UpdateMines => { Msg::UpdateMines => {
let mines = get_mines(); let mines = get_mines();
if mines <= self.height * self.width { if mines <= self.height * self.width {

View File

@@ -1,4 +1,4 @@
use yew::{prelude::*, html::Scope}; use yew::{html::Scope, prelude::*};
use crate::minesweeper::{board::Board, cell::Cell}; use crate::minesweeper::{board::Board, cell::Cell};
@@ -11,19 +11,19 @@ pub struct BoardComponent {
link: Scope<Self>, link: Scope<Self>,
board: Board, board: Board,
onsignal: Callback<Cell>, onsignal: Callback<Cell>,
flagsignal: Callback<Cell> flagsignal: Callback<Cell>,
} }
pub enum Msg { pub enum Msg {
Discover { cell: Cell }, Discover { cell: Cell },
Flag{ cell: Cell } Flag { cell: Cell },
} }
#[derive(Clone, PartialEq, Properties)] #[derive(Clone, PartialEq, Properties)]
pub struct Props { pub struct Props {
pub board: Board, pub board: Board,
pub onsignal: Callback<Cell>, pub onsignal: Callback<Cell>,
pub flagsignal: Callback<Cell> pub flagsignal: Callback<Cell>,
} }
impl Component for BoardComponent { impl Component for BoardComponent {
@@ -36,12 +36,11 @@ impl Component for BoardComponent {
link: ctx.link().clone(), link: ctx.link().clone(),
board: ctx.props().board.clone(), board: ctx.props().board.clone(),
onsignal: ctx.props().onsignal.clone(), onsignal: ctx.props().onsignal.clone(),
flagsignal: ctx.props().flagsignal.clone() flagsignal: ctx.props().flagsignal.clone(),
} }
} }
fn view(&self, ctx: &Context<Self>) -> Html { fn view(&self, ctx: &Context<Self>) -> Html {
let b = ctx.props().board.get_board().clone(); let b = ctx.props().board.get_board().clone();
let height = self.board.get_height(); let height = self.board.get_height();
@@ -88,5 +87,4 @@ impl Component for BoardComponent {
self.flagsignal = ctx.props().flagsignal.clone(); self.flagsignal = ctx.props().flagsignal.clone();
true true
} }
} }

View File

@@ -1,7 +1,7 @@
// use std::fmt::format; // use std::fmt::format;
use yew::{prelude::*, html::Scope};
use crate::minesweeper::cell::Cell; use crate::minesweeper::cell::Cell;
use yew::{html::Scope, prelude::*};
// use log::info; // use log::info;
// use wasm_bindgen::JsValue; // use wasm_bindgen::JsValue;
@@ -17,7 +17,7 @@ pub struct Button {
pub enum Msg { pub enum Msg {
Clicked, Clicked,
RightClicked RightClicked,
} }
#[derive(Clone, PartialEq, Properties)] #[derive(Clone, PartialEq, Properties)]
@@ -26,7 +26,7 @@ pub struct Props {
pub width: usize, pub width: usize,
pub height: usize, pub height: usize,
pub onsignal: Callback<Cell>, pub onsignal: Callback<Cell>,
pub flagsignal: Callback<Cell> pub flagsignal: Callback<Cell>,
} }
impl Component for Button { impl Component for Button {
@@ -40,17 +40,33 @@ impl Component for Button {
onsignal: ctx.props().onsignal.clone(), onsignal: ctx.props().onsignal.clone(),
flagsignal: ctx.props().flagsignal.clone(), flagsignal: ctx.props().flagsignal.clone(),
width: ctx.props().width.to_owned(), width: ctx.props().width.to_owned(),
height: ctx.props().height.to_owned() height: ctx.props().height.to_owned(),
} }
} }
fn view(&self, _ctx: &Context<Self>) -> Html { fn view(&self, _ctx: &Context<Self>) -> Html {
let style_width = format!(
let style_width = format!("width: calc(max(min({}vw - ({}vw), {}vh - ({}vh) - ({}px)), 25px)); ", (100 / self.width) as f32, (40 / self.width) as f32, (100 / self.height) as f32, (20 / self.height) as f32, (110 / self.height) as f32); "width: calc(max(min({}vw - ({}vw), {}vh - ({}vh) - ({}px)), 25px)); ",
let style_font = format!("font-size: calc(0.5 * max(min({}vw - ({}vw), {}vh - ({}vh) - ({}px)), 20px)); ", (100 / self.width) as f32, (40 / self.width) as f32, (100 / self.height) as f32, (20 / self.height) as f32, (110 / self.height) as f32); (100 / self.width) as f32,
(40 / self.width) as f32,
(100 / self.height) as f32,
(20 / self.height) as f32,
(110 / self.height) as f32
);
let style_font = format!(
"font-size: calc(0.5 * max(min({}vw - ({}vw), {}vh - ({}vh) - ({}px)), 20px)); ",
(100 / self.width) as f32,
(40 / self.width) as f32,
(100 / self.height) as f32,
(20 / self.height) as f32,
(110 / self.height) as f32
);
// 100% = 20hv + (2 * n + 1) + n * x // 100% = 20hv + (2 * n + 1) + n * x
let mut style = if !self.cell.is_hidden() {format!("background-color: #2f2f2f; color: #ffffff; transition: background-color 0.5s, color 0.5s; transition-delay: {}s, {}s; ", self.cell.get_delay(), self.cell.get_delay())} else {"".to_string()}; let mut style = if !self.cell.is_hidden() {
format!("background-color: #2f2f2f; color: #ffffff; transition: background-color 0.5s, color 0.5s; transition-delay: {}s, {}s; ", self.cell.get_delay(), self.cell.get_delay())
} else {
"".to_string()
};
style.push_str(&style_width); style.push_str(&style_width);
style.push_str(&style_font); style.push_str(&style_font);
@@ -87,7 +103,7 @@ impl Component for Button {
} }
Msg::RightClicked => { Msg::RightClicked => {
self.flagsignal.emit(self.cell); self.flagsignal.emit(self.cell);
}, }
} }
true true
} }

View File

@@ -1,2 +1,2 @@
pub mod button;
pub mod board; pub mod board;
pub mod button;

View File

@@ -1,2 +1,2 @@
pub mod minesweeper;
pub mod components; pub mod components;
pub mod minesweeper;

View File

@@ -1,4 +1,3 @@
use super::cell::Cell; use super::cell::Cell;
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
@@ -6,7 +5,7 @@ pub struct Board {
board: Vec<Vec<Cell>>, board: Vec<Vec<Cell>>,
height: usize, height: usize,
width: usize, width: usize,
num_mines: usize num_mines: usize,
} }
impl Board { impl Board {
@@ -23,7 +22,7 @@ impl Board {
board: t, board: t,
height, height,
width, width,
num_mines num_mines,
} }
} }
@@ -32,7 +31,11 @@ impl Board {
for i in -1..=1 { for i in -1..=1 {
for j in -1..=1 { for j in -1..=1 {
let new_pos: (isize, isize) = ((pos.0 as isize) + i, (pos.1 as isize) + j); let new_pos: (isize, isize) = ((pos.0 as isize) + i, (pos.1 as isize) + j);
if new_pos.0 >= 0 && new_pos.0 < (self.height as isize) && new_pos.1 >= 0 && new_pos.1 < (self.width as isize) { if new_pos.0 >= 0
&& new_pos.0 < (self.height as isize)
&& new_pos.1 >= 0
&& new_pos.1 < (self.width as isize)
{
value += self.is_mine((new_pos.0 as usize, new_pos.1 as usize)) as usize; value += self.is_mine((new_pos.0 as usize, new_pos.1 as usize)) as usize;
} }
} }

View File

@@ -5,7 +5,7 @@ pub struct Cell {
value: usize, value: usize,
hidden: bool, hidden: bool,
flagged: bool, flagged: bool,
delay: f32 delay: f32,
} }
impl Cell { impl Cell {
@@ -16,7 +16,7 @@ impl Cell {
value: 0, value: 0,
hidden: true, hidden: true,
flagged: false, flagged: false,
delay: 0.0 delay: 0.0,
} }
} }
@@ -68,7 +68,6 @@ impl Cell {
pub fn set_delay(&mut self, delay: f32) { pub fn set_delay(&mut self, delay: f32) {
self.delay = delay; self.delay = delay;
} }
} }
impl ToString for Cell { impl ToString for Cell {

View File

@@ -1,5 +1,5 @@
pub mod cell;
pub mod board; pub mod board;
pub mod cell;
use board::Board; use board::Board;
use cell::Cell; use cell::Cell;
@@ -27,7 +27,10 @@ impl Game {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
while added_mines < self.board.get_num_mines() { 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())); let pos = (
rng.gen_range(0..self.board.get_height()),
rng.gen_range(0..self.board.get_width()),
);
if !self.board.is_mine(pos) { if !self.board.is_mine(pos) {
self.board.set_mine(pos, true); self.board.set_mine(pos, true);
added_mines += 1; added_mines += 1;
@@ -36,7 +39,8 @@ impl Game {
for i in 0..self.board.get_height() { for i in 0..self.board.get_height() {
for j in 0..self.board.get_width() { for j in 0..self.board.get_width() {
self.board.set_value((i, j), self.board.calculate_value((i, j))) self.board
.set_value((i, j), self.board.calculate_value((i, j)))
} }
} }
} }
@@ -66,12 +70,14 @@ impl Game {
let pos = cells_to_show[k]; let pos = cells_to_show[k];
for i in -1..=1 { for i in -1..=1 {
for j in -1..=1 { for j in -1..=1 {
let new_pos = ((pos.0 as isize + i) as usize, (pos.1 as isize + j) as usize); let new_pos =
if pos.0 as isize + i < 0 || ((pos.0 as isize + i) as usize, (pos.1 as isize + j) as usize);
pos.0 as isize + i >= self.get_height() as isize || if pos.0 as isize + i < 0
pos.1 as isize + j < 0 || || pos.0 as isize + i >= self.get_height() as isize
pos.1 as isize + j >= self.get_width() as isize || || pos.1 as isize + j < 0
!self.board.get_cell(new_pos).is_hidden() { || pos.1 as isize + j >= self.get_width() as isize
|| !self.board.get_cell(new_pos).is_hidden()
{
continue; continue;
} }
if self.board.get_cell(new_pos).get_value() == 0 { if self.board.get_cell(new_pos).get_value() == 0 {