summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorstale <redkugelblitzin@gmail.com>2022-04-27 09:46:35 -0300
committerstale <redkugelblitzin@gmail.com>2022-04-27 09:46:35 -0300
commitf8deba936e62e5c9b4f7487f656c92c253427d42 (patch)
tree9fcae0f8f8793ab83d7181a75ac6bc62be51fe1a /src
parent29596951b18eca8fb5a8dd16c874e01000587847 (diff)
had forgotten the first move immunity
Diffstat (limited to 'src')
-rw-r--r--src/main.rs31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 268fa58..b19eafe 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,14 +2,13 @@ use std::{ io, io::Read };
mod minesweeper {
use std::convert::TryFrom;
- use std::fmt::Display;
use rand::{ thread_rng, Rng, distributions::Uniform };
const HIDDEN_BIT: u8 = 1 << 7;
pub const FLAGGED_BIT: u8 = 1 << 6;
const CORRECT_BIT: u8 = 1 << 5; // grading for a rightly flagged mine
const MINE_VAL: u8 = !(HIDDEN_BIT | FLAGGED_BIT | CORRECT_BIT);
- const NEIGH_OFFS: &[(i8,i8)] = &[
+ const NEIGH_OFFS: &[(isize,isize)] = &[
(-1,-1),(0,-1),(1,-1),
(-1, 0), (1, 0),
(-1, 1),(0, 1),(1, 1),
@@ -17,6 +16,7 @@ mod minesweeper {
#[derive(PartialEq)]
pub enum Phase {
SafeFirstMove,
+ FirstMoveFail,
Run,
Die,
Win,
@@ -51,7 +51,7 @@ mod minesweeper {
pub fn act(mut self, m: Move) -> Self {
let lost_phase = | phase | {
match phase {
- Phase::SafeFirstMove => unimplemented!(),
+ Phase::SafeFirstMove => Phase::FirstMoveFail,
Phase::Run => Phase::Die,
_ => unreachable!(),
}
@@ -74,6 +74,13 @@ mod minesweeper {
if self.board.hidden_tiles == self.mine_count {
self.phase = Phase::Win;
}
+ if self.phase == Phase::FirstMoveFail {
+ self.board = Board::new(self.board.width, self.board.height);
+ self.mine_count -= 1;
+ self.board = self.board.spread_mines(self.mine_count);
+ self.phase = Phase::SafeFirstMove;
+ self = self.act(m);
+ }
self
}
}
@@ -101,8 +108,8 @@ mod minesweeper {
for (nx,ny) in NEIGH_OFFS {
let x = randpos.0;
let y = randpos.1;
- let nxc = usize::try_from(x as i8 + nx);
- let nyc = usize::try_from(y as i8 + ny);
+ let nxc = usize::try_from(isize::try_from(x).unwrap() + nx);
+ let nyc = usize::try_from(isize::try_from(y).unwrap() + ny);
let _ = nxc.and_then(|nx: usize| { nyc.and_then(|ny: usize| {
if nx > w - 1 || ny > h - 1 { return usize::try_from(-1) };
let off = self.pos_to_off((nx,ny));
@@ -131,8 +138,8 @@ mod minesweeper {
if *c > 0 { return }
drop(c);
for (ox,oy) in NEIGH_OFFS {
- let nxr = usize::try_from(pos.0 as i8 + ox);
- let nyr = usize::try_from(pos.1 as i8 + oy);
+ let nxr = usize::try_from(isize::try_from(pos.0).unwrap() + ox);
+ let nyr = usize::try_from(isize::try_from(pos.1).unwrap() + oy);
let _ = nxr.and_then(|nx: usize| { nyr.and_then(|ny: usize| {
if nx > self.width - 1 || ny > self.height - 1 { return usize::try_from(-1) };
self.flood_reveal((nx, ny));
@@ -172,9 +179,11 @@ mod minesweeper {
const ULINE: &[u8] = &[27,b'[',b'4',b'm'];
const REGULAR: &[u8] = &[27,b'[',b'0',b'm'];
- let mut ret = vec![27,b'[',b'2',b'J',27,b'[',b'0',b'm',b'\r'];
+ let mut ret = vec![27,b'[',b'2',b'J',27,b'[',b'0',b'm'];
let mut cur_clr = FG;
for y in 0..self.height {
+ ret.push(b'\r');
+ ret.push(b'\n');
for x in 0..self.width {
let c = &self.data[self.pos_to_off((x,y))];
let is_cursor = if let Some(cursor) = cursor { cursor.0 == x && cursor.1 == y } else { false };
@@ -190,8 +199,6 @@ mod minesweeper {
}
if is_cursor { ret.extend_from_slice(REGULAR); ret.extend_from_slice(cur_clr); }
}
- ret.push(b'\r');
- ret.push(b'\n');
}
ret
}
@@ -203,8 +210,8 @@ use std::convert::{ TryFrom, TryInto };
use std::io::Write;
fn main() -> Result<(), io::Error> {
- let board = Board::new(10,10);
- let mut game = Game::new(board, 10);
+ let board = Board::new(30,10);
+ let mut game = Game::new(board, 300/8);
let mut cursor: (usize, usize) = (0,0);
let stdout = io::stdout();
let mut lstdout = stdout.lock();