some code quality fixes

This commit is contained in:
Guilleag01
2023-08-09 14:19:22 +02:00
parent cc78457aee
commit e5d17b6a7d
5 changed files with 35 additions and 36 deletions

View File

@@ -1,5 +1,3 @@
#[warn(unused_assignments)]
// use serde_wasm_bindgen::to_value; // use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::HtmlInputElement; use web_sys::HtmlInputElement;
@@ -49,7 +47,7 @@ impl Component for App {
fn create(ctx: &Context<Self>) -> Self { fn create(ctx: &Context<Self>) -> Self {
let height = 10; let height = 10;
let width = 10; let width = 10;
let num_mines =(height * width / 10) as usize; let num_mines = height * width / 10;
let mut game = Game::new(height, width, 5); let mut game = Game::new(height, width, 5);
game.start_board(); game.start_board();
@@ -142,7 +140,7 @@ impl Component for App {
if re.is_match(&text) { if re.is_match(&text) {
value = text.parse().unwrap(); value = text.parse().unwrap();
} }
return value; value
}; };
match msg { match msg {

View File

@@ -1,4 +1,4 @@
use std::fmt::format; // use std::fmt::format;
use yew::{prelude::*, html::Scope}; use yew::{prelude::*, html::Scope};
use crate::minesweeper::cell::Cell; use crate::minesweeper::cell::Cell;
@@ -36,11 +36,11 @@ impl Component for Button {
fn create(ctx: &Context<Self>) -> Self { fn create(ctx: &Context<Self>) -> Self {
Self { Self {
link: ctx.link().clone(), link: ctx.link().clone(),
cell: ctx.props().cell.clone(), cell: ctx.props().cell.to_owned(),
onsignal: ctx.props().onsignal.clone(), onsignal: ctx.props().onsignal.clone(),
flagsignal: ctx.props().flagsignal.clone(), flagsignal: ctx.props().flagsignal.clone(),
width: ctx.props().width.clone(), width: ctx.props().width.to_owned(),
height: ctx.props().height.clone() height: ctx.props().height.to_owned()
} }
} }
@@ -93,11 +93,11 @@ impl Component for Button {
} }
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool { fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
self.cell = ctx.props().cell.clone(); self.cell = ctx.props().cell.to_owned();
self.onsignal = ctx.props().onsignal.clone(); self.onsignal = ctx.props().onsignal.clone();
self.flagsignal = ctx.props().flagsignal.clone(); self.flagsignal = ctx.props().flagsignal.clone();
self.width = ctx.props().width.clone(); self.width = ctx.props().width.to_owned();
self.height = ctx.props().height.clone(); self.height = ctx.props().height.to_owned();
true true
} }
} }

View File

@@ -19,12 +19,12 @@ impl Board {
} }
} }
return Self { Self {
board: t, board: t,
height, height,
width, width,
num_mines num_mines
}; }
} }
pub fn calculate_value(&self, pos: (usize, usize)) -> usize { pub fn calculate_value(&self, pos: (usize, usize)) -> usize {
@@ -37,11 +37,11 @@ impl Board {
} }
} }
} }
return value; value
} }
pub fn get_board(&self) -> &Vec<Vec<Cell>> { pub fn get_board(&self) -> &Vec<Vec<Cell>> {
return &self.board; &self.board
} }
pub fn set_mine(&mut self, pos: (usize, usize), value: bool) { pub fn set_mine(&mut self, pos: (usize, usize), value: bool) {
@@ -49,31 +49,31 @@ impl Board {
} }
pub fn get_height(&self) -> usize { pub fn get_height(&self) -> usize {
return self.height; self.height
} }
pub fn get_width(&self) -> usize { pub fn get_width(&self) -> usize {
return self.width; self.width
} }
pub fn get_num_mines(&self) -> usize { pub fn get_num_mines(&self) -> usize {
return self.num_mines; self.num_mines
} }
pub fn is_mine(&self, pos: (usize, usize)) -> bool { pub fn is_mine(&self, pos: (usize, usize)) -> bool {
return self.board[pos.0][pos.1].is_mine(); self.board[pos.0][pos.1].is_mine()
} }
pub fn get_value(&self, pos: (usize, usize)) -> usize { pub fn get_value(&self, pos: (usize, usize)) -> usize {
return self.board[pos.0][pos.1].get_value(); self.board[pos.0][pos.1].get_value()
} }
pub fn get_cell(&self, pos: (usize, usize)) -> Cell { pub fn get_cell(&self, pos: (usize, usize)) -> Cell {
return self.board[pos.0][pos.1]; self.board[pos.0][pos.1]
} }
pub fn set_value(&mut self, pos: (usize, usize), new_value: usize) { pub fn set_value(&mut self, pos: (usize, usize), new_value: usize) {
return self.board[pos.0][pos.1].set_value(new_value) self.board[pos.0][pos.1].set_value(new_value)
} }
pub fn show_cell(&mut self, pos: (usize, usize)) { pub fn show_cell(&mut self, pos: (usize, usize)) {
@@ -97,18 +97,18 @@ impl ToString for Board {
// result.push_str(" "); // result.push_str(" ");
result.push_str(i.to_string().as_str()); result.push_str(i.to_string().as_str());
} }
result.push_str("\n"); result.push('\n');
for i in 0..self.height { for i in 0..self.height {
result.push_str(i.to_string().as_str()); result.push_str(i.to_string().as_str());
result.push_str(" "); result.push(' ');
for j in 0..self.width { for j in 0..self.width {
result.push_str(self.get_cell((i, j)).to_string().as_str()); result.push_str(self.get_cell((i, j)).to_string().as_str());
} }
result.push_str("\n"); result.push(' ');
} }
return result; result
} }
} }

View File

@@ -10,22 +10,22 @@ pub struct Cell {
impl Cell { impl Cell {
pub fn new(pos: (usize, usize)) -> Self { pub fn new(pos: (usize, usize)) -> Self {
return Self { Self {
pos, pos,
mine: false, mine: false,
value: 0, value: 0,
hidden: true, hidden: true,
flagged: false, flagged: false,
delay: 0.0 delay: 0.0
}; }
} }
pub fn get_pos(&self) -> (usize, usize) { pub fn get_pos(&self) -> (usize, usize) {
return self.pos; self.pos
} }
pub fn is_mine(&self) -> bool { pub fn is_mine(&self) -> bool {
return self.mine; self.mine
} }
pub fn set_mine(&mut self, new_mine: bool) { pub fn set_mine(&mut self, new_mine: bool) {
@@ -33,7 +33,7 @@ impl Cell {
} }
pub fn get_value(&self) -> usize { pub fn get_value(&self) -> usize {
return self.value; self.value
} }
pub fn set_value(&mut self, new_value: usize) { pub fn set_value(&mut self, new_value: usize) {
@@ -46,11 +46,11 @@ impl Cell {
} }
pub fn is_hidden(&self) -> bool { pub fn is_hidden(&self) -> bool {
return self.hidden; self.hidden
} }
pub fn is_flagged(&self) -> bool { pub fn is_flagged(&self) -> bool {
return self.flagged; self.flagged
} }
pub fn set_flag(&mut self, new_flag: bool) { pub fn set_flag(&mut self, new_flag: bool) {
@@ -83,6 +83,6 @@ impl ToString for Cell {
return " ".to_string(); return " ".to_string();
} }
return self.value.to_string(); self.value.to_string()
} }
} }

View File

@@ -47,9 +47,10 @@ impl Game {
return; return;
} }
let mut cells_to_show = Vec::<(usize, usize)>::new(); // 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); // cells_to_show.push(init_pos);
self.board.get_cell(init_pos).set_delay(0.0); self.board.get_cell(init_pos).set_delay(0.0);