No mines on first interaction
This commit is contained in:
@@ -7,7 +7,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
yew = { version = "0.20", features = ["csr"] }
|
yew = { version = "0.20", features = ["csr"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde-wasm-bindgen = "0.4"
|
serde-wasm-bindgen = "0.5.0"
|
||||||
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
|
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
|
||||||
wasm-bindgen-futures = "0.4"
|
wasm-bindgen-futures = "0.4"
|
||||||
web-sys = "0.3"
|
web-sys = "0.3"
|
||||||
|
|||||||
16
src/app.rs
16
src/app.rs
@@ -49,8 +49,8 @@ impl Component for App {
|
|||||||
let width = 10;
|
let width = 10;
|
||||||
let num_mines = height * width / 10;
|
let num_mines = height * width / 10;
|
||||||
|
|
||||||
let mut game = Game::new(height, width, 5);
|
let game = Game::new(height, width, 5);
|
||||||
game.start_board();
|
// game.start_board();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
link: ctx.link().clone(),
|
link: ctx.link().clone(),
|
||||||
@@ -157,6 +157,10 @@ impl Component for App {
|
|||||||
|
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Discover { cell } => {
|
Msg::Discover { cell } => {
|
||||||
|
if !self.game.get_fist_interaction() {
|
||||||
|
self.game.start_board(cell.get_pos());
|
||||||
|
self.game.set_fist_interaction(true);
|
||||||
|
}
|
||||||
self.game.show(cell.get_pos());
|
self.game.show(cell.get_pos());
|
||||||
}
|
}
|
||||||
Msg::Flag { cell } => {
|
Msg::Flag { cell } => {
|
||||||
@@ -167,7 +171,7 @@ impl Component for App {
|
|||||||
}
|
}
|
||||||
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;
|
||||||
@@ -188,7 +192,7 @@ impl Component for App {
|
|||||||
let mines = get_mines();
|
let mines = get_mines();
|
||||||
self.num_mines = mines.min(self.height * self.width);
|
self.num_mines = mines.min(self.height * self.width);
|
||||||
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 => {
|
||||||
@@ -207,7 +211,7 @@ impl Component for App {
|
|||||||
let mines = get_mines();
|
let mines = get_mines();
|
||||||
self.num_mines = mines.min(self.height * self.width);
|
self.num_mines = mines.min(self.height * self.width);
|
||||||
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 => {
|
||||||
@@ -215,7 +219,7 @@ impl Component for App {
|
|||||||
if mines <= self.height * self.width {
|
if mines <= self.height * self.width {
|
||||||
self.num_mines = mines;
|
self.num_mines = mines;
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,32 +11,57 @@ use rand::Rng;
|
|||||||
|
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
board: Board,
|
board: Board,
|
||||||
|
first_interaction: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn new(height: usize, width: usize, num_mines: usize) -> Self {
|
pub fn new(height: usize, width: usize, num_mines: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
board: Board::new(height, width, num_mines),
|
board: Board::new(height, width, num_mines),
|
||||||
|
first_interaction: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_board(&mut self) {
|
pub fn start_board(&mut self, init_pos: (usize, usize)) {
|
||||||
// TODO: make a better implementation
|
// TODO: make a better implementation
|
||||||
let mut added_mines = 0;
|
// let mut added_mines = 0;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
while added_mines < self.board.get_num_mines() {
|
let mut possible_pos: Vec<(usize, usize)> = Vec::new();
|
||||||
let pos = (
|
|
||||||
rng.gen_range(0..self.board.get_height()),
|
for i in 0..self.board.get_height() {
|
||||||
rng.gen_range(0..self.board.get_width()),
|
for j in 0..self.board.get_width() {
|
||||||
);
|
possible_pos.push((i, j));
|
||||||
if !self.board.is_mine(pos) {
|
|
||||||
self.board.set_mine(pos, true);
|
|
||||||
added_mines += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.board.get_num_mines() < self.get_height() * self.get_width() {
|
||||||
|
possible_pos.remove(
|
||||||
|
possible_pos
|
||||||
|
.iter()
|
||||||
|
.position(|value| *value == init_pos)
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..self.board.get_num_mines() {
|
||||||
|
let pos = rng.gen_range(0..possible_pos.len());
|
||||||
|
self.board.set_mine(possible_pos[pos], true);
|
||||||
|
possible_pos.remove(pos);
|
||||||
|
}
|
||||||
|
|
||||||
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
|
self.board
|
||||||
@@ -118,6 +143,14 @@ impl Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_flag(&mut self, pos: (usize, usize), flag: bool) {
|
pub fn set_flag(&mut self, pos: (usize, usize), flag: bool) {
|
||||||
self.board.set_flag(pos, flag);
|
self.board.set_flag(pos, flag)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_fist_interaction(&self) -> bool {
|
||||||
|
self.first_interaction
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_fist_interaction(&mut self, first_interaction: bool) {
|
||||||
|
self.first_interaction = first_interaction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,9 @@
|
|||||||
font-weight: 400;
|
font-weight: 400;
|
||||||
color: #0f0f0f98;
|
color: #0f0f0f98;
|
||||||
background-color: #2f2f2f;
|
background-color: #2f2f2f;
|
||||||
/* position: absolute; */
|
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: auto;
|
width: auto;
|
||||||
display: inline-block;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
|
||||||
@@ -20,11 +19,11 @@
|
|||||||
|
|
||||||
.container {
|
.container {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
width: fit-content;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
padding: 10vh;
|
padding: 10vh;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
width: auto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
button {
|
button {
|
||||||
@@ -70,7 +69,6 @@ button {
|
|||||||
.board {
|
.board {
|
||||||
display: flexbox;
|
display: flexbox;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
/* width: calc((min(10vw - (2vw), 10vh - (2vh) - (110px / 10)) + (1px * 10)) * 10); */
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user