Skip to content

Commit

Permalink
Minor refactor to fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
crazymerlyn committed Apr 13, 2024
1 parent b333a4c commit 6f2e2dd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["CrazyMerlyn <[email protected]>"]
name = "sudoku-tty"
version = "0.1.2"
version = "0.1.3"
description = "Play sudoku in terminal"
repository = "https://github.com/crazymerlyn/sudoku"

Expand Down
31 changes: 14 additions & 17 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use grid::Grid;
use termion;
use termion::clear;
use termion::cursor;
use termion::event::*;
use termion::event::{Event, Key};
use termion::input::{Events, TermRead};
use termion::style;

Expand Down Expand Up @@ -94,11 +94,11 @@ impl<R: TermRead + Read, W: Write> Game<R, W> {

if let Event::Key(key) = evt {
match key {
Key::Char('q') | Key::Ctrl('c') | Key::Ctrl('d') => return None,
Key::Down | Key::Char('j') | Key::Char('s') => {
Key::Char('q') | Key::Ctrl('c' | 'd') => return None,
Key::Down | Key::Char('j' | 's') => {
current_index += 1;
}
Key::Up | Key::Char('k') | Key::Char('w') => {
Key::Up | Key::Char('k' | 'w') => {
current_index = if current_index > 0 {
current_index - 1
} else {
Expand All @@ -115,19 +115,16 @@ impl<R: TermRead + Read, W: Write> Game<R, W> {
pub fn run(&mut self) {
self.init();

let diff = match self.get_difficulty() {
Some(val) => val,
None => {
return;
}
let Some(diff) = self.get_difficulty() else {
return;
};

self.grid = Generator::generate(diff);

let (w, h) = termion::terminal_size().unwrap();
let top = (h - 19) / 2;
let left = (w - 37) / 2;
let mut message = "".to_string();
let mut message = String::new();

writeln!(
self.stdout,
Expand All @@ -146,16 +143,16 @@ impl<R: TermRead + Read, W: Write> Game<R, W> {

if let Event::Key(key) = evt {
match key {
Key::Right | Key::Char('d') | Key::Char('l') => {
Key::Right | Key::Char('d' | 'l') => {
self.grid.move_cursor(Direction::Right);
}
Key::Left | Key::Char('a') | Key::Char('h') => {
Key::Left | Key::Char('a' | 'h') => {
self.grid.move_cursor(Direction::Left);
}
Key::Up | Key::Char('w') | Key::Char('k') => {
Key::Up | Key::Char('w' | 'k') => {
self.grid.move_cursor(Direction::Up);
}
Key::Down | Key::Char('s') | Key::Char('j') => {
Key::Down | Key::Char('s' | 'j') => {
self.grid.move_cursor(Direction::Down);
}
Key::Char(' ') | Key::Backspace => self.grid.update_current(0),
Expand All @@ -168,12 +165,12 @@ impl<R: TermRead + Read, W: Write> Game<R, W> {
self.run();
return;
}
ch if ch.is_digit(10) => {
self.grid.update_current(ch as usize - '0' as usize)
ch if ch.is_ascii_digit() => {
self.grid.update_current(ch as usize - '0' as usize);
}
_ => {}
},
Key::Ctrl('c') | Key::Ctrl('d') => break,
Key::Ctrl('c' | 'd') => break,
_ => {}
};
if self.grid.is_solved() {
Expand Down
18 changes: 9 additions & 9 deletions src/grid/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use rand::{random, thread_rng, Rng};

use super::Grid;

const VERY_EASY: &'static str = include_str!("./seeds/veasy.csv");
const EASY: &'static str = include_str!("./seeds/easy.csv");
const MEDIUM: &'static str = include_str!("./seeds/medium.csv");
const HARD: &'static str = include_str!("./seeds/hard.csv");
const FIENDISH: &'static str = include_str!("./seeds/fiendish.csv");
const VERY_EASY: &str = include_str!("./seeds/veasy.csv");
const EASY: &str = include_str!("./seeds/easy.csv");
const MEDIUM: &str = include_str!("./seeds/medium.csv");
const HARD: &str = include_str!("./seeds/hard.csv");
const FIENDISH: &str = include_str!("./seeds/fiendish.csv");

pub struct Generator {}

Expand All @@ -22,7 +22,7 @@ impl Generator {

let mut permutation: Vec<_> = (1..10).collect();
thread_rng().shuffle(&mut permutation);
puzzle.permute(permutation);
puzzle.permute(&permutation);

if random() {
puzzle.flip_horizontally();
Expand All @@ -46,8 +46,8 @@ pub enum Difficulty {
}

impl Difficulty {
pub fn puzzles(&self) -> &'static str {
match *self {
pub fn puzzles(self) -> &'static str {
match self {
Difficulty::VeryEasy => VERY_EASY,
Difficulty::Easy => EASY,
Difficulty::Medium => MEDIUM,
Expand Down Expand Up @@ -79,7 +79,7 @@ impl fmt::Display for Difficulty {
Difficulty::Hard => "Hard",
Difficulty::Fiendish => "Fiendish",
};
write!(f, "{}", str_rep)
write!(f, "{str_rep}")
}
}

Expand Down
51 changes: 26 additions & 25 deletions src/grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub mod generator;

const BORDER_COLOR: color::Fg<color::Rgb> = color::Fg(color::Rgb(220, 220, 220));

const BORDER_TOP: &'static str = "┏━━━┯━━━┯━━━┳━━━┯━━━┯━━━┳━━━┯━━━┯━━━┓";
const BORDER_BOTTOM: &'static str = "┗━━━┷━━━┷━━━┻━━━┷━━━┷━━━┻━━━┷━━━┷━━━┛";
const BORDER_HORIZONTAL_THIN: &'static str = "┠───┼───┼───╂───┼───┼───╂───┼───┼───┨";
const BORDER_HORIZONTAL_THICK: &'static str = "┣━━━┿━━━┿━━━╋━━━┿━━━┿━━━╋━━━┿━━━┿━━━┫";
const BORDER_VERTICAL_THICK: &'static str = "┃";
const BORDER_VERTICAL_THIN: &'static str = "│";
const BORDER_TOP: &str = "┏━━━┯━━━┯━━━┳━━━┯━━━┯━━━┳━━━┯━━━┯━━━┓";
const BORDER_BOTTOM: &str = "┗━━━┷━━━┷━━━┻━━━┷━━━┷━━━┻━━━┷━━━┷━━━┛";
const BORDER_HORIZONTAL_THIN: &str = "┠───┼───┼───╂───┼───┼───╂───┼───┼───┨";
const BORDER_HORIZONTAL_THICK: &str = "┣━━━┿━━━┿━━━╋━━━┿━━━┿━━━╋━━━┿━━━┿━━━┫";
const BORDER_VERTICAL_THICK: &str = "┃";
const BORDER_VERTICAL_THIN: &str = "│";

#[derive(Debug, Clone)]
struct GridState {
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Grid {
self.state.freeze();
}

pub fn permute(&mut self, permutation: Vec<u8>) {
pub fn permute(&mut self, permutation: &[u8]) {
self.state.permute(permutation);
}

Expand Down Expand Up @@ -151,7 +151,7 @@ impl GridState {
} else if c == '\n' {
i += 1;
j = 0;
} else if c.is_digit(10) {
} else if c.is_ascii_digit() {
values[i][j] = c as u8 - b'0';
} else {
panic!("Unknown character in csv: {}", c);
Expand Down Expand Up @@ -185,7 +185,7 @@ impl GridState {
}
}

pub fn permute(&mut self, permutation: Vec<u8>) {
pub fn permute(&mut self, permutation: &[u8]) {
assert_eq!(permutation.len(), 9);
assert!((1..10).all(|n| permutation.contains(&n)));

Expand Down Expand Up @@ -214,19 +214,19 @@ impl GridState {
}

pub fn col(&self, col: usize) -> Vec<Square> {
let mut column = vec![];
let mut column = Vec::with_capacity(9);
for i in 0..9 {
column.push(self.squares[i][col]);
}
column
}

pub fn block(&self, y: usize, x: usize) -> Vec<Square> {
let mut block = vec![];
let mut block = Vec::with_capacity(9);

for i in y * 3..(y + 1) * 3 {
for j in x * 3..(x + 1) * 3 {
block.push(self.squares[i][j])
block.push(self.squares[i][j]);
}
}

Expand Down Expand Up @@ -325,12 +325,12 @@ impl fmt::Display for Grid {
impl fmt::Display for GridState {
fn fmt(&self, ff: &mut fmt::Formatter) -> fmt::Result {
let mistakes = self.find_invalid_squares();
let mut f = "".to_string();
let mut f = String::new();

write!(f, "{}{}", BORDER_COLOR, BORDER_TOP)?;
write!(f, "{BORDER_COLOR}{BORDER_TOP}")?;
write!(f, "{}{}", cursor::Down(1), cursor::Left(37))?;
for i in 0..9 {
write!(f, "{}{}", BORDER_COLOR, BORDER_VERTICAL_THICK)?;
write!(f, "{BORDER_COLOR}{BORDER_VERTICAL_THICK}")?;
for j in 0..9 {
let st = if (i, j) == self.current {
format!("{}", style::Invert)
Expand All @@ -353,25 +353,25 @@ impl fmt::Display for GridState {
};

write!(f, " {}{}{}{} ", st, fg, self.squares[i][j], nt)?;
write!(f, "{}", BORDER_COLOR)?;
write!(f, "{BORDER_COLOR}")?;
if j % 3 == 2 {
write!(f, "{}", BORDER_VERTICAL_THICK)?;
write!(f, "{BORDER_VERTICAL_THICK}")?;
} else {
write!(f, "{}", BORDER_VERTICAL_THIN)?;
write!(f, "{BORDER_VERTICAL_THIN}")?;
}
}
write!(f, "{}{}", cursor::Down(1), cursor::Left(37))?;
write!(f, "{}", BORDER_COLOR)?;
write!(f, "{BORDER_COLOR}")?;
if i == 8 {
write!(f, "{}", BORDER_BOTTOM)?;
write!(f, "{BORDER_BOTTOM}")?;
} else if i % 3 == 2 {
write!(f, "{}", BORDER_HORIZONTAL_THICK)?;
write!(f, "{BORDER_HORIZONTAL_THICK}")?;
} else {
write!(f, "{}", BORDER_HORIZONTAL_THIN)?;
write!(f, "{BORDER_HORIZONTAL_THIN}")?;
}
write!(f, "{}{}", cursor::Down(1), cursor::Left(37))?;
}
write!(ff, "{}", f)
write!(ff, "{f}")
}
}

Expand All @@ -383,6 +383,7 @@ fn next_multiple(a: usize, b: usize) -> usize {
b + a - (b % a)
}

#[derive(Copy, Clone)]
pub enum Direction {
Right,
Left,
Expand All @@ -391,8 +392,8 @@ pub enum Direction {
}

impl Direction {
pub fn coords(&self) -> (usize, usize) {
match *self {
pub fn coords(self) -> (usize, usize) {
match self {
Direction::Right => (0, 1),
Direction::Left => (0, 8),
Direction::Up => (8, 0),
Expand Down
25 changes: 9 additions & 16 deletions src/grid/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,26 @@ impl Square {
if value == 0 {
Square::Empty
} else {
assert!(value <= 9 as u8);
assert!(value <= 9_u8);
Square::Filled(value)
}
}

pub fn initial(value: u8) -> Square {
assert!(value > 0 && value <= 9 as u8);
assert!(value > 0 && value <= 9_u8);
Square::Initial(value)
}

pub fn is_initial(&self) -> bool {
match *self {
Square::Initial(_) => true,
_ => false,
}
pub fn is_initial(self) -> bool {
matches!(self, Square::Initial(_))
}

pub fn is_empty(&self) -> bool {
match *self {
Square::Empty => true,
_ => false,
}
pub fn is_empty(self) -> bool {
matches!(self, Square::Empty)
}

pub fn value(&self) -> u8 {
match *self {
pub fn value(self) -> u8 {
match self {
Square::Initial(value) | Square::Filled(value) => value,
Square::Empty => 0,
}
Expand All @@ -53,8 +47,7 @@ impl PartialEq for Square {
impl fmt::Display for Square {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Square::Initial(value) => write!(f, "{}", value),
Square::Filled(value) => write!(f, "{}", value),
Square::Filled(value) | Square::Initial(value) => write!(f, "{value}"),
Square::Empty => write!(f, " "),
}
}
Expand Down

0 comments on commit 6f2e2dd

Please sign in to comment.