add undo/redo functionality; organize and comment
This commit is contained in:
parent
e8b47f31ac
commit
6424063250
@ -4,13 +4,9 @@ use crossterm::event::{poll, read, Event::*, KeyCode::*};
|
|||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
mod state;
|
mod state;
|
||||||
use state::*;
|
|
||||||
|
|
||||||
mod sudoku;
|
mod sudoku;
|
||||||
use sudoku::*;
|
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
use ui::*;
|
use {state::*, sudoku::*, ui::*};
|
||||||
|
|
||||||
use std::{io, time::Duration};
|
use std::{io, time::Duration};
|
||||||
|
|
||||||
@ -81,6 +77,9 @@ fn main() {
|
|||||||
_ => state.preselect_num(num as u8 - b'0'),
|
_ => state.preselect_num(num as u8 - b'0'),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Char('u') | Char('U') => state.undo(),
|
||||||
|
Char('r') | Char('R') => state.redo(),
|
||||||
|
|
||||||
Char('q') | Char('Q') => {
|
Char('q') | Char('Q') => {
|
||||||
screen.deinit().or_crash();
|
screen.deinit().or_crash();
|
||||||
break;
|
break;
|
||||||
|
|||||||
270
src/state.rs
270
src/state.rs
@ -3,62 +3,57 @@ use crate::Dir::*;
|
|||||||
|
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
pub enum Dir {
|
/// the entire game logic state
|
||||||
Up,
|
|
||||||
Down,
|
|
||||||
Left,
|
|
||||||
Right,
|
|
||||||
FarUp,
|
|
||||||
FarDown,
|
|
||||||
FarLeft,
|
|
||||||
FarRight,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, PartialEq, Clone, Copy)]
|
|
||||||
pub enum Mode {
|
|
||||||
#[default]
|
|
||||||
Edit,
|
|
||||||
Markup,
|
|
||||||
Go,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub board: Board,
|
pub board: Board,
|
||||||
pub difficulty: Difficulty,
|
|
||||||
pub modifiable: [[bool; 9]; 9],
|
pub modifiable: [[bool; 9]; 9],
|
||||||
pub markups: [[[bool; 9]; 9]; 9],
|
pub markups: [[[bool; 9]; 9]; 9],
|
||||||
pub start_time: time::Instant,
|
|
||||||
pub mode: Mode,
|
|
||||||
pub next_mode: Mode,
|
|
||||||
pub preselection: u8,
|
pub preselection: u8,
|
||||||
pub cur_row: usize,
|
pub cur_row: usize,
|
||||||
pub cur_col: usize,
|
pub cur_col: usize,
|
||||||
|
|
||||||
|
pub mode: Mode,
|
||||||
|
pub next_mode: Mode,
|
||||||
|
|
||||||
|
pub difficulty: Difficulty,
|
||||||
|
pub start_time: time::Instant,
|
||||||
|
|
||||||
|
pub undo_stack: Vec<UndoStep>,
|
||||||
|
pub redo_stack: Vec<UndoStep>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
/// returns a new `State` with a randomly generated
|
||||||
|
/// sudoku `Board` of the provided `Difficulty`
|
||||||
pub fn init(difficulty: Difficulty) -> Self {
|
pub fn init(difficulty: Difficulty) -> Self {
|
||||||
let board = generate_sudoku(difficulty);
|
let board = generate_sudoku(difficulty);
|
||||||
let modifiable = State::get_modifiables(board);
|
let modifiable = State::init_modifiables(board);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
board,
|
board,
|
||||||
difficulty,
|
|
||||||
modifiable,
|
modifiable,
|
||||||
markups: [[[false; 9]; 9]; 9],
|
markups: [[[false; 9]; 9]; 9],
|
||||||
start_time: time::Instant::now(),
|
|
||||||
mode: Mode::default(),
|
|
||||||
next_mode: Mode::default(),
|
|
||||||
preselection: 1,
|
preselection: 1,
|
||||||
cur_row: 4,
|
cur_row: 4,
|
||||||
cur_col: 4,
|
cur_col: 4,
|
||||||
|
|
||||||
|
mode: Mode::default(),
|
||||||
|
next_mode: Mode::default(),
|
||||||
|
|
||||||
|
difficulty,
|
||||||
|
start_time: time::Instant::now(),
|
||||||
|
|
||||||
|
undo_stack: Vec::with_capacity(160),
|
||||||
|
redo_stack: Vec::with_capacity(80),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_time(&self) -> time::Duration {
|
/// returns a boolean mask of the board, indicating which cells
|
||||||
time::Instant::now() - self.start_time
|
/// can be modified by the user and which are part of the puzzle constraints.
|
||||||
}
|
/// makes only cells that are initialized with the value 0 modifiable
|
||||||
|
fn init_modifiables(board: Board) -> [[bool; 9]; 9] {
|
||||||
fn get_modifiables(board: Board) -> [[bool; 9]; 9] {
|
|
||||||
let mut modifiable = [[false; 9]; 9];
|
let mut modifiable = [[false; 9]; 9];
|
||||||
for (board_row, modifiable_row) in board.iter().zip(modifiable.iter_mut()) {
|
for (board_row, modifiable_row) in board.iter().zip(modifiable.iter_mut()) {
|
||||||
for (board_cell, modifiable_flag) in board_row.iter().zip(modifiable_row.iter_mut()) {
|
for (board_cell, modifiable_flag) in board_row.iter().zip(modifiable_row.iter_mut()) {
|
||||||
@ -74,6 +69,8 @@ impl State {
|
|||||||
self.modifiable[self.cur_row][self.cur_col]
|
self.modifiable[self.cur_row][self.cur_col]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns a mutable reference to the number
|
||||||
|
/// contained in the current cell.
|
||||||
pub fn current_cell(&mut self) -> &mut u8 {
|
pub fn current_cell(&mut self) -> &mut u8 {
|
||||||
&mut self.board[self.cur_row][self.cur_col]
|
&mut self.board[self.cur_row][self.cur_col]
|
||||||
}
|
}
|
||||||
@ -103,7 +100,7 @@ impl State {
|
|||||||
pub fn toggle_current_cell(&mut self) {
|
pub fn toggle_current_cell(&mut self) {
|
||||||
if self.current_cell_is_modifiable() {
|
if self.current_cell_is_modifiable() {
|
||||||
if *self.current_cell() == self.preselection {
|
if *self.current_cell() == self.preselection {
|
||||||
*self.current_cell() = 0;
|
self.delete_current_cell();
|
||||||
} else {
|
} else {
|
||||||
*self.current_cell() = self.preselection;
|
*self.current_cell() = self.preselection;
|
||||||
self.delete_colliding_marks(self.preselection, self.cur_row, self.cur_col);
|
self.delete_colliding_marks(self.preselection, self.cur_row, self.cur_col);
|
||||||
@ -114,15 +111,91 @@ impl State {
|
|||||||
pub fn delete_current_cell(&mut self) {
|
pub fn delete_current_cell(&mut self) {
|
||||||
if self.current_cell_is_modifiable() {
|
if self.current_cell_is_modifiable() {
|
||||||
*self.current_cell() = 0;
|
*self.current_cell() = 0;
|
||||||
|
|
||||||
|
self.push_to_undo_stack(UndoStep::Replace(
|
||||||
|
self.preselection,
|
||||||
|
(self.cur_row as u8, self.cur_col as u8),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// deletes all marks of `num` in it's row, column and block.
|
||||||
|
/// used to automatically remove marks when placing a number that
|
||||||
|
/// invalidates those marks.
|
||||||
pub fn delete_colliding_marks(&mut self, num: u8, row: usize, col: usize) {
|
pub fn delete_colliding_marks(&mut self, num: u8, row: usize, col: usize) {
|
||||||
|
let mut deleted = [None; 27];
|
||||||
|
let mut pos = 0;
|
||||||
|
|
||||||
for i in 0..9 {
|
for i in 0..9 {
|
||||||
|
if self.markups[i][col][num as usize - 1] {
|
||||||
|
deleted[pos] = Some((i as u8, col as u8));
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
if self.markups[row][i][num as usize - 1] {
|
||||||
|
deleted[pos] = Some((row as u8, i as u8));
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
if self.markups[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3][num as usize - 1] {
|
||||||
|
deleted[pos] = Some((
|
||||||
|
row as u8 / 3 * 3 + i as u8 / 3,
|
||||||
|
col as u8 / 3 * 3 + i as u8 % 3,
|
||||||
|
));
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
self.markups[i][col][num as usize - 1] = false;
|
self.markups[i][col][num as usize - 1] = false;
|
||||||
self.markups[row][i][num as usize - 1] = false;
|
self.markups[row][i][num as usize - 1] = false;
|
||||||
self.markups[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3][num as usize - 1] = false;
|
self.markups[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3][num as usize - 1] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.push_to_undo_stack(UndoStep::Unplace(
|
||||||
|
self.preselection,
|
||||||
|
(row as u8, col as u8),
|
||||||
|
deleted,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_current_mark(&mut self) {
|
||||||
|
if self.markups[self.cur_row][self.cur_col][self.preselection as usize - 1] {
|
||||||
|
self.delete_current_mark();
|
||||||
|
} else {
|
||||||
|
self.set_current_mark();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_current_mark(&mut self) {
|
||||||
|
if *self.current_cell() != 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.markups[self.cur_row][self.cur_col][self.preselection as usize - 1] = false;
|
||||||
|
|
||||||
|
self.push_to_undo_stack(UndoStep::Remark(
|
||||||
|
self.preselection,
|
||||||
|
(self.cur_row as u8, self.cur_col as u8),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_current_mark(&mut self) {
|
||||||
|
if *self.current_cell() != 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.markups[self.cur_row][self.cur_col][self.preselection as usize - 1] = true;
|
||||||
|
|
||||||
|
self.push_to_undo_stack(UndoStep::Unmark(
|
||||||
|
self.preselection,
|
||||||
|
(self.cur_row as u8, self.cur_col as u8),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_cursor_to(&mut self, row: usize, col: usize) {
|
||||||
|
assert!(
|
||||||
|
row < 9 && col < 9,
|
||||||
|
"[!] Error: State::go_to: Can't move to row/column out of bounds."
|
||||||
|
);
|
||||||
|
self.cur_row = row;
|
||||||
|
self.cur_col = col;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_mode(&mut self, mode: Mode) {
|
pub fn enter_mode(&mut self, mode: Mode) {
|
||||||
@ -137,6 +210,8 @@ impl State {
|
|||||||
self.next_mode = self.mode;
|
self.next_mode = self.mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// enter a mode until one action has been taken,
|
||||||
|
/// then returning to the previous mode
|
||||||
pub fn enter_mode_once(&mut self, mode: Mode) {
|
pub fn enter_mode_once(&mut self, mode: Mode) {
|
||||||
if self.mode != mode {
|
if self.mode != mode {
|
||||||
self.next_mode = self.mode;
|
self.next_mode = self.mode;
|
||||||
@ -148,23 +223,11 @@ impl State {
|
|||||||
self.mode = self.next_mode;
|
self.mode = self.next_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_cursor_to(&mut self, row: usize, col: usize) {
|
fn get_elapsed_time(&self) -> time::Duration {
|
||||||
assert!(
|
time::Instant::now() - self.start_time
|
||||||
row < 9 && col < 9,
|
|
||||||
"[!] Error: State::go_to: Can't move to row/column out of bounds."
|
|
||||||
);
|
|
||||||
self.cur_row = row;
|
|
||||||
self.cur_col = col;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn toggle_current_mark(&mut self) {
|
|
||||||
self.markups[self.cur_row][self.cur_col][self.preselection as usize - 1] ^= true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete_current_mark(&mut self) {
|
|
||||||
self.markups[self.cur_row][self.cur_col][self.preselection as usize - 1] = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns number of filled cells
|
||||||
pub fn get_completion_string(&self) -> String {
|
pub fn get_completion_string(&self) -> String {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for row in self.board {
|
for row in self.board {
|
||||||
@ -178,6 +241,9 @@ impl State {
|
|||||||
[to_char(count / 10), to_char(count % 10)].iter().collect()
|
[to_char(count / 10), to_char(count % 10)].iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns how many of the 9 final occurences of the
|
||||||
|
/// preselected number have been found.
|
||||||
|
/// returns `'!'` if the user erroneously placed more than 9.
|
||||||
pub fn get_preselection_completion_char(&self) -> char {
|
pub fn get_preselection_completion_char(&self) -> char {
|
||||||
let count = self
|
let count = self
|
||||||
.board
|
.board
|
||||||
@ -185,9 +251,14 @@ impl State {
|
|||||||
.flatten()
|
.flatten()
|
||||||
.filter(|&cell| cell == self.preselection)
|
.filter(|&cell| cell == self.preselection)
|
||||||
.count();
|
.count();
|
||||||
(count.min(9) as u8 + b'0') as char
|
match count {
|
||||||
|
10.. => '!',
|
||||||
|
_ => (count as u8 + b'0') as char,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns the difficulty string used on the ingame scoreboard.
|
||||||
|
/// if you want the complete difficulty names use `Difficulty::to_string()`
|
||||||
pub fn get_difficulty_string(&self) -> String {
|
pub fn get_difficulty_string(&self) -> String {
|
||||||
match self.difficulty {
|
match self.difficulty {
|
||||||
Difficulty::Easy => String::from("Easy "),
|
Difficulty::Easy => String::from("Easy "),
|
||||||
@ -198,8 +269,10 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns (mins, secs) as a pair of strings
|
||||||
|
/// both are zero-padded to a width of 2 characters
|
||||||
pub fn get_timer_strings(&self) -> (String, String) {
|
pub fn get_timer_strings(&self) -> (String, String) {
|
||||||
let mut n = self.get_time().as_secs();
|
let mut n = self.get_elapsed_time().as_secs();
|
||||||
|
|
||||||
let mut mins = String::with_capacity(2);
|
let mut mins = String::with_capacity(2);
|
||||||
let mut secs = String::with_capacity(2);
|
let mut secs = String::with_capacity(2);
|
||||||
@ -218,6 +291,7 @@ impl State {
|
|||||||
(mins, secs)
|
(mins, secs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns timer as string in `mm:ss` format
|
||||||
pub fn get_timer_string(&self) -> String {
|
pub fn get_timer_string(&self) -> String {
|
||||||
let (mins, secs) = self.get_timer_strings();
|
let (mins, secs) = self.get_timer_strings();
|
||||||
let mut timer = String::with_capacity(5);
|
let mut timer = String::with_capacity(5);
|
||||||
@ -226,4 +300,98 @@ impl State {
|
|||||||
timer.push_str(&secs);
|
timer.push_str(&secs);
|
||||||
timer
|
timer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// undo an action that has been taken
|
||||||
|
pub fn undo(&mut self) {
|
||||||
|
use UndoStep::*;
|
||||||
|
|
||||||
|
if let Some(undo_step) = self.undo_stack.pop() {
|
||||||
|
self.redo_stack.push(undo_step);
|
||||||
|
match undo_step {
|
||||||
|
Unplace(num, (row, col), deleted_marks) => {
|
||||||
|
self.board[row as usize][col as usize] = 0;
|
||||||
|
for (r, c) in deleted_marks.iter().take_while(|x| x.is_some()).flatten() {
|
||||||
|
self.markups[*r as usize][*c as usize][num as usize - 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Replace(num, (row, col)) => {
|
||||||
|
self.board[row as usize][col as usize] = num;
|
||||||
|
}
|
||||||
|
Remark(num, (row, col)) => {
|
||||||
|
self.markups[row as usize][col as usize][num as usize - 1] = true;
|
||||||
|
}
|
||||||
|
Unmark(num, (row, col)) => {
|
||||||
|
self.markups[row as usize][col as usize][num as usize - 1] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// redo an action if one was taken and undone.
|
||||||
|
pub fn redo(&mut self) {
|
||||||
|
use UndoStep::*;
|
||||||
|
|
||||||
|
if let Some(redo_step) = self.redo_stack.pop() {
|
||||||
|
self.undo_stack.push(redo_step);
|
||||||
|
match redo_step {
|
||||||
|
Unplace(num, (row, col), deleted_marks) => {
|
||||||
|
self.board[row as usize][col as usize] = num;
|
||||||
|
for (r, c) in deleted_marks.iter().take_while(|x| x.is_some()).flatten() {
|
||||||
|
self.markups[*r as usize][*c as usize][num as usize - 1] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Replace(_, (row, col)) => {
|
||||||
|
self.board[row as usize][col as usize] = 0;
|
||||||
|
}
|
||||||
|
Remark(num, (row, col)) => {
|
||||||
|
self.markups[row as usize][col as usize][num as usize - 1] = false;
|
||||||
|
}
|
||||||
|
Unmark(num, (row, col)) => {
|
||||||
|
self.markups[row as usize][col as usize][num as usize - 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// pushes a move done by the player to the undo stack
|
||||||
|
/// this additionally invalidates the redo stack
|
||||||
|
pub fn push_to_undo_stack(&mut self, undo_step: UndoStep) {
|
||||||
|
self.undo_stack.push(undo_step);
|
||||||
|
self.redo_stack.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Dir {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
FarUp,
|
||||||
|
FarDown,
|
||||||
|
FarLeft,
|
||||||
|
FarRight,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, PartialEq, Clone, Copy)]
|
||||||
|
pub enum Mode {
|
||||||
|
#[default]
|
||||||
|
Edit,
|
||||||
|
Markup,
|
||||||
|
Go,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
|
pub enum UndoStep {
|
||||||
|
/// unplace num at (row, col) and re-mark
|
||||||
|
/// (row, col) if there were removed marks
|
||||||
|
Unplace(u8, (u8, u8), [Option<(u8, u8)>; 27]),
|
||||||
|
|
||||||
|
/// re-place num at (row, col)
|
||||||
|
Replace(u8, (u8, u8)),
|
||||||
|
|
||||||
|
/// unmark num at (row, col)
|
||||||
|
Unmark(u8, (u8, u8)),
|
||||||
|
|
||||||
|
/// re-mark num at (row, col)
|
||||||
|
Remark(u8, (u8, u8)),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,3 @@
|
|||||||
// TODO: remove when done
|
|
||||||
#![cfg_attr(debug_assertions, allow(unused))]
|
|
||||||
|
|
||||||
use crate::generator::Difficulty::*;
|
use crate::generator::Difficulty::*;
|
||||||
use crate::rand::{seq::SliceRandom, thread_rng};
|
use crate::rand::{seq::SliceRandom, thread_rng};
|
||||||
use crate::sudoku::{Board, New};
|
use crate::sudoku::{Board, New};
|
||||||
@ -9,6 +6,7 @@ use std::fmt;
|
|||||||
|
|
||||||
/// categories of difficulty, indicating how many
|
/// categories of difficulty, indicating how many
|
||||||
/// empty spaces will be on a sudoku board.
|
/// empty spaces will be on a sudoku board.
|
||||||
|
/// (see `Difficulty::removal_count()` for values)
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
#[derive(Default, Copy, Clone, PartialEq)]
|
#[derive(Default, Copy, Clone, PartialEq)]
|
||||||
pub enum Difficulty {
|
pub enum Difficulty {
|
||||||
@ -47,6 +45,8 @@ impl fmt::Display for Difficulty {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// generate a random, unsolved sudoku board with a given `Difficulty`.
|
/// generate a random, unsolved sudoku board with a given `Difficulty`.
|
||||||
|
/// solves a empty sudoku with random cell order and then removes
|
||||||
|
/// some number of cells depending on the difficulty.
|
||||||
pub fn generate_sudoku(difficulty: Difficulty) -> Board {
|
pub fn generate_sudoku(difficulty: Difficulty) -> Board {
|
||||||
let mut board = Board::new();
|
let mut board = Board::new();
|
||||||
|
|
||||||
@ -65,13 +65,15 @@ pub fn generate_sudoku(difficulty: Difficulty) -> Board {
|
|||||||
board
|
board
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// solves a sudoku, randomizing which empty cell of equal
|
||||||
|
/// mrv to choose next.
|
||||||
|
/// this is used to generate random, fully solvable sudokus.
|
||||||
|
///
|
||||||
|
/// NOTE: this does not guarantee a single-solution sudoku.
|
||||||
|
///
|
||||||
/// TODO: currently empty cells needs to be recreated
|
/// TODO: currently empty cells needs to be recreated
|
||||||
/// on each recursive call, and are randomized anew
|
/// on each recursive call, and are randomized anew
|
||||||
/// each time, which is an unnecessary overhead.
|
/// each time, which is an unnecessary overhead.
|
||||||
///
|
|
||||||
/// solves a sudoku, randomizing which empty cell of equal
|
|
||||||
/// mrv () to choose next.
|
|
||||||
/// this is used to generate random, fully solvable sudokus.
|
|
||||||
fn solve_random(board: &mut Board) -> Result<(), ()> {
|
fn solve_random(board: &mut Board) -> Result<(), ()> {
|
||||||
let mut empty_cells: Vec<(usize, usize)> = Vec::new();
|
let mut empty_cells: Vec<(usize, usize)> = Vec::new();
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@ trait New {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl New for Board {
|
impl New for Board {
|
||||||
|
/// return an empty `Board`
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
[[0u8; 9]; 9]
|
[[0u8; 9]; 9]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
// TODO: remove when done
|
|
||||||
#![cfg_attr(debug_assertions, allow(unused))]
|
|
||||||
|
|
||||||
use crate::sudoku::Board;
|
use crate::sudoku::Board;
|
||||||
|
|
||||||
|
/// returns true if the provided sudoku `Board` is in a solved state
|
||||||
pub fn is_solution(sudoku: &Board) -> bool {
|
pub fn is_solution(sudoku: &Board) -> bool {
|
||||||
for i in 0..9 {
|
for i in 0..9 {
|
||||||
let mut row_set = 0u16;
|
let mut row_set = 0u16;
|
||||||
|
|||||||
66
src/ui.rs
66
src/ui.rs
@ -16,7 +16,8 @@ use crossterm::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Screen<T>
|
/// state of the user interface
|
||||||
|
pub struct Ui<T>
|
||||||
where
|
where
|
||||||
T: io::Write,
|
T: io::Write,
|
||||||
{
|
{
|
||||||
@ -27,10 +28,15 @@ where
|
|||||||
pub height: usize,
|
pub height: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Screen<T>
|
impl<T> Ui<T>
|
||||||
where
|
where
|
||||||
T: io::Write,
|
T: io::Write,
|
||||||
{
|
{
|
||||||
|
/// returns a new `Ui` using `ostream` as its' output stream
|
||||||
|
/// and sets up terminal state:
|
||||||
|
/// - remembers the current cursor position
|
||||||
|
/// - enters alternate screen buffer
|
||||||
|
/// - enables raw mode
|
||||||
pub fn init(ostream: T) -> Self {
|
pub fn init(ostream: T) -> Self {
|
||||||
let (width, height) = size().unwrap();
|
let (width, height) = size().unwrap();
|
||||||
let (width, height) = (width as usize, height as usize);
|
let (width, height) = (width as usize, height as usize);
|
||||||
@ -38,7 +44,7 @@ where
|
|||||||
let presel_color_pair = (Color::Black, Color::Cyan);
|
let presel_color_pair = (Color::Black, Color::Cyan);
|
||||||
let markup_color_background = Color::Cyan;
|
let markup_color_background = Color::Cyan;
|
||||||
|
|
||||||
let mut screen = Screen {
|
let mut ui = Ui {
|
||||||
presel_color_pair,
|
presel_color_pair,
|
||||||
markup_color_background,
|
markup_color_background,
|
||||||
ostream,
|
ostream,
|
||||||
@ -47,22 +53,29 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
queue!(
|
queue!(
|
||||||
screen.ostream,
|
ui.ostream,
|
||||||
SavePosition,
|
SavePosition,
|
||||||
EnterAlternateScreen,
|
EnterAlternateScreen,
|
||||||
Clear(All)
|
Clear(All)
|
||||||
)
|
)
|
||||||
.expect("[-]: Error: ui::init: Failed to enter alternate screen.");
|
.expect("[-]: Error: ui::init: Failed to enter alternate screen.");
|
||||||
enable_raw_mode().expect("[-]: Error: ui::init: Failed to enable raw mode.");
|
enable_raw_mode().expect("[-]: Error: ui::init: Failed to enable raw mode.");
|
||||||
screen
|
ui
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// resets terminal state that `Ui::init()` sets:
|
||||||
|
/// - disables raw mode
|
||||||
|
/// - leaves alternate screen buffer
|
||||||
|
/// - restores cursor position
|
||||||
pub fn deinit(&mut self) -> io::Result<()> {
|
pub fn deinit(&mut self) -> io::Result<()> {
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
execute!(self.ostream, LeaveAlternateScreen, RestorePosition)?;
|
execute!(self.ostream, LeaveAlternateScreen, RestorePosition)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// updates `width` and `height`.
|
||||||
|
/// clears the screen and redraws the board if the dimensions changed.
|
||||||
|
/// returns an error if the new dimensions are too small to fit the ui.
|
||||||
pub fn update_dimensions(&mut self) -> io::Result<()> {
|
pub fn update_dimensions(&mut self) -> io::Result<()> {
|
||||||
let old_dimensions = (self.width, self.height);
|
let old_dimensions = (self.width, self.height);
|
||||||
|
|
||||||
@ -90,6 +103,15 @@ where
|
|||||||
queue!(self.ostream, MoveToColumn(0))
|
queue!(self.ostream, MoveToColumn(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// draws the entire ui, watching for possibly changed screen dimensions.
|
||||||
|
/// all other `draw_...()` functions just `queue!(...)` the actions to draw
|
||||||
|
/// their respective ui element.
|
||||||
|
/// this function chains all of these calls in order and then flushes the
|
||||||
|
/// changes to `self.ostream`.
|
||||||
|
///
|
||||||
|
/// NOTE: this function does not draw the static elements of the ui
|
||||||
|
/// using `Ui::draw_board()`. for efficiency these are only
|
||||||
|
/// redrawn on screen dimensions changes.
|
||||||
pub fn draw(&mut self, state: &State) -> io::Result<()> {
|
pub fn draw(&mut self, state: &State) -> io::Result<()> {
|
||||||
self.update_dimensions()?;
|
self.update_dimensions()?;
|
||||||
|
|
||||||
@ -100,6 +122,12 @@ where
|
|||||||
self.ostream.flush()
|
self.ostream.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `queue!(...)`s the drawing of the unchanging ui elements
|
||||||
|
/// such as the board and scoreboards outlines.
|
||||||
|
/// uses the lines provided by `board_template()`.
|
||||||
|
///
|
||||||
|
/// NOTE: this function itself does not flush to `self.ostream`
|
||||||
|
/// in order to only have to flush once per frame.
|
||||||
pub fn draw_board(&mut self) -> io::Result<()> {
|
pub fn draw_board(&mut self) -> io::Result<()> {
|
||||||
self.init_cursor_offset()?;
|
self.init_cursor_offset()?;
|
||||||
queue!(self.ostream, SetForegroundColor(Color::Reset))?;
|
queue!(self.ostream, SetForegroundColor(Color::Reset))?;
|
||||||
@ -114,6 +142,10 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `queue!(...)`s the drawing of the numbers in the cells.
|
||||||
|
///
|
||||||
|
/// NOTE: this function itself does not flush to `self.ostream`
|
||||||
|
/// in order to only have to flush once per frame.
|
||||||
fn draw_numbers(&mut self, state: &State) -> io::Result<()> {
|
fn draw_numbers(&mut self, state: &State) -> io::Result<()> {
|
||||||
self.init_cursor_offset()?;
|
self.init_cursor_offset()?;
|
||||||
queue!(self.ostream, SetForegroundColor(Color::Reset))?;
|
queue!(self.ostream, SetForegroundColor(Color::Reset))?;
|
||||||
@ -174,6 +206,10 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `queue!(...)`s the drawing of variable scoreboard content
|
||||||
|
///
|
||||||
|
/// NOTE: this function itself does not flush to `self.ostream`
|
||||||
|
/// in order to only have to flush once per frame.
|
||||||
fn draw_scoreboard(&mut self, state: &State) -> io::Result<()> {
|
fn draw_scoreboard(&mut self, state: &State) -> io::Result<()> {
|
||||||
self.init_cursor_offset()?;
|
self.init_cursor_offset()?;
|
||||||
queue!(self.ostream, SetForegroundColor(Color::Reset))?;
|
queue!(self.ostream, SetForegroundColor(Color::Reset))?;
|
||||||
@ -225,6 +261,10 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `queue!(...)`s the placement of the cursor on the selected cell.
|
||||||
|
///
|
||||||
|
/// NOTE: this function itself does not flush to `self.ostream`
|
||||||
|
/// in order to only have to flush once per frame.
|
||||||
fn draw_cursor(&mut self, state: &State) -> io::Result<()> {
|
fn draw_cursor(&mut self, state: &State) -> io::Result<()> {
|
||||||
let (row, col) = (state.cur_row, state.cur_col);
|
let (row, col) = (state.cur_row, state.cur_col);
|
||||||
let (x, y) = ((self.width / 2 - 14) as u16, (self.height / 2 - 6) as u16);
|
let (x, y) = ((self.width / 2 - 14) as u16, (self.height / 2 - 6) as u16);
|
||||||
@ -243,6 +283,12 @@ where
|
|||||||
queue!(self.ostream, MoveTo(x, y), SetCursorStyle::SteadyBlock)
|
queue!(self.ostream, MoveTo(x, y), SetCursorStyle::SteadyBlock)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `queue!(...)`s the movement of the cursor by (`x`, `y`).
|
||||||
|
/// positive `x` values correspond to movement right by `x` columns.
|
||||||
|
/// positive `y` values correspond to movement down by `y` rows.
|
||||||
|
///
|
||||||
|
/// NOTE: this function itself does not flush to `self.ostream`
|
||||||
|
/// in order to only have to flush once per frame.
|
||||||
fn move_cursor_by(&mut self, x: isize, y: isize) -> io::Result<()> {
|
fn move_cursor_by(&mut self, x: isize, y: isize) -> io::Result<()> {
|
||||||
match x.cmp(&0) {
|
match x.cmp(&0) {
|
||||||
Equal => Ok(()),
|
Equal => Ok(()),
|
||||||
@ -257,6 +303,10 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `queue!(...)`s the movement of the cursor to the top-left
|
||||||
|
/// of the inner ui.
|
||||||
|
/// this simplifies handling the padding in other drawing functions
|
||||||
|
/// since the padding depends on the screen dimensions.
|
||||||
fn init_cursor_offset(&mut self) -> io::Result<()> {
|
fn init_cursor_offset(&mut self) -> io::Result<()> {
|
||||||
let lft_pad = (self.width / 2 - 14) as u16;
|
let lft_pad = (self.width / 2 - 14) as u16;
|
||||||
let top_pad = (self.height / 2 - 6) as u16;
|
let top_pad = (self.height / 2 - 6) as u16;
|
||||||
@ -264,6 +314,8 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns a template for the parts of the board that
|
||||||
|
/// are always the same.
|
||||||
fn board_template() -> [String; 13] {
|
fn board_template() -> [String; 13] {
|
||||||
[
|
[
|
||||||
String::from("┌────────┬────────┬────────┐ ┌───────┐"),
|
String::from("┌────────┬────────┬────────┐ ┌───────┐"),
|
||||||
@ -287,6 +339,10 @@ pub trait UiCrash {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> UiCrash for io::Result<T> {
|
impl<T> UiCrash for io::Result<T> {
|
||||||
|
/// ends the process with exit code 1
|
||||||
|
/// if called on `Err(_)` variant.
|
||||||
|
/// if called on `Ok(_)` variant, this is a no-op
|
||||||
|
/// that discards the contained value.
|
||||||
fn or_crash(&self) {
|
fn or_crash(&self) {
|
||||||
if self.is_err() {
|
if self.is_err() {
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user