|
|
|
@ -1,21 +1,42 @@
|
|
|
|
|
//! Game board logic.
|
|
|
|
|
|
|
|
|
|
use std::fs::read_to_string;
|
|
|
|
|
|
|
|
|
|
const SIZE: usize = 9;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
|
|
|
pub struct Cell {
|
|
|
|
|
pub value: u8,
|
|
|
|
|
pub loaded: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
|
pub struct Gameboard {
|
|
|
|
|
pub cells: [[u8; SIZE]; SIZE],
|
|
|
|
|
pub cells: [[Cell; SIZE]; SIZE],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Gameboard {
|
|
|
|
|
pub fn new() -> Gameboard {
|
|
|
|
|
Gameboard {
|
|
|
|
|
cells: [[0; SIZE]; SIZE],
|
|
|
|
|
cells: [[Cell::default(); SIZE]; SIZE],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_cells(cells: [[u8; SIZE]; SIZE]) -> Gameboard {
|
|
|
|
|
let mut ret = Gameboard::new();
|
|
|
|
|
for (i, row) in cells.iter().enumerate() {
|
|
|
|
|
for (j, &col) in row.iter().enumerate() {
|
|
|
|
|
ret.cells[i][j] = Cell {
|
|
|
|
|
value: col,
|
|
|
|
|
loaded: col != 0,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn char(&self, ind: [usize; 2]) -> Option<char> {
|
|
|
|
|
Some(match self.cells[ind[1]][ind[0]] {
|
|
|
|
|
Some(match self.cells[ind[1]][ind[0]].value {
|
|
|
|
|
1 => '1',
|
|
|
|
|
2 => '2',
|
|
|
|
|
3 => '3',
|
|
|
|
@ -30,14 +51,14 @@ impl Gameboard {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set(&mut self, ind: [usize; 2], val: u8) {
|
|
|
|
|
self.cells[ind[1]][ind[0]] = val;
|
|
|
|
|
if !self.cells[ind[1]][ind[0]].loaded {
|
|
|
|
|
self.cells[ind[1]][ind[0]].value = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load_sdm(filename: &str) -> Self {
|
|
|
|
|
use std::fs::read_to_string;
|
|
|
|
|
|
|
|
|
|
let data = read_to_string(filename).expect("failed to read SDM file");
|
|
|
|
|
let mut cells = [[0; SIZE]; SIZE];
|
|
|
|
|
let mut cells = [[Cell::default(); SIZE]; SIZE];
|
|
|
|
|
let mut row = 0;
|
|
|
|
|
let mut col = 0;
|
|
|
|
|
for c in data.chars() {
|
|
|
|
@ -45,8 +66,12 @@ impl Gameboard {
|
|
|
|
|
col = 0;
|
|
|
|
|
row += 1;
|
|
|
|
|
}
|
|
|
|
|
if let Some(d) = c.to_digit(10) {
|
|
|
|
|
cells[row][col] = d as u8;
|
|
|
|
|
if let Some(v) = c.to_digit(10) {
|
|
|
|
|
let value = v as u8;
|
|
|
|
|
cells[row][col] = Cell {
|
|
|
|
|
value,
|
|
|
|
|
loaded: value != 0,
|
|
|
|
|
};
|
|
|
|
|
col += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -62,19 +87,17 @@ mod tests {
|
|
|
|
|
#[test]
|
|
|
|
|
fn load_sdm() {
|
|
|
|
|
let got = Gameboard::load_sdm("static/puzzle.sdm");
|
|
|
|
|
let want = Gameboard {
|
|
|
|
|
cells: [
|
|
|
|
|
[0, 1, 6, 4, 0, 0, 0, 0, 0],
|
|
|
|
|
[2, 0, 0, 0, 0, 9, 0, 0, 0],
|
|
|
|
|
[4, 0, 0, 0, 0, 0, 0, 6, 2],
|
|
|
|
|
[0, 7, 0, 2, 3, 0, 1, 0, 0],
|
|
|
|
|
[1, 0, 0, 0, 0, 0, 0, 0, 3],
|
|
|
|
|
[0, 0, 3, 0, 8, 7, 0, 4, 0],
|
|
|
|
|
[9, 6, 0, 0, 0, 0, 0, 0, 5],
|
|
|
|
|
[0, 0, 0, 8, 0, 0, 0, 0, 7],
|
|
|
|
|
[0, 0, 0, 0, 0, 6, 8, 2, 0],
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
let want = Gameboard::from_cells([
|
|
|
|
|
[0, 1, 6, 4, 0, 0, 0, 0, 0],
|
|
|
|
|
[2, 0, 0, 0, 0, 9, 0, 0, 0],
|
|
|
|
|
[4, 0, 0, 0, 0, 0, 0, 6, 2],
|
|
|
|
|
[0, 7, 0, 2, 3, 0, 1, 0, 0],
|
|
|
|
|
[1, 0, 0, 0, 0, 0, 0, 0, 3],
|
|
|
|
|
[0, 0, 3, 0, 8, 7, 0, 4, 0],
|
|
|
|
|
[9, 6, 0, 0, 0, 0, 0, 0, 5],
|
|
|
|
|
[0, 0, 0, 8, 0, 0, 0, 0, 7],
|
|
|
|
|
[0, 0, 0, 0, 0, 6, 8, 2, 0],
|
|
|
|
|
]);
|
|
|
|
|
assert_eq!(got, want);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|