├── .gitignore ├── screenshot.png ├── assets └── Verdana.ttf ├── Cargo.toml ├── src ├── settings.rs ├── main.rs ├── field.rs └── app.rs ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | target/* 2 | Cargo.lock 3 | *.swp 4 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xairy/rust-sudoku/HEAD/screenshot.png -------------------------------------------------------------------------------- /assets/Verdana.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xairy/rust-sudoku/HEAD/assets/Verdana.ttf -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-sudoku" 3 | version = "0.1.2" 4 | authors = ["Andrey Konovalov "] 5 | 6 | [dependencies] 7 | rand = "0.8" 8 | piston_window = "0.131" 9 | -------------------------------------------------------------------------------- /src/settings.rs: -------------------------------------------------------------------------------- 1 | pub struct Vec2f { 2 | pub x: f64, 3 | pub y: f64 4 | } 5 | 6 | pub struct Settings { 7 | pub wind_size: Vec2f, 8 | pub cell_size: Vec2f, 9 | pub font_size: u32, 10 | pub text_offset: Vec2f 11 | } 12 | 13 | impl Settings { 14 | pub fn new() -> Settings { 15 | Settings { 16 | wind_size: Vec2f{ x: 900.0, y: 900.0 }, 17 | cell_size: Vec2f{ x: 100.0, y: 100.0 }, 18 | font_size: 64, 19 | text_offset: Vec2f{ x: 30.0, y: 75.0 } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rust-sudoku 2 | =========== 3 | 4 | A Sudoku implementation in Rust. 5 | 6 | ![Screenshot](screenshot.png?raw=true "Screenshot") 7 | 8 | ## Controls 9 | 10 | Use mouse or arrow keys to select cells. 11 | 12 | Keys: 13 | * **1-9** - fill in a digit 14 | * **Backspace** - clear a cell 15 | * **R** - generate new 16 | * **S** - show solution 17 | * **Esc** - exit 18 | 19 | ## Requirements 20 | 21 | 1. [Rust](http://www.rust-lang.org/install.html) 22 | 2. [Cargo](http://doc.crates.io/) 23 | 3. [Freetype6](https://github.com/PistonDevelopers/Piston-Tutorials/tree/master/getting-started#installing-dependencies) 24 | 25 | ## Building 26 | 27 | ``` 28 | $ cargo build 29 | ``` 30 | 31 | ## Running 32 | 33 | ``` 34 | $ cargo run 35 | ``` 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Andrey Konovalov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate piston_window; 2 | extern crate rand; 3 | 4 | use piston_window::*; 5 | use std::path::Path; 6 | 7 | mod app; 8 | mod field; 9 | mod settings; 10 | 11 | fn main() { 12 | let settings = settings::Settings::new(); 13 | 14 | let opengl = OpenGL::V3_2; 15 | let mut window: PistonWindow = WindowSettings::new( 16 | "Sudoku", 17 | [(settings.wind_size.x as u32), (settings.wind_size.y as u32)], 18 | ) 19 | .exit_on_esc(true) 20 | .resizable(false) 21 | .graphics_api(opengl) 22 | .build() 23 | .unwrap(); 24 | 25 | let font_path = Path::new("assets/Verdana.ttf"); 26 | let mut cache = window.load_font(font_path).unwrap(); 27 | 28 | let mut app = app::App::new(settings); 29 | 30 | while let Some(e) = window.next() { 31 | window.draw_2d(&e, |c, g, device| { 32 | app.on_render_new(c, g, &mut cache); 33 | 34 | cache.factory.encoder.flush(device); 35 | }); 36 | 37 | if let Some(button) = e.press_args() { 38 | app.on_button_press(&button); 39 | } 40 | 41 | if let Some(args) = e.mouse_cursor_args() { 42 | app.on_mouse_move(&args); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/field.rs: -------------------------------------------------------------------------------- 1 | use rand; 2 | use rand::seq::SliceRandom; 3 | use rand::Rng; 4 | 5 | pub struct Coords { 6 | pub x: u8, 7 | pub y: u8, 8 | } 9 | 10 | #[derive(Copy, Clone)] 11 | pub struct Cell { 12 | pub digit: Option, 13 | pub fixed: bool, 14 | } 15 | 16 | #[derive(Copy, Clone)] 17 | pub struct Field { 18 | pub cells: [[Cell; 9]; 9], 19 | } 20 | 21 | impl Field { 22 | pub fn new() -> Field { 23 | let mut field = Field { 24 | cells: [[Cell { 25 | digit: None, 26 | fixed: false, 27 | }; 9]; 9], 28 | }; 29 | field.fill_random(); 30 | field 31 | } 32 | 33 | pub fn get_cell(&mut self, x: u8, y: u8) -> &mut Cell { 34 | &mut self.cells[y as usize][x as usize] 35 | } 36 | 37 | pub fn find_conflict(&mut self, coords: &Coords, digit: u8) -> Option { 38 | for x in 0..9 { 39 | if x != coords.x { 40 | if let Some(cell_digit) = self.get_cell(x, coords.y).digit { 41 | if cell_digit == digit { 42 | return Some(Coords { x, y: coords.y }); 43 | } 44 | } 45 | } 46 | } 47 | 48 | for y in 0..9 { 49 | if y != coords.y { 50 | if let Some(cell_digit) = self.get_cell(coords.x, y).digit { 51 | if cell_digit == digit { 52 | return Some(Coords { x: coords.x, y }); 53 | } 54 | } 55 | } 56 | } 57 | 58 | let section = Coords { 59 | x: coords.x / 3, 60 | y: coords.y / 3, 61 | }; 62 | for x in section.x * 3..(section.x + 1) * 3 { 63 | for y in section.y * 3..(section.y + 1) * 3 { 64 | if x != coords.x || y != coords.y { 65 | if let Some(cell_digit) = self.get_cell(x, y).digit { 66 | if cell_digit == digit { 67 | return Some(Coords { x, y }); 68 | } 69 | } 70 | } 71 | } 72 | } 73 | 74 | None 75 | } 76 | 77 | pub fn clear(&mut self) { 78 | for y in 0..9 { 79 | for x in 0..9 { 80 | self.cells[x][y] = Cell { 81 | digit: None, 82 | fixed: false, 83 | }; 84 | } 85 | } 86 | } 87 | 88 | pub fn fill_random(&mut self) { 89 | self.clear(); 90 | 91 | let x = rand::thread_rng().gen_range(0u8..9u8); 92 | let y = rand::thread_rng().gen_range(0u8..9u8); 93 | let digit = rand::thread_rng().gen_range(1u8..10u8); 94 | self.get_cell(x, y).digit = Some(digit); 95 | 96 | let solution = self.find_solution().unwrap(); 97 | self.cells = solution.cells; 98 | 99 | loop { 100 | let mut x; 101 | let mut y; 102 | let digit; 103 | 104 | loop { 105 | x = rand::thread_rng().gen_range(0u8..9u8); 106 | y = rand::thread_rng().gen_range(0u8..9u8); 107 | if self.get_cell(x, y).digit.is_none() { 108 | continue; 109 | } 110 | digit = self.get_cell(x, y).digit.unwrap(); 111 | self.get_cell(x, y).digit = None; 112 | break; 113 | } 114 | 115 | let solutions = self.find_solutions(2); 116 | if solutions.len() == 1 { 117 | continue; 118 | } 119 | self.get_cell(x, y).digit = Some(digit); 120 | 121 | break; 122 | } 123 | 124 | // FIXME(xairy): generates perfect sudoku, but slow. 125 | /* 126 | let mut cells = Vec::new(); 127 | for y in 0..9 { 128 | for x in 0..9 { 129 | cells.push((x, y)); 130 | } 131 | } 132 | rand::thread_rng().shuffle(&mut cells); 133 | 134 | for &(x, y) in cells.iter() { 135 | let digit = self.get_cell(x, y).digit.unwrap(); 136 | self.get_cell(x, y).digit = None; 137 | let solutions = self.find_solutions(2); 138 | if solutions.len() > 1 { 139 | self.get_cell(x, y).digit = Some(digit); 140 | } 141 | } 142 | */ 143 | 144 | for y in 0..9 { 145 | for x in 0..9 { 146 | if self.get_cell(x, y).digit.is_some() { 147 | self.get_cell(x, y).fixed = true; 148 | } 149 | } 150 | } 151 | } 152 | 153 | pub fn fill_solution(&mut self) { 154 | if let Some(s) = self.find_solution() { 155 | self.cells = s.cells; 156 | } 157 | } 158 | 159 | pub fn find_solution(&mut self) -> Option { 160 | let solutions = self.find_solutions(1); 161 | solutions.first().copied() 162 | } 163 | 164 | fn find_solutions(&mut self, stop_at: u32) -> Vec { 165 | let mut solutions = Vec::new(); 166 | let mut field = *self; 167 | field.find_solutions_impl(&mut solutions, stop_at); 168 | solutions 169 | } 170 | 171 | fn find_solutions_impl(&mut self, solutions: &mut Vec, stop_at: u32) -> bool { 172 | let mut empty_cell: Option = None; 173 | 'outer: for y in 0..9 { 174 | for x in 0..9 { 175 | if self.get_cell(x, y).digit.is_none() { 176 | empty_cell = Some(Coords { x, y }); 177 | break 'outer; 178 | } 179 | } 180 | } 181 | 182 | if empty_cell.is_none() { 183 | solutions.push(*self); 184 | return solutions.len() >= (stop_at as usize); 185 | } 186 | let coords = empty_cell.unwrap(); 187 | 188 | let mut digits: Vec = (1..10).collect(); 189 | digits.shuffle(&mut rand::thread_rng()); 190 | 191 | for &digit in digits.iter() { 192 | if self.find_conflict(&coords, digit).is_none() { 193 | self.get_cell(coords.x, coords.y).digit = Some(digit); 194 | if self.find_solutions_impl(solutions, stop_at) { 195 | return true; 196 | } 197 | self.get_cell(coords.x, coords.y).digit = None; 198 | } 199 | } 200 | 201 | false 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /src/app.rs: -------------------------------------------------------------------------------- 1 | use piston_window::*; 2 | 3 | use field; 4 | use settings; 5 | 6 | struct Vec2f { 7 | x: f64, 8 | y: f64, 9 | } 10 | 11 | pub struct App { 12 | settings: settings::Settings, 13 | mouse_coords: Vec2f, 14 | field: field::Field, 15 | selected_cell: Option, 16 | conflicting_cell: Option, 17 | } 18 | 19 | impl App { 20 | pub fn new(settings: settings::Settings) -> App { 21 | App { 22 | settings, 23 | mouse_coords: Vec2f { x: 0.0, y: 0.0 }, 24 | field: field::Field::new(), 25 | selected_cell: None, 26 | conflicting_cell: None, 27 | } 28 | } 29 | 30 | pub fn on_render_new(&mut self, c: Context, g: &mut G2d, cache: &mut Glyphs) { 31 | clear([1.0; 4], g); 32 | 33 | let pointed_cell = field::Coords { 34 | x: (self.mouse_coords.x / self.settings.cell_size.x).floor() as u8, 35 | y: (self.mouse_coords.y / self.settings.cell_size.y).floor() as u8, 36 | }; 37 | rectangle( 38 | [0.95, 0.95, 0.95, 1.0], 39 | [ 40 | (pointed_cell.x as f64) * self.settings.cell_size.x, 41 | (pointed_cell.y as f64) * self.settings.cell_size.y, 42 | self.settings.cell_size.x, 43 | self.settings.cell_size.y, 44 | ], 45 | c.transform, 46 | g, 47 | ); 48 | 49 | for y in 0..9 { 50 | for x in 0..9 { 51 | let cell = self.field.get_cell(x, y); 52 | if cell.fixed { 53 | rectangle( 54 | [0.9, 0.9, 0.9, 1.0], 55 | [ 56 | (x as f64) * self.settings.cell_size.x, 57 | (y as f64) * self.settings.cell_size.y, 58 | self.settings.cell_size.x, 59 | self.settings.cell_size.y, 60 | ], 61 | c.transform, 62 | g, 63 | ); 64 | } 65 | } 66 | } 67 | 68 | if let Some(ref cell) = self.selected_cell { 69 | if let Some(digit) = self.field.get_cell(cell.x, cell.y).digit { 70 | for y in 0..9 { 71 | for x in 0..9 { 72 | if let Some(other_digit) = self.field.get_cell(x, y).digit { 73 | if other_digit == digit { 74 | rectangle( 75 | [0.8, 0.8, 0.9, 1.0], 76 | [ 77 | (x as f64) * self.settings.cell_size.x, 78 | (y as f64) * self.settings.cell_size.y, 79 | self.settings.cell_size.x, 80 | self.settings.cell_size.y, 81 | ], 82 | c.transform, 83 | g, 84 | ); 85 | } 86 | } 87 | } 88 | } 89 | } 90 | } 91 | 92 | if let Some(ref cell) = self.conflicting_cell { 93 | rectangle( 94 | [0.9, 0.8, 0.8, 1.0], 95 | [ 96 | (cell.x as f64) * self.settings.cell_size.x, 97 | (cell.y as f64) * self.settings.cell_size.y, 98 | self.settings.cell_size.x, 99 | self.settings.cell_size.y, 100 | ], 101 | c.transform, 102 | g, 103 | ); 104 | } 105 | 106 | if let Some(ref cell) = self.selected_cell { 107 | rectangle( 108 | [0.8, 0.9, 0.8, 1.0], 109 | [ 110 | (cell.x as f64) * self.settings.cell_size.x, 111 | (cell.y as f64) * self.settings.cell_size.y, 112 | self.settings.cell_size.x, 113 | self.settings.cell_size.y, 114 | ], 115 | c.transform, 116 | g, 117 | ); 118 | } 119 | 120 | for y in 0..9 { 121 | for x in 0..9 { 122 | if let Some(ref digit) = self.field.cells[y][x].digit { 123 | let transform = c.transform.trans( 124 | (x as f64) * self.settings.cell_size.x + self.settings.text_offset.x, 125 | (y as f64) * self.settings.cell_size.y + self.settings.text_offset.y, 126 | ); 127 | let text = graphics::Text::new(self.settings.font_size); 128 | text.draw(&digit.to_string(), cache, &c.draw_state, transform, g) 129 | .unwrap(); 130 | } 131 | } 132 | } 133 | 134 | for n in 1..9 { 135 | let mut thick = 2.0; 136 | if n % 3 == 0 { 137 | thick = 8.0; 138 | } 139 | rectangle( 140 | [0.0, 0.0, 0.0, 1.0], 141 | [ 142 | (n as f64) * self.settings.cell_size.x - thick / 2.0, 143 | 0.0, 144 | thick / 2.0, 145 | self.settings.wind_size.y, 146 | ], 147 | c.transform, 148 | g, 149 | ); 150 | rectangle( 151 | [0.0, 0.0, 0.0, 1.0], 152 | [ 153 | 0.0, 154 | (n as f64) * self.settings.cell_size.y - thick / 2.0, 155 | self.settings.wind_size.x, 156 | thick / 2.0, 157 | ], 158 | c.transform, 159 | g, 160 | ); 161 | } 162 | } 163 | 164 | pub fn on_button_press(&mut self, button: &Button) { 165 | match button { 166 | Button::Keyboard(key) => { 167 | self.on_key_down(key); 168 | } 169 | Button::Mouse(button) => { 170 | self.on_mouse_click(button); 171 | } 172 | Button::Controller(_) => {} 173 | Button::Hat(_) => {} 174 | } 175 | } 176 | 177 | fn on_key_down(&mut self, pressed_key: &Key) { 178 | let key_digit_mapping = [ 179 | (Key::D1, 1), 180 | (Key::D2, 2), 181 | (Key::D3, 3), 182 | (Key::D4, 4), 183 | (Key::D5, 5), 184 | (Key::D6, 6), 185 | (Key::D7, 7), 186 | (Key::D8, 8), 187 | (Key::D9, 9), 188 | (Key::NumPad1, 1), 189 | (Key::NumPad2, 2), 190 | (Key::NumPad3, 3), 191 | (Key::NumPad4, 4), 192 | (Key::NumPad5, 5), 193 | (Key::NumPad6, 6), 194 | (Key::NumPad7, 7), 195 | (Key::NumPad8, 8), 196 | (Key::NumPad9, 9), 197 | ]; 198 | for &(key, digit) in key_digit_mapping.iter() { 199 | if pressed_key == &key { 200 | if let Some(ref cell) = self.selected_cell { 201 | if !self.field.get_cell(cell.x, cell.y).fixed { 202 | match self.field.find_conflict(cell, digit) { 203 | Some(coords) => { 204 | self.conflicting_cell = Some(coords); 205 | } 206 | None => { 207 | self.field.get_cell(cell.x, cell.y).digit = Some(digit); 208 | self.conflicting_cell = None; 209 | } 210 | } 211 | } 212 | } 213 | } 214 | } 215 | if pressed_key == &Key::Backspace { 216 | if let Some(ref cell) = self.selected_cell { 217 | if !self.field.get_cell(cell.x, cell.y).fixed { 218 | self.field.get_cell(cell.x, cell.y).digit = None; 219 | self.conflicting_cell = None; 220 | } 221 | } 222 | } 223 | if pressed_key == &Key::S { 224 | self.field.fill_solution(); 225 | self.conflicting_cell = None; 226 | self.selected_cell = None; 227 | } 228 | if pressed_key == &Key::R { 229 | self.field.fill_random(); 230 | self.conflicting_cell = None; 231 | self.selected_cell = None; 232 | } 233 | if pressed_key == &Key::Up { 234 | match self.selected_cell { 235 | Some(ref mut cell) => { 236 | if cell.y > 0 { 237 | cell.y -= 1; 238 | } 239 | } 240 | None => self.selected_cell = Some(field::Coords { x: 0, y: 0 }), 241 | } 242 | } 243 | if pressed_key == &Key::Down { 244 | match self.selected_cell { 245 | Some(ref mut cell) => { 246 | if cell.y < 8 { 247 | cell.y += 1; 248 | } 249 | } 250 | None => self.selected_cell = Some(field::Coords { x: 0, y: 0 }), 251 | } 252 | } 253 | if pressed_key == &Key::Left { 254 | match self.selected_cell { 255 | Some(ref mut cell) => { 256 | if cell.x > 0 { 257 | cell.x -= 1; 258 | } 259 | } 260 | None => self.selected_cell = Some(field::Coords { x: 0, y: 0 }), 261 | } 262 | } 263 | if pressed_key == &Key::Right { 264 | match self.selected_cell { 265 | Some(ref mut cell) => { 266 | if cell.x < 8 { 267 | cell.x += 1; 268 | } 269 | } 270 | None => self.selected_cell = Some(field::Coords { x: 0, y: 0 }), 271 | } 272 | } 273 | } 274 | 275 | fn on_mouse_click(&mut self, button: &MouseButton) { 276 | if let &MouseButton::Left = button { 277 | self.selected_cell = Some(field::Coords { 278 | x: (self.mouse_coords.x / self.settings.cell_size.x) as u8, 279 | y: (self.mouse_coords.y / self.settings.cell_size.y) as u8, 280 | }); 281 | } 282 | } 283 | 284 | pub fn on_mouse_move(&mut self, args: &[f64; 2]) { 285 | self.mouse_coords.x = args[0]; 286 | self.mouse_coords.y = args[1]; 287 | } 288 | } 289 | --------------------------------------------------------------------------------