From f286cbfbea2e6d5e5282485cd6cd81a697154d7a Mon Sep 17 00:00:00 2001 From: Maxime Vorwerk Date: Fri, 4 Oct 2024 01:42:01 +0200 Subject: [PATCH] base java functionality --- java/Cell.java | 52 ++++++++++++++++ java/Grid.java | 137 +++++++++++++++++++++++++++++++++++++++++ java/Solver.java | 4 ++ java/SudokuSolver.java | 10 +++ 4 files changed, 203 insertions(+) create mode 100644 java/Cell.java create mode 100644 java/Grid.java create mode 100644 java/Solver.java create mode 100644 java/SudokuSolver.java diff --git a/java/Cell.java b/java/Cell.java new file mode 100644 index 0000000..bd40813 --- /dev/null +++ b/java/Cell.java @@ -0,0 +1,52 @@ +import java.util.BitSet; + +public class Cell { + private BitSet candidates = new BitSet(9); + private Cell[] row; + private Cell[] col; + private Cell[] square; + public final int pos; + private int n_set; + + public Cell(int pos) { + this.candidates.set(0, 9); + this.n_set = 9; + this.pos = pos; + } + + public void finishInit(Cell[] row, Cell[] col, Cell[] square) { + this.row = row; + this.col = col; + this.square = square; + } + + public void set(int i) { + this.candidates.clear(0, 9); + this.candidates.set(i-1); + this.n_set = 1; + + for (Cell cell: this.row) { + cell.clear(i); + } + for (Cell cell: this.col) { + cell.clear(i); + } + for (Cell cell: this.square) { + cell.clear(i); + } + } + + public void clear(int i) { + this.candidates.clear(i-1); + this.n_set -= 1; + } + + public boolean probe(int i) { + return candidates.get(i-1); + } + + public boolean getIsSet() { + return this.n_set == 1; + } +} + diff --git a/java/Grid.java b/java/Grid.java new file mode 100644 index 0000000..dfc7b4b --- /dev/null +++ b/java/Grid.java @@ -0,0 +1,137 @@ + +public class Grid { + private Cell[] cells = new Cell[81]; + private Cell[][] rows = new Cell[9][9]; + private Cell[][] squares = new Cell[9][9]; + + public Grid() { + initializeCells(); + } + + public Grid(String repr) { + initializeCells(); + parseRepr(repr); + } + + private void initializeCells() { + for (int i = 0; i < 81; i++) { + this.cells[i] = new Cell(i); + } + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + Cell cell = this.cells[9*i +j]; + + this.rows[i][j] = cell; + this.squares[3*(i/3) +(j/3)][3*(i%3) +(j%3)] = cell; + } + } + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + Cell cell = this.cells[9*i +j]; + int curr_square = 3*(i%3) + (j%3); + + Cell[] row = new Cell[8]; + Cell[] col = new Cell[8]; + Cell[] square = new Cell[8]; + + int offset_row = 0; + int offset_col = 0; + int offset_square = 0; + for (int k = 0; k < 8; k++) { + if (k == j) { + offset_row = 1; + } + if (k == i) { + offset_col = 1; + } + if (k == curr_square) { + offset_square = 1; + } + + row[k] = this.rows[i][k+offset_row]; + col[k] = this.rows[k+offset_col][j]; + square[k] = this.squares[3*(i/3) +(j/3)][k+offset_square]; + } + + cell.finishInit(row, col, square); + } + } + } + + @Deprecated + public void setCell(int n, int v) { + this.cells[n].set(v); + } + + private void parseRepr(String repr) { + String[] rows = repr.split("\\."); + assert rows.length == 9; + for (int i = 0; i < rows.length; i++) { + String line = rows[i]; + assert line.length() == 9; + + for (int j = 0; j < 9; j++) { + char c = line.charAt(j); + if (c != ' ') { + int val = c - '0'; + this.cells[9*i +j].set(val); + } + } + } + } + + @Override + public String toString() { + Character[][] values = new Character[27][27]; + for (int i = 0; i < 27; i++) { + for (int j = 0; j < 27; j++) { + int value = 3*(i%3) +(j%3) +1; + values[i][j] = this.rows[i/3][j/3].probe(value) + ? (char)('0'+value) + : ' '; + } + } + String formatString = "%c%c%c %c%c%c %c%c%c | %c%c%c %c%c%c %c%c%c | %c%c%c %c%c%c %c%c%c\n"; + String delim1 = " | |\n"; + String delim2 = "---------------------------------------\n"; + return + String.format(formatString, (Object [])values[0])+ + String.format(formatString, (Object [])values[1])+ + String.format(formatString, (Object [])values[2])+ + delim1+ + String.format(formatString, (Object [])values[3])+ + String.format(formatString, (Object [])values[4])+ + String.format(formatString, (Object [])values[5])+ + delim1+ + String.format(formatString, (Object [])values[6])+ + String.format(formatString, (Object [])values[7])+ + String.format(formatString, (Object [])values[8])+ + delim2+ + String.format(formatString, (Object [])values[9])+ + String.format(formatString, (Object [])values[10])+ + String.format(formatString, (Object [])values[11])+ + delim1+ + String.format(formatString, (Object [])values[12])+ + String.format(formatString, (Object [])values[13])+ + String.format(formatString, (Object [])values[14])+ + delim1+ + String.format(formatString, (Object [])values[15])+ + String.format(formatString, (Object [])values[16])+ + String.format(formatString, (Object [])values[17])+ + delim2+ + String.format(formatString, (Object [])values[18])+ + String.format(formatString, (Object [])values[19])+ + String.format(formatString, (Object [])values[20])+ + delim1+ + String.format(formatString, (Object [])values[21])+ + String.format(formatString, (Object [])values[22])+ + String.format(formatString, (Object [])values[23])+ + delim1+ + String.format(formatString, (Object [])values[24])+ + String.format(formatString, (Object [])values[25])+ + String.format(formatString, (Object [])values[26]); + } +} + diff --git a/java/Solver.java b/java/Solver.java new file mode 100644 index 0000000..caaf5b2 --- /dev/null +++ b/java/Solver.java @@ -0,0 +1,4 @@ + +public class Solver { +} + diff --git a/java/SudokuSolver.java b/java/SudokuSolver.java new file mode 100644 index 0000000..7a9265a --- /dev/null +++ b/java/SudokuSolver.java @@ -0,0 +1,10 @@ + +public class SudokuSolver { + public static final String hard = " 5 1 6.3 5 8 9 . 7 4 . 2 . 9 31 . 1 9. 8 36 9 5.92 .6 7 8 "; + public static final String extreme = " 1 7 . 1 485.84 6 3 .5 19 . 35 6. 5 . 593 64 .18 72 3.3 "; + public static void main(String[] args) { + Grid G = new Grid(SudokuSolver.hard); + System.out.println(G); + } +} +