Reset button
This commit is contained in:
55
src/app.rs
55
src/app.rs
@@ -1,16 +1,14 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
// use serde_wasm_bindgen::to_value;
|
// use serde_wasm_bindgen::to_value;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
// use wasm_bindgen_futures::spawn_local;
|
// use wasm_bindgen_futures::spawn_local;
|
||||||
use yew::{prelude::*, html::Scope};
|
use yew::{prelude::*, html::Scope};
|
||||||
|
|
||||||
use log::info;
|
// use log::info;
|
||||||
use wasm_bindgen::JsValue;
|
// use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
use minesweeper_ui::components::{button::Button, board::BoardComponent};
|
use minesweeper_ui::components::board::BoardComponent;
|
||||||
|
|
||||||
use minesweeper_ui::minesweeper::{cell::Cell, board::Board, Game};
|
use minesweeper_ui::minesweeper::{cell::Cell, Game};
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -20,12 +18,16 @@ extern "C" {
|
|||||||
|
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
Discover{ cell: Cell },
|
Discover{ cell: Cell },
|
||||||
Flag{ cell: Cell }
|
Flag{ cell: Cell },
|
||||||
|
Reset
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
link: Scope<Self>,
|
link: Scope<Self>,
|
||||||
game: Game
|
game: Game,
|
||||||
|
height: usize,
|
||||||
|
width: usize,
|
||||||
|
num_mines: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component for App {
|
impl Component for App {
|
||||||
@@ -34,32 +36,47 @@ impl Component for App {
|
|||||||
type Properties = ();
|
type Properties = ();
|
||||||
|
|
||||||
fn create(ctx: &Context<Self>) -> Self {
|
fn create(ctx: &Context<Self>) -> Self {
|
||||||
let height = 100;
|
let height = 30;
|
||||||
let width = 41;
|
let width = 30;
|
||||||
let num_mines =(height * width / 10) as usize;
|
let num_mines =(height * width / 10) as usize;
|
||||||
|
|
||||||
let mut game = Game::new(height, width, num_mines);
|
let mut game = Game::new(height, width, 5);
|
||||||
game.start_board();
|
game.start_board();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
link: ctx.link().clone(),
|
link: ctx.link().clone(),
|
||||||
game
|
game,
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
num_mines
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
html!{
|
html!{
|
||||||
<main class="container">
|
<main class="container">
|
||||||
// Disable context menu
|
// Disable context menu
|
||||||
// <button class="restart-button">Restart</button>
|
<script>
|
||||||
<script>{"document.addEventListener('contextmenu', event => event.preventDefault());"}</script>
|
{"document.addEventListener('contextmenu', event => event.preventDefault());"}
|
||||||
<BoardComponent onsignal={self.link.callback(|cell| Msg::Discover{cell})} flagsignal={self.link.callback(|cell| Msg::Flag{cell})} board={b}/>
|
</script>
|
||||||
|
|
||||||
|
<div class="upper-menu">
|
||||||
|
<button class="button-reset"
|
||||||
|
onclick={self.link.callback(|_| Msg::Reset)}>{"Reset"}</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="game">
|
||||||
|
</div>
|
||||||
|
<BoardComponent
|
||||||
|
onsignal={self.link.callback(|cell| Msg::Discover{cell})}
|
||||||
|
flagsignal={self.link.callback(|cell| Msg::Flag{cell})}
|
||||||
|
board={b}/>
|
||||||
</main>
|
</main>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Discover {cell} => {
|
Msg::Discover {cell} => {
|
||||||
self.game.show(cell.get_pos());
|
self.game.show(cell.get_pos());
|
||||||
@@ -67,6 +84,10 @@ impl Component for App {
|
|||||||
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 => {
|
||||||
|
self.game = Game::new(self.height, self.width, self.num_mines);
|
||||||
|
self.game.start_board();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use crate::minesweeper::{board::Board, cell::Cell};
|
|||||||
|
|
||||||
use crate::components::button::Button;
|
use crate::components::button::Button;
|
||||||
|
|
||||||
use log::info;
|
// use log::info;
|
||||||
use wasm_bindgen::JsValue;
|
// use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
pub struct BoardComponent {
|
pub struct BoardComponent {
|
||||||
link: Scope<Self>,
|
link: Scope<Self>,
|
||||||
@@ -62,7 +62,7 @@ impl Component for BoardComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Discover {cell} => {
|
Msg::Discover {cell} => {
|
||||||
self.onsignal.emit(cell);
|
self.onsignal.emit(cell);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use yew::{prelude::*, html::Scope};
|
use yew::{prelude::*, html::Scope};
|
||||||
use crate::minesweeper::cell::Cell;
|
use crate::minesweeper::cell::Cell;
|
||||||
|
|
||||||
use log::info;
|
// use log::info;
|
||||||
use wasm_bindgen::JsValue;
|
// use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
pub struct Button {
|
pub struct Button {
|
||||||
link: Scope<Self>,
|
link: Scope<Self>,
|
||||||
@@ -36,7 +36,7 @@ impl Component for Button {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self, ctx: &Context<Self>) -> Html {
|
fn view(&self, _ctx: &Context<Self>) -> Html {
|
||||||
let mut style = String::new();
|
let mut style = String::new();
|
||||||
if !self.cell.is_hidden() {
|
if !self.cell.is_hidden() {
|
||||||
style = 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());
|
style = 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());
|
||||||
@@ -46,7 +46,7 @@ impl Component for Button {
|
|||||||
<button
|
<button
|
||||||
class={if self.cell.is_hidden() {"button-hidden"} else {"button-shown"}}
|
class={if self.cell.is_hidden() {"button-hidden"} else {"button-shown"}}
|
||||||
onclick={self.link.callback(|_| Msg::Clicked)}
|
onclick={self.link.callback(|_| Msg::Clicked)}
|
||||||
oncontextmenu={self.link.callback(|e: MouseEvent| {Msg::RightClicked})}
|
oncontextmenu={self.link.callback(|_| {Msg::RightClicked})}
|
||||||
style={style}>
|
style={style}>
|
||||||
if self.cell.is_flagged() {
|
if self.cell.is_flagged() {
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-pennant-filled" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-pennant-filled" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
@@ -70,7 +70,7 @@ impl Component for Button {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Clicked => {
|
Msg::Clicked => {
|
||||||
self.onsignal.emit(self.cell);
|
self.onsignal.emit(self.cell);
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ use cell::Cell;
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
// use getrandom::getrandom;
|
// use getrandom::getrandom;
|
||||||
|
|
||||||
use log::info;
|
// use log::info;
|
||||||
use wasm_bindgen::JsValue;
|
// use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
board: Board,
|
board: Board,
|
||||||
|
|||||||
Reference in New Issue
Block a user