formatting
This commit is contained in:
93
src/app.rs
93
src/app.rs
@@ -3,7 +3,7 @@ use wasm_bindgen::prelude::*;
|
||||
use web_sys::HtmlInputElement;
|
||||
|
||||
// use wasm_bindgen_futures::spawn_local;
|
||||
use yew::{prelude::*, html::Scope};
|
||||
use yew::{html::Scope, prelude::*};
|
||||
|
||||
// use log::info;
|
||||
use wasm_bindgen::JsValue;
|
||||
@@ -21,9 +21,9 @@ extern "C" {
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
Discover{ cell: Cell },
|
||||
Flag{ cell: Cell },
|
||||
Reset,
|
||||
Discover { cell: Cell },
|
||||
Flag { cell: Cell },
|
||||
Reset,
|
||||
ToggleSettings,
|
||||
UpdateHeight,
|
||||
UpdateWidth,
|
||||
@@ -36,7 +36,7 @@ pub struct App {
|
||||
height: usize,
|
||||
width: usize,
|
||||
num_mines: usize,
|
||||
show_settings: bool
|
||||
show_settings: bool,
|
||||
}
|
||||
|
||||
impl Component for App {
|
||||
@@ -52,38 +52,42 @@ impl Component for App {
|
||||
let mut game = Game::new(height, width, 5);
|
||||
game.start_board();
|
||||
|
||||
Self {
|
||||
Self {
|
||||
link: ctx.link().clone(),
|
||||
game,
|
||||
height,
|
||||
width,
|
||||
num_mines,
|
||||
show_settings: false
|
||||
show_settings: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self, _ctx: &Context<Self>) -> Html {
|
||||
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">
|
||||
// Disable context menu
|
||||
<script>
|
||||
{"document.addEventListener('contextmenu', event => event.preventDefault());"}
|
||||
</script>
|
||||
|
||||
|
||||
<div class="upper-menu">
|
||||
<div class="menu-buttons">
|
||||
<button class="button-reset"
|
||||
<button class="button-reset"
|
||||
onclick={self.link.callback(|_| Msg::Reset)}>
|
||||
{"Reset"}
|
||||
</button>
|
||||
<div class="time">
|
||||
{"00:00"}
|
||||
</div>
|
||||
<button
|
||||
id="open-settings"
|
||||
<button
|
||||
id="open-settings"
|
||||
class="open-settings"
|
||||
onclick={self.link.callback(|_| Msg::ToggleSettings)}>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-settings-filled" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="#ffffff" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
@@ -120,9 +124,9 @@ impl Component for App {
|
||||
</div>
|
||||
|
||||
<div class="game">
|
||||
<BoardComponent
|
||||
onsignal={self.link.callback(|cell| Msg::Discover{cell})}
|
||||
flagsignal={self.link.callback(|cell| Msg::Flag{cell})}
|
||||
<BoardComponent
|
||||
onsignal={self.link.callback(|cell| Msg::Discover{cell})}
|
||||
flagsignal={self.link.callback(|cell| Msg::Flag{cell})}
|
||||
board={b}/>
|
||||
</div>
|
||||
</main>
|
||||
@@ -133,9 +137,17 @@ impl Component for App {
|
||||
let d = web_sys::window().unwrap().document().unwrap();
|
||||
|
||||
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;
|
||||
if re.is_match(&text) {
|
||||
value = text.parse().unwrap();
|
||||
@@ -144,23 +156,33 @@ impl Component for App {
|
||||
};
|
||||
|
||||
match msg {
|
||||
Msg::Discover {cell} => {
|
||||
Msg::Discover { cell } => {
|
||||
self.game.show(cell.get_pos());
|
||||
},
|
||||
Msg::Flag {cell} => {
|
||||
self.game.set_flag(cell.get_pos(), !self.game.get_cell(cell.get_pos()).is_flagged());
|
||||
},
|
||||
}
|
||||
Msg::Flag { cell } => {
|
||||
self.game.set_flag(
|
||||
cell.get_pos(),
|
||||
!self.game.get_cell(cell.get_pos()).is_flagged(),
|
||||
);
|
||||
}
|
||||
Msg::Reset => {
|
||||
self.game = Game::new(self.height, self.width, self.num_mines);
|
||||
self.game.start_board();
|
||||
},
|
||||
}
|
||||
Msg::ToggleSettings => {
|
||||
self.show_settings = !self.show_settings;
|
||||
},
|
||||
}
|
||||
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) {
|
||||
self.height = text.parse().unwrap();
|
||||
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.start_board();
|
||||
}
|
||||
},
|
||||
}
|
||||
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) {
|
||||
self.width = text.parse().unwrap();
|
||||
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.start_board();
|
||||
}
|
||||
},
|
||||
}
|
||||
Msg::UpdateMines => {
|
||||
let mines = get_mines();
|
||||
if mines <= self.height * self.width {
|
||||
@@ -192,4 +221,4 @@ impl Component for App {
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use yew::{prelude::*, html::Scope};
|
||||
use yew::{html::Scope, prelude::*};
|
||||
|
||||
use crate::minesweeper::{board::Board, cell::Cell};
|
||||
|
||||
@@ -11,19 +11,19 @@ pub struct BoardComponent {
|
||||
link: Scope<Self>,
|
||||
board: Board,
|
||||
onsignal: Callback<Cell>,
|
||||
flagsignal: Callback<Cell>
|
||||
flagsignal: Callback<Cell>,
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
Discover{ cell: Cell },
|
||||
Flag{ cell: Cell }
|
||||
Discover { cell: Cell },
|
||||
Flag { cell: Cell },
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Properties)]
|
||||
pub struct Props {
|
||||
pub board: Board,
|
||||
pub onsignal: Callback<Cell>,
|
||||
pub flagsignal: Callback<Cell>
|
||||
pub flagsignal: Callback<Cell>,
|
||||
}
|
||||
|
||||
impl Component for BoardComponent {
|
||||
@@ -36,12 +36,11 @@ impl Component for BoardComponent {
|
||||
link: ctx.link().clone(),
|
||||
board: ctx.props().board.clone(),
|
||||
onsignal: ctx.props().onsignal.clone(),
|
||||
flagsignal: ctx.props().flagsignal.clone()
|
||||
flagsignal: ctx.props().flagsignal.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||
|
||||
let b = ctx.props().board.get_board().clone();
|
||||
|
||||
let height = self.board.get_height();
|
||||
@@ -54,13 +53,13 @@ impl Component for BoardComponent {
|
||||
<>
|
||||
{row.into_iter().map(|c| {
|
||||
html! {
|
||||
<Button
|
||||
onsignal={self.link.callback(move |_| Msg::Discover{cell: c})}
|
||||
flagsignal={self.link.callback(move |_| Msg::Flag{cell: c})}
|
||||
<Button
|
||||
onsignal={self.link.callback(move |_| Msg::Discover{cell: c})}
|
||||
flagsignal={self.link.callback(move |_| Msg::Flag{cell: c})}
|
||||
cell={c}
|
||||
height={height}
|
||||
width={width}/>
|
||||
}
|
||||
}
|
||||
}).collect::<Html>()}
|
||||
<br/>
|
||||
</>
|
||||
@@ -72,10 +71,10 @@ impl Component for BoardComponent {
|
||||
|
||||
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||
match msg {
|
||||
Msg::Discover {cell} => {
|
||||
Msg::Discover { cell } => {
|
||||
self.onsignal.emit(cell);
|
||||
}
|
||||
Msg::Flag {cell} => {
|
||||
Msg::Flag { cell } => {
|
||||
self.flagsignal.emit(cell);
|
||||
}
|
||||
}
|
||||
@@ -88,5 +87,4 @@ impl Component for BoardComponent {
|
||||
self.flagsignal = ctx.props().flagsignal.clone();
|
||||
true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// use std::fmt::format;
|
||||
|
||||
use yew::{prelude::*, html::Scope};
|
||||
use crate::minesweeper::cell::Cell;
|
||||
use yew::{html::Scope, prelude::*};
|
||||
|
||||
// use log::info;
|
||||
// use wasm_bindgen::JsValue;
|
||||
@@ -17,7 +17,7 @@ pub struct Button {
|
||||
|
||||
pub enum Msg {
|
||||
Clicked,
|
||||
RightClicked
|
||||
RightClicked,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Properties)]
|
||||
@@ -26,7 +26,7 @@ pub struct Props {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub onsignal: Callback<Cell>,
|
||||
pub flagsignal: Callback<Cell>
|
||||
pub flagsignal: Callback<Cell>,
|
||||
}
|
||||
|
||||
impl Component for Button {
|
||||
@@ -40,22 +40,38 @@ impl Component for Button {
|
||||
onsignal: ctx.props().onsignal.clone(),
|
||||
flagsignal: ctx.props().flagsignal.clone(),
|
||||
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 {
|
||||
|
||||
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);
|
||||
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);
|
||||
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
|
||||
);
|
||||
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
|
||||
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_font);
|
||||
|
||||
html!{
|
||||
<button
|
||||
html! {
|
||||
<button
|
||||
class={if self.cell.is_hidden() {"button-hidden"} else {"button-shown"}}
|
||||
onclick={self.link.callback(|_| Msg::Clicked)}
|
||||
oncontextmenu={self.link.callback(|_| {Msg::RightClicked})}
|
||||
@@ -87,7 +103,7 @@ impl Component for Button {
|
||||
}
|
||||
Msg::RightClicked => {
|
||||
self.flagsignal.emit(self.cell);
|
||||
},
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -100,4 +116,4 @@ impl Component for Button {
|
||||
self.height = ctx.props().height.to_owned();
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
pub mod board;
|
||||
pub mod button;
|
||||
pub mod board;
|
||||
@@ -1,2 +1,2 @@
|
||||
pub mod components;
|
||||
pub mod minesweeper;
|
||||
pub mod components;
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
use super::cell::Cell;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
@@ -6,7 +5,7 @@ pub struct Board {
|
||||
board: Vec<Vec<Cell>>,
|
||||
height: usize,
|
||||
width: usize,
|
||||
num_mines: usize
|
||||
num_mines: usize,
|
||||
}
|
||||
|
||||
impl Board {
|
||||
@@ -23,7 +22,7 @@ impl Board {
|
||||
board: t,
|
||||
height,
|
||||
width,
|
||||
num_mines
|
||||
num_mines,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +31,11 @@ impl Board {
|
||||
for i in -1..=1 {
|
||||
for j in -1..=1 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -111,4 +114,4 @@ impl ToString for Board {
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ pub struct Cell {
|
||||
value: usize,
|
||||
hidden: bool,
|
||||
flagged: bool,
|
||||
delay: f32
|
||||
delay: f32,
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
@@ -16,7 +16,7 @@ impl Cell {
|
||||
value: 0,
|
||||
hidden: true,
|
||||
flagged: false,
|
||||
delay: 0.0
|
||||
delay: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ impl Cell {
|
||||
}
|
||||
|
||||
pub fn set_value(&mut self, new_value: usize) {
|
||||
self.value = new_value;
|
||||
self.value = new_value;
|
||||
}
|
||||
|
||||
pub fn show(&mut self) {
|
||||
@@ -68,7 +68,6 @@ impl Cell {
|
||||
pub fn set_delay(&mut self, delay: f32) {
|
||||
self.delay = delay;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl ToString for Cell {
|
||||
@@ -85,4 +84,4 @@ impl ToString for Cell {
|
||||
|
||||
self.value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod cell;
|
||||
pub mod board;
|
||||
pub mod cell;
|
||||
|
||||
use board::Board;
|
||||
use cell::Cell;
|
||||
@@ -20,14 +20,17 @@ impl Game {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_board(&mut self){
|
||||
pub fn start_board(&mut self) {
|
||||
// TODO: make a better implementation
|
||||
let mut added_mines = 0;
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
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) {
|
||||
self.board.set_mine(pos, true);
|
||||
added_mines += 1;
|
||||
@@ -36,7 +39,8 @@ impl Game {
|
||||
|
||||
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)))
|
||||
self.board
|
||||
.set_value((i, j), self.board.calculate_value((i, j)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,29 +53,31 @@ impl Game {
|
||||
|
||||
// let mut cells_to_show = Vec::<(usize, usize)>::new();
|
||||
let mut cells_to_show = Vec::<(usize, usize)>::from([init_pos]);
|
||||
|
||||
|
||||
// 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 {
|
||||
let new_cells = cells_to_show.len() - added_cells;
|
||||
added_cells = 0;
|
||||
|
||||
|
||||
cells_to_show = cells_to_show[new_cells..cells_to_show.len()].to_vec();
|
||||
|
||||
for k in 0..cells_to_show.len() {
|
||||
let pos = cells_to_show[k];
|
||||
for i 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);
|
||||
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(new_pos).is_hidden() {
|
||||
let new_pos =
|
||||
((pos.0 as isize + i) as usize, (pos.1 as isize + j) as usize);
|
||||
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(new_pos).is_hidden()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if self.board.get_cell(new_pos).get_value() == 0 {
|
||||
@@ -82,12 +88,12 @@ impl Game {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +103,7 @@ impl Game {
|
||||
|
||||
pub fn get_width(&self) -> usize {
|
||||
self.board.get_width()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_board(&self) -> &Board {
|
||||
&self.board
|
||||
|
||||
Reference in New Issue
Block a user