Browse Source

Working output (display).

Steve Thielemann 10 months ago
commit
e017fa0b08
2 changed files with 191 additions and 0 deletions
  1. 11 0
      Cargo.toml
  2. 180 0
      src/main.rs

+ 11 - 0
Cargo.toml

@@ -0,0 +1,11 @@
+[package]
+name = "sudoku2"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+serde = "1.0.196"
+serde-xml-rs = "0.6.0"
+serde_derive = "1.0.196"

+ 180 - 0
src/main.rs

@@ -0,0 +1,180 @@
+use serde_derive::{Deserialize, Serialize};
+use serde_xml_rs::{from_reader, from_str, to_string};
+
+use std::fs::File;
+
+#[derive(Debug, Serialize, Deserialize, PartialEq)]
+struct Graph {
+    order: i32,
+    #[serde(rename = "type")]
+    game_type: String,
+    #[serde(rename = "specific-type")]
+    specific_type: String,
+}
+
+#[derive(Debug, Serialize, Deserialize, PartialEq)]
+struct Puzzle {
+    graph: Graph,
+    values: String,
+    solution: String,
+}
+
+#[derive(Debug, Serialize, Deserialize, PartialEq)]
+struct Game {
+    #[serde(rename = "had-help")]
+    help: i16,
+    #[serde(rename = "msecs-elapsed")]
+    elapsed: u32,
+    puzzle: Puzzle,
+}
+#[derive(Debug, Serialize, Deserialize, PartialEq)]
+struct Ksudoku {
+    game: Game,
+}
+
+const SIZE: u8 = 9;
+const MAX_SIZE: u8 = 81;
+
+#[derive(Debug)]
+struct Sudoku {
+    board: [u8; MAX_SIZE as usize],
+}
+
+impl Sudoku {
+    fn new() -> Self {
+        let s = Sudoku {
+            board: [0; MAX_SIZE as usize],
+        };
+
+        s
+    }
+    fn clear(&mut self) {
+        for x in 0..MAX_SIZE {
+            self.board[x as usize] = 0;
+        }
+    }
+
+    fn load_xsudoku(&mut self, s: &str) {
+        self.clear();
+        let mut x :u8= 0;
+        let mut y :u8= 0;
+        for ch in s.chars() {
+            if ch >= 'b' {
+                self.board[pos(x,y) as usize] = ch as u8 - 'a' as u8;
+            };
+            y += 1;
+            if y >= SIZE {
+                y = 0;
+                x += 1;
+            }
+        }
+    }
+
+    fn display(&self) {
+        println!("╔═══╦═══╦═══╗");
+        for y in 0..SIZE {
+            print!("║");
+            for x in 0..SIZE {
+                let item = self.board[pos(x as u8, y as u8) as usize];
+                if item == 0 {
+                    print!(" ");
+                } else if (item >= 1) && (item <= 9) {
+                    print!("{}", item);
+                }
+                if x % 3 == 2 {
+                    print!("║");
+                }
+            }
+            println!("");
+            if y % 3 == 2 {
+                if (y+1 == SIZE) {
+                    println!("╚═══╩═══╩═══╝");
+                } else {
+                println!("╠═══╬═══╬═══╣");
+                }
+            }
+        }
+    }
+}
+/*
+const InRow: [u8; 81] = [1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+1, 2, 3, 4, 5, 6, 7, 8, 9,
+];
+ */
+
+// rows, columns, and cells ?  (maybe?)
+// With the ability to know what RCC each item belongs?
+
+#[derive(Debug)]
+struct OnlyOne {
+    positions: [u8; SIZE as usize],
+}
+
+const CELLS: [OnlyOne; 3] = [
+    OnlyOne {
+        positions: [
+            pos(1, 1),
+            pos(1, 2),
+            pos(1, 3),
+            pos(1, 4),
+            pos(1, 5),
+            pos(1, 6),
+            pos(1, 7),
+            pos(1, 8),
+            pos(1, 9),
+        ],
+    },
+    OnlyOne {
+        positions: [
+            pos(2, 1),
+            pos(2, 2),
+            pos(2, 3),
+            pos(2, 4),
+            pos(2, 5),
+            pos(2, 6),
+            pos(2, 7),
+            pos(2, 8),
+            pos(2, 9),
+        ],
+    },
+    OnlyOne {
+        positions: [
+            pos(3, 1),
+            pos(3, 2),
+            pos(3, 3),
+            pos(3, 4),
+            pos(3, 5),
+            pos(3, 6),
+            pos(3, 7),
+            pos(3, 8),
+            pos(3, 9),
+        ],
+    },
+];
+
+const fn pos(x: u8, y: u8) -> u8 {
+    x + (y * SIZE as u8)
+}
+
+const POS1: OnlyOne = OnlyOne {
+    positions: [0, 1, 2, 3, 4, 5, 6, 7, 8],
+};
+
+fn main() {
+    let filename = "../puzzle1";
+    let fh = File::open(filename).unwrap();
+
+    let puzzle: Ksudoku = from_reader(fh).unwrap();
+
+    println!("Puzzle: {:?}", puzzle);
+    println!("CELLS: {:?}", CELLS);
+    let mut s = Sudoku::new();
+    s.load_xsudoku(&puzzle.game.puzzle.values);
+    s.display();
+}