Initial commit
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
/dist/
|
||||||
|
/target/
|
||||||
|
/Cargo.lock
|
||||||
3
.taurignore
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
/src
|
||||||
|
/public
|
||||||
|
/Cargo.toml
|
||||||
3
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"]
|
||||||
|
}
|
||||||
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"emmet.includeLanguages": {
|
||||||
|
"rust": "html"
|
||||||
|
}
|
||||||
|
}
|
||||||
17
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "cargo tauri dev",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "cargo", // note: full path to the cargo
|
||||||
|
"args": [
|
||||||
|
"tauri",
|
||||||
|
"dev"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
21
Cargo.toml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
[package]
|
||||||
|
name = "minesweeper-ui"
|
||||||
|
version = "0.0.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[dependencies]
|
||||||
|
yew = { version = "0.20", features = ["csr"] }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde-wasm-bindgen = "0.4"
|
||||||
|
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
|
||||||
|
wasm-bindgen-futures = "0.4"
|
||||||
|
web-sys = "0.3"
|
||||||
|
js-sys = "0.3"
|
||||||
|
getrandom = { version = "0.2.10", features = ["js"] }
|
||||||
|
rand = "0.8.5"
|
||||||
|
wasm-logger = "0.2.0"
|
||||||
|
log = "0.4.19"
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
members = ["src-tauri"]
|
||||||
7
README.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Tauri + Yew
|
||||||
|
|
||||||
|
This template should help get you started developing with Tauri and Yew.
|
||||||
|
|
||||||
|
## Recommended IDE Setup
|
||||||
|
|
||||||
|
[VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer).
|
||||||
10
Trunk.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[build]
|
||||||
|
target = "./index.html"
|
||||||
|
|
||||||
|
[watch]
|
||||||
|
ignore = ["./src-tauri"]
|
||||||
|
|
||||||
|
[serve]
|
||||||
|
address = "127.0.0.1"
|
||||||
|
port = 1420
|
||||||
|
open = false
|
||||||
9
index.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>Tauri + Yew App</title>
|
||||||
|
<link data-trunk rel="css" href="styles.css" />
|
||||||
|
<link data-trunk rel="copy-dir" href="public" />
|
||||||
|
</head>
|
||||||
|
</html>
|
||||||
6
public/tauri.svg
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<svg width="206" height="231" viewBox="0 0 206 231" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M143.143 84C143.143 96.1503 133.293 106 121.143 106C108.992 106 99.1426 96.1503 99.1426 84C99.1426 71.8497 108.992 62 121.143 62C133.293 62 143.143 71.8497 143.143 84Z" fill="#FFC131"/>
|
||||||
|
<ellipse cx="84.1426" cy="147" rx="22" ry="22" transform="rotate(180 84.1426 147)" fill="#24C8DB"/>
|
||||||
|
<path fill-rule="evenodd" clip-rule="evenodd" d="M166.738 154.548C157.86 160.286 148.023 164.269 137.757 166.341C139.858 160.282 141 153.774 141 147C141 144.543 140.85 142.121 140.558 139.743C144.975 138.204 149.215 136.139 153.183 133.575C162.73 127.404 170.292 118.608 174.961 108.244C179.63 97.8797 181.207 86.3876 179.502 75.1487C177.798 63.9098 172.884 53.4021 165.352 44.8883C157.82 36.3744 147.99 30.2165 137.042 27.1546C126.095 24.0926 114.496 24.2568 103.64 27.6274C92.7839 30.998 83.1319 37.4317 75.8437 46.1553C74.9102 47.2727 74.0206 48.4216 73.176 49.5993C61.9292 50.8488 51.0363 54.0318 40.9629 58.9556C44.2417 48.4586 49.5653 38.6591 56.679 30.1442C67.0505 17.7298 80.7861 8.57426 96.2354 3.77762C111.685 -1.01901 128.19 -1.25267 143.769 3.10474C159.348 7.46215 173.337 16.2252 184.056 28.3411C194.775 40.457 201.767 55.4101 204.193 71.404C206.619 87.3978 204.374 103.752 197.73 118.501C191.086 133.25 180.324 145.767 166.738 154.548ZM41.9631 74.275L62.5557 76.8042C63.0459 72.813 63.9401 68.9018 65.2138 65.1274C57.0465 67.0016 49.2088 70.087 41.9631 74.275Z" fill="#FFC131"/>
|
||||||
|
<path fill-rule="evenodd" clip-rule="evenodd" d="M38.4045 76.4519C47.3493 70.6709 57.2677 66.6712 67.6171 64.6132C65.2774 70.9669 64 77.8343 64 85.0001C64 87.1434 64.1143 89.26 64.3371 91.3442C60.0093 92.8732 55.8533 94.9092 51.9599 97.4256C42.4128 103.596 34.8505 112.392 30.1816 122.756C25.5126 133.12 23.9357 144.612 25.6403 155.851C27.3449 167.09 32.2584 177.598 39.7906 186.112C47.3227 194.626 57.153 200.784 68.1003 203.846C79.0476 206.907 90.6462 206.743 101.502 203.373C112.359 200.002 122.011 193.568 129.299 184.845C130.237 183.722 131.131 182.567 131.979 181.383C143.235 180.114 154.132 176.91 164.205 171.962C160.929 182.49 155.596 192.319 148.464 200.856C138.092 213.27 124.357 222.426 108.907 227.222C93.458 232.019 76.9524 232.253 61.3736 227.895C45.7948 223.538 31.8055 214.775 21.0867 202.659C10.3679 190.543 3.37557 175.59 0.949823 159.596C-1.47592 143.602 0.768139 127.248 7.41237 112.499C14.0566 97.7497 24.8183 85.2327 38.4045 76.4519ZM163.062 156.711L163.062 156.711C162.954 156.773 162.846 156.835 162.738 156.897C162.846 156.835 162.954 156.773 163.062 156.711Z" fill="#24C8DB"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 2.5 KiB |
BIN
public/yew.png
Normal file
|
After Width: | Height: | Size: 7.5 KiB |
4
src-tauri/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target/
|
||||||
|
|
||||||
23
src-tauri/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "minesweeper"
|
||||||
|
version = "0.0.0"
|
||||||
|
description = "A Tauri App"
|
||||||
|
authors = ["you"]
|
||||||
|
license = ""
|
||||||
|
repository = ""
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
tauri-build = { version = "1.4", features = [] }
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tauri = { version = "1.4", features = ["shell-open"] }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
# this feature is used for production builds or when `devPath` points to the filesystem
|
||||||
|
# DO NOT REMOVE!!
|
||||||
|
custom-protocol = ["tauri/custom-protocol"]
|
||||||
3
src-tauri/build.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
tauri_build::build()
|
||||||
|
}
|
||||||
BIN
src-tauri/icons/128x128.png
Normal file
|
After Width: | Height: | Size: 3.4 KiB |
BIN
src-tauri/icons/128x128@2x.png
Normal file
|
After Width: | Height: | Size: 6.8 KiB |
BIN
src-tauri/icons/32x32.png
Normal file
|
After Width: | Height: | Size: 974 B |
BIN
src-tauri/icons/Square107x107Logo.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
src-tauri/icons/Square142x142Logo.png
Normal file
|
After Width: | Height: | Size: 3.8 KiB |
BIN
src-tauri/icons/Square150x150Logo.png
Normal file
|
After Width: | Height: | Size: 3.9 KiB |
BIN
src-tauri/icons/Square284x284Logo.png
Normal file
|
After Width: | Height: | Size: 7.6 KiB |
BIN
src-tauri/icons/Square30x30Logo.png
Normal file
|
After Width: | Height: | Size: 903 B |
BIN
src-tauri/icons/Square310x310Logo.png
Normal file
|
After Width: | Height: | Size: 8.4 KiB |
BIN
src-tauri/icons/Square44x44Logo.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
src-tauri/icons/Square71x71Logo.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
BIN
src-tauri/icons/Square89x89Logo.png
Normal file
|
After Width: | Height: | Size: 2.4 KiB |
BIN
src-tauri/icons/StoreLogo.png
Normal file
|
After Width: | Height: | Size: 1.5 KiB |
BIN
src-tauri/icons/icon.icns
Normal file
BIN
src-tauri/icons/icon.ico
Normal file
|
After Width: | Height: | Size: 85 KiB |
BIN
src-tauri/icons/icon.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
15
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||||
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
|
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
||||||
|
#[tauri::command]
|
||||||
|
fn greet(name: &str) -> String {
|
||||||
|
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
tauri::Builder::default()
|
||||||
|
.invoke_handler(tauri::generate_handler![greet])
|
||||||
|
.run(tauri::generate_context!())
|
||||||
|
.expect("error while running tauri application");
|
||||||
|
}
|
||||||
46
src-tauri/tauri.conf.json
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"build": {
|
||||||
|
"beforeDevCommand": "trunk serve",
|
||||||
|
"beforeBuildCommand": "trunk build",
|
||||||
|
"devPath": "http://localhost:1420",
|
||||||
|
"distDir": "../dist",
|
||||||
|
"withGlobalTauri": true
|
||||||
|
},
|
||||||
|
"package": {
|
||||||
|
"productName": "minesweeper",
|
||||||
|
"version": "0.0.0"
|
||||||
|
},
|
||||||
|
"tauri": {
|
||||||
|
"allowlist": {
|
||||||
|
"all": false,
|
||||||
|
"shell": {
|
||||||
|
"all": false,
|
||||||
|
"open": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"bundle": {
|
||||||
|
"active": true,
|
||||||
|
"targets": "all",
|
||||||
|
"identifier": "com.tauri.dev",
|
||||||
|
"icon": [
|
||||||
|
"icons/32x32.png",
|
||||||
|
"icons/128x128.png",
|
||||||
|
"icons/128x128@2x.png",
|
||||||
|
"icons/icon.icns",
|
||||||
|
"icons/icon.ico"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"security": {
|
||||||
|
"csp": null
|
||||||
|
},
|
||||||
|
"windows": [
|
||||||
|
{
|
||||||
|
"fullscreen": false,
|
||||||
|
"resizable": true,
|
||||||
|
"title": "minesweeper",
|
||||||
|
"width": 800,
|
||||||
|
"height": 600
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
77
src/app.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
// use serde_wasm_bindgen::to_value;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
// use web_sys::EventTarget;
|
||||||
|
// use wasm_bindgen_futures::spawn_local;
|
||||||
|
use yew::{prelude::*, html::Scope};
|
||||||
|
|
||||||
|
use log::info;
|
||||||
|
use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
|
use minesweeper_ui::components::{button::Button, board::BoardComponent};
|
||||||
|
|
||||||
|
use minesweeper_ui::minesweeper::{cell::Cell, board::Board, Game};
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
extern "C" {
|
||||||
|
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "tauri"])]
|
||||||
|
async fn invoke(cmd: &str, args: JsValue) -> JsValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Msg {
|
||||||
|
Discover{cell: Cell}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct App {
|
||||||
|
link: Scope<Self>,
|
||||||
|
game: Game
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #[function_component(App)]
|
||||||
|
// pub fn app() -> Html {
|
||||||
|
// html! {
|
||||||
|
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
impl Component for App {
|
||||||
|
type Message = Msg;
|
||||||
|
|
||||||
|
type Properties = ();
|
||||||
|
|
||||||
|
fn create(ctx: &Context<Self>) -> Self {
|
||||||
|
let mut game = Game::new(25, 40, 50);
|
||||||
|
game.start_board();
|
||||||
|
|
||||||
|
info!("\n{}", game.get_board().to_string());
|
||||||
|
|
||||||
|
Self {
|
||||||
|
link: ctx.link().clone(),
|
||||||
|
game
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||||
|
let b = self.game.get_board().clone();
|
||||||
|
|
||||||
|
html!{
|
||||||
|
<main class="container">
|
||||||
|
<BoardComponent onsignal={self.link.callback(|cell| Msg::Discover{cell})} board={b}/>
|
||||||
|
</main>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, ctx: &Context<Self>, 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
77
src/components/board.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
use yew::{prelude::*, html::Scope};
|
||||||
|
|
||||||
|
use crate::minesweeper::{board::Board, cell::Cell};
|
||||||
|
|
||||||
|
use crate::components::button::Button;
|
||||||
|
|
||||||
|
use log::info;
|
||||||
|
use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
|
pub struct BoardComponent {
|
||||||
|
link: Scope<Self>,
|
||||||
|
board: Board,
|
||||||
|
onsignal: Callback<Cell>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Msg {
|
||||||
|
Discover{ cell: Cell },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Properties)]
|
||||||
|
pub struct Props {
|
||||||
|
pub board: Board,
|
||||||
|
pub onsignal: Callback<Cell>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for BoardComponent {
|
||||||
|
type Message = Msg;
|
||||||
|
|
||||||
|
type Properties = Props;
|
||||||
|
|
||||||
|
fn create(ctx: &Context<Self>) -> Self {
|
||||||
|
Self {
|
||||||
|
link: ctx.link().clone(),
|
||||||
|
board: ctx.props().board.clone(),
|
||||||
|
onsignal: ctx.props().onsignal.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||||
|
|
||||||
|
let b = ctx.props().board.get_board().to_owned();
|
||||||
|
|
||||||
|
html! {
|
||||||
|
<div class="board">
|
||||||
|
{b.into_iter().map( |row| {
|
||||||
|
html! {
|
||||||
|
<>
|
||||||
|
{row.into_iter().map(|c| {
|
||||||
|
html! {
|
||||||
|
<Button onsignal={self.link.callback(move |_| Msg::Discover{cell: c})} cell={c}/>
|
||||||
|
}
|
||||||
|
}).collect::<Html>()}
|
||||||
|
<br/>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
}).collect::<Html>()}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||||
|
match msg {
|
||||||
|
Msg::Discover {cell} => {
|
||||||
|
// info!("Pos (from board): {}", format!("{:?}", cell.get_pos()));
|
||||||
|
self.onsignal.emit(cell)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||||
|
self.board = ctx.props().board.clone();
|
||||||
|
self.onsignal = ctx.props().onsignal.clone();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
61
src/components/button.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
|
||||||
|
use yew::{prelude::*, html::Scope};
|
||||||
|
|
||||||
|
use crate::minesweeper::cell::Cell;
|
||||||
|
|
||||||
|
use log::info;
|
||||||
|
use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
|
pub struct Button {
|
||||||
|
link: Scope<Self>,
|
||||||
|
cell: Cell,
|
||||||
|
onsignal: Callback<Cell>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Msg {
|
||||||
|
Clicked,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Properties)]
|
||||||
|
pub struct Props {
|
||||||
|
pub cell: Cell,
|
||||||
|
pub onsignal: Callback<Cell>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for Button {
|
||||||
|
type Message = Msg;
|
||||||
|
type Properties = Props;
|
||||||
|
|
||||||
|
fn create(ctx: &Context<Self>) -> Self {
|
||||||
|
Self {
|
||||||
|
link: ctx.link().clone(),
|
||||||
|
cell: ctx.props().cell.clone(),
|
||||||
|
onsignal: ctx.props().onsignal.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||||
|
|
||||||
|
html!{
|
||||||
|
<button class={if self.cell.is_hidden() {"button-hidden"} else {"button-shown"} } onclick={self.link.callback(|_| Msg::Clicked)}>
|
||||||
|
{ &self.cell.to_string() }
|
||||||
|
</button>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||||
|
match msg {
|
||||||
|
Msg::Clicked => {
|
||||||
|
// info!("hid (from cell): {}", format!("{:?}", self.cell.is_hidden()));
|
||||||
|
self.onsignal.emit(self.cell)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||||
|
self.cell = ctx.props().cell.clone();
|
||||||
|
self.onsignal = ctx.props().onsignal.clone();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/components/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod button;
|
||||||
|
pub mod board;
|
||||||
2
src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod minesweeper;
|
||||||
|
pub mod components;
|
||||||
8
src/main.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
mod app;
|
||||||
|
|
||||||
|
use app::App;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
wasm_logger::init(wasm_logger::Config::default());
|
||||||
|
yew::Renderer::<App>::new().render();
|
||||||
|
}
|
||||||
107
src/minesweeper/board.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
|
||||||
|
use super::cell::Cell;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
pub struct Board {
|
||||||
|
board: Vec<Vec<Cell>>,
|
||||||
|
height: usize,
|
||||||
|
width: usize,
|
||||||
|
num_mines: usize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn new(height: usize, width: usize, num_mines: usize) -> Self {
|
||||||
|
let mut t: Vec<Vec<Cell>> = Vec::new();
|
||||||
|
for i in 0..height {
|
||||||
|
t.push(Vec::new());
|
||||||
|
for j in 0..width {
|
||||||
|
t[i].push(Cell::new((i, j)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Self {
|
||||||
|
board: t,
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
num_mines
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn calculate_value(&self, pos: (usize, usize)) -> usize {
|
||||||
|
let mut value: usize = 0;
|
||||||
|
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) {
|
||||||
|
value += self.is_mine((new_pos.0 as usize, new_pos.1 as usize)) as usize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_board(&self) -> &Vec<Vec<Cell>> {
|
||||||
|
return &self.board;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_mine(&mut self, pos: (usize, usize), value: bool) {
|
||||||
|
self.board[pos.0][pos.1].set_mine(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_height(&self) -> usize {
|
||||||
|
return self.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_width(&self) -> usize {
|
||||||
|
return self.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_num_mines(&self) -> usize {
|
||||||
|
return self.num_mines;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_mine(&self, pos: (usize, usize)) -> bool {
|
||||||
|
return self.board[pos.0][pos.1].is_mine();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_value(&self, pos: (usize, usize)) -> usize {
|
||||||
|
return self.board[pos.0][pos.1].get_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_cell(&self, pos: (usize, usize)) -> Cell {
|
||||||
|
return self.board[pos.0][pos.1];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_value(&mut self, pos: (usize, usize), new_value: usize) {
|
||||||
|
return self.board[pos.0][pos.1].set_value(new_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_cell(&mut self, pos: (usize, usize)) {
|
||||||
|
self.board[pos.0][pos.1].show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToString for Board {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
let mut result = String::new();
|
||||||
|
result.push_str(" ");
|
||||||
|
for i in 0..self.width {
|
||||||
|
// result.push_str(" ");
|
||||||
|
result.push_str(i.to_string().as_str());
|
||||||
|
}
|
||||||
|
result.push_str("\n");
|
||||||
|
|
||||||
|
for i in 0..self.height {
|
||||||
|
result.push_str(i.to_string().as_str());
|
||||||
|
result.push_str(" ");
|
||||||
|
|
||||||
|
for j in 0..self.width {
|
||||||
|
result.push_str(self.get_cell((i, j)).to_string().as_str());
|
||||||
|
}
|
||||||
|
result.push_str("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/minesweeper/cell.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
pub struct Cell {
|
||||||
|
pos: (usize, usize), // (height, width)
|
||||||
|
mine: bool,
|
||||||
|
value: usize,
|
||||||
|
hidden: bool,
|
||||||
|
flagged: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Cell {
|
||||||
|
pub fn new(pos: (usize, usize)) -> Self {
|
||||||
|
return Self {
|
||||||
|
pos,
|
||||||
|
mine: false,
|
||||||
|
value: 0,
|
||||||
|
hidden: true,
|
||||||
|
flagged: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pos(&self) -> (usize, usize) {
|
||||||
|
return self.pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_mine(&self) -> bool {
|
||||||
|
return self.mine;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_mine(&mut self, new_mine: bool) {
|
||||||
|
self.mine = new_mine;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_value(&self) -> usize {
|
||||||
|
return self.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_value(&mut self, new_value: usize) {
|
||||||
|
self.value = new_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show(&mut self) {
|
||||||
|
self.hidden = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_hidden(&self) -> bool {
|
||||||
|
return self.hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_flagged(&self) -> bool {
|
||||||
|
return self.flagged;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_flag(&mut self, new_flag: bool) {
|
||||||
|
self.flagged = new_flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToString for Cell {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
if self.hidden {
|
||||||
|
return " ".to_string();
|
||||||
|
}
|
||||||
|
if self.is_mine() {
|
||||||
|
return "*".to_string();
|
||||||
|
}
|
||||||
|
if self.value == 0 {
|
||||||
|
return " ".to_string();
|
||||||
|
}
|
||||||
|
return self.value.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
74
src/minesweeper/mod.rs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
pub mod cell;
|
||||||
|
pub mod board;
|
||||||
|
|
||||||
|
use board::Board;
|
||||||
|
use cell::Cell;
|
||||||
|
use rand::Rng;
|
||||||
|
// use getrandom::getrandom;
|
||||||
|
|
||||||
|
pub struct Game {
|
||||||
|
board: Board,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Game {
|
||||||
|
pub fn new(height: usize, width: usize, num_mines: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
board: Board::new(height, width, num_mines),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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()));
|
||||||
|
if !self.board.is_mine(pos) {
|
||||||
|
self.board.set_mine(pos, true);
|
||||||
|
added_mines += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show(&mut self, pos: (usize, usize)) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_width(&self) -> usize {
|
||||||
|
return self.board.get_width();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_board(&self) -> &Board {
|
||||||
|
return &self.board;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_cell(&self,pos: (usize, usize)) -> Cell {
|
||||||
|
self.board.get_cell(pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
141
styles.css
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
:root {
|
||||||
|
font-family: Courier , Avenir, Helvetica, Arial, sans-serif;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 24px;
|
||||||
|
font-weight: 400;
|
||||||
|
|
||||||
|
color: #0f0f0f;
|
||||||
|
background-color: #f6f6f6;
|
||||||
|
|
||||||
|
font-synthesis: none;
|
||||||
|
text-rendering: optimizeLegibility;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
-webkit-text-size-adjust: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board{
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
margin: 0;
|
||||||
|
padding: 10vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo {
|
||||||
|
height: 6em;
|
||||||
|
padding: 1.5em;
|
||||||
|
will-change: filter;
|
||||||
|
transition: 0.75s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo.tauri:hover {
|
||||||
|
filter: drop-shadow(0 0 2em #24c8db);
|
||||||
|
}
|
||||||
|
|
||||||
|
.row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
font-weight: 500;
|
||||||
|
color: #646cff;
|
||||||
|
text-decoration: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
color: #535bf2;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-hidden {
|
||||||
|
vertical-align: middle;
|
||||||
|
text-align: center;
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
/* padding: 16px; */
|
||||||
|
/* font-size: 1em; */
|
||||||
|
font-weight: 500;
|
||||||
|
font-family: inherit;
|
||||||
|
color: #ffffff;
|
||||||
|
background-color: #0f0f0f98;
|
||||||
|
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);
|
||||||
|
height: 50px;
|
||||||
|
width: 50px;
|
||||||
|
margin: 2px;
|
||||||
|
-webkit-transition: background-color 0.5s;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-shown {
|
||||||
|
vertical-align: middle;
|
||||||
|
text-align: center;
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
/* padding: 16px; */
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 500;
|
||||||
|
font-family: inherit;
|
||||||
|
color: #ffffff;
|
||||||
|
background-color: #2f2f2f;
|
||||||
|
/* transition: border-color 0.25s; */
|
||||||
|
/* box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2); */
|
||||||
|
height: 50px;
|
||||||
|
width: 50px;
|
||||||
|
margin: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
.button-hidden:hover {
|
||||||
|
background-color: #333333;
|
||||||
|
-webkit-transition: background-color 0.5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
button:active {
|
||||||
|
border-color: #396cd8;
|
||||||
|
background-color: #e8e8e8;
|
||||||
|
}
|
||||||
|
|
||||||
|
input,
|
||||||
|
button {
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#greet-input {
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root {
|
||||||
|
color: #f6f6f6;
|
||||||
|
background-color: #2f2f2f;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
color: #24c8db;
|
||||||
|
}
|
||||||
|
|
||||||
|
input,
|
||||||
|
button {
|
||||||
|
color: #ffffff;
|
||||||
|
background-color: #0f0f0f98;
|
||||||
|
}
|
||||||
|
button:active {
|
||||||
|
background-color: #0f0f0f69;
|
||||||
|
}
|
||||||
|
}
|
||||||