Friday 15 July 2011

Sudoku Solver and HiddenSingles Java -



Sudoku Solver and HiddenSingles Java -

so trying write programme solves sudoku puzzles , works of time when seek solving puzzle (that can solved through hiddensingles) works , doesnt.

public class sudoku { private int row, column; private int[][]board; public sudoku () { } public sudoku (int[][]board) { column = board.length; row = board[0].length; this.board = board; } public void printboard() { (int = 0; < 9; i++) { if (i%3==0) { system.out.println("______________________________"); } (int j = 0; j < 9; j++) { if(j % 3 == 0) system.out.print("|"); if (j == 0) { system.out.printf("" + board[i][j]); } else { system.out.printf(" " + board[i][j]); } } system.out.print("|"); system.out.printf("\n"); } } public int [][] board() { homecoming board; } public boolean[] candidates(int row, int column) { boolean[] candidate = new boolean[10]; candidate[0] = false; (int = 1; < 10; i++) { if (!isrowoccupied (i, row, column) && !iscolumnoccupied (i, row, column) && !isboxoccupied (i, boxrow (row), boxcolumn(column), row, column)) { candidate[i] = true; } } homecoming candidate; } public int boxrow (int row) { int r = 0; if (row >= 3 && row <= 5 ) { r = 3; } if (row >= 6 && row <= 8) { r = 6; } homecoming r; } public int boxcolumn (int column) { int c = 0; if (column >= 3 && column <= 5) { c = 3; } if (column >= 6 && column <= 8) { c = 6; } homecoming c; } public boolean isrowoccupied (int value, int row, int column) { (int = 0; < board.length; i++) { if (i != column && board[row][i] == value) { homecoming true; } } homecoming false; } public boolean iscolumnoccupied (int value, int row, int column) { (int = 0; < board.length; i++) { if (i != row && board[i][column] == value) { homecoming true; } } homecoming false; } public boolean isboxoccupied (int value, int rowstart, int columnstart, int row, int column) { (int = rowstart; < rowstart + 3; i++) { (int j = columnstart; j < columnstart + 3; j++) { if (i != row && j != column && board[i][j] == value) { homecoming true; } } } homecoming false; } public boolean issolved() { (int = 0; < 9; i++) { (int j = 0; j < 9; j++) { if (board [i][j] == 0) { homecoming false; } } } homecoming true; } public void solve() { while (!issolved() && (hiddensingles()) && nakedsingles()) { // boolean [] array = candidates (0, 2); // (int = 0; < 10; i++) // { // system.out.println (array [i]); // } // printboard(); } } public boolean nakedsingles() { boolean nakedsingle = false; for(int = 0; < 9; i++) { for(int j = 0; j < 9; j++) { if (board[i][j] == 0) { boolean [] array = candidates (i, j); int count = 0, insert = 0; (int k = 1; k < array.length; k++) { if (array[k]) { count++; insert = k; } } if (count == 1) { board [i][j] = insert; nakedsingle = true; } } } } homecoming nakedsingle; } public boolean hiddenrow() { boolean hiddenrow = false; for(int = 0; < 9; i++) { for(int j = 0; j < 9; j++) { if (board[i][j] == 0) { boolean [] array = candidates (i, j); (int k = 0; k < 9; k++) { if (board[i][k] == 0 && k != j) { boolean [] array2 = candidates (i, k); (int m = 1; m < array2.length; m++) { if (array [m] == array2 [m] && array [m] == true) { array [m] = false; } } } } (int m = 1; m < 10; m++) { if (array [m]) { board [i][j] = m; hiddenrow = true; break; } } } } } homecoming hiddenrow; } public boolean hiddencolumn() { boolean hiddencolumn = false; for(int = 0; < 9; i++) { for(int j = 0; j < 9; j++) { if (board[i][j] == 0) { boolean [] array = candidates (i, j); (int k = 0; k < 9; k++) { if (board[k][j] == 0 && k != i) { boolean [] array2 = candidates (k, j); (int m = 1; m < array2.length; m++) { if (array [m] == array2 [m] && array [m] == true) { array [m] = false; } } } } (int m = 1; m < 10; m++) { if (array [m]) { board [i][j] = m; hiddencolumn = true; break; } } } } } homecoming hiddencolumn; } public boolean hiddenbox() { boolean hiddenbox = false; (int = 0; < 9; i++) { (int j = 0; j < 9; j++) { if (board[i][j] == 0) { boolean [] array = candidates (i, j); (int m = boxrow(i); m < boxrow(i) + 3; m++) { (int n = boxcolumn(j); n < boxcolumn (j) + 3; n++) { if (board [m][n] == 0 && m != && n != j) { boolean [] array2 = candidates (m, n); (int q = 1; q < array2.length; q++) { if (array [q] == array2 [q] && array [q]) { array [q] = false; } } } } } (int m = 1; m < 10; m++) { if (array [m]) { board [i][j] = m; hiddenbox = true; break; } } } } } homecoming hiddenbox; } public boolean hiddensingles() { boolean hiddensingle = false; if (hiddenrow() || hiddencolumn() || hiddenbox()) { hiddensingle = true; } // int [] countcandidates = {0,0,0,0,0,0,0,0,0,0}; // int insert = 0; // boolean hiddensingle = false; // boolean isrow = false; // boolean iscolumn = false; // for(int = 0; < 9; i++) // { // for(int j = 0; j < 9; j++) // { // if (board[i][j] == 0) // { // int count = 0; // boolean [] array = candidates (i, j); // //rows // (int k = 0; k < 9; k++) { // if (board[i][k] == 0 && k != j) { // // boolean [] array2 = candidates (i, k); // (int l = 1; l < array2.length; l++) // { // if (array [l] == array2 [l] && array [l]) // { // countcandidates [l] = ++countcandidates [l]; // } // } // } // } // (int l = 1; l < countcandidates.length; l++) // { // if (countcandidates [l] == 0) // { // insert = l; // board [i][j] = insert; // hiddensingle = true; // isrow = true; // break; // } // } // if (!isrow) // { // //columns // (int k = 0; k < 9; k++) { // // if (board[k][j] == 0 && k != i) { // boolean [] array2 = candidates (k, j); // (int l = 1; l < array2.length; l++) // { // if (array [l] == array2 [l] && array[l]) // { // countcandidates [l] = countcandidates [l]++; // } // } // } // } // (int l = 1; l < countcandidates.length; l++) // { // if (countcandidates [l] == 0) // { // insert = l; // board [i][j] = insert; // hiddensingle = true; // iscolumn = true; // break; // } // } // if (!iscolumn) // { // (int m = boxrow(i); m < boxrow(i) + 3; m++) // { // (int n = boxcolumn(j); n < boxcolumn (j) + 3; n++) // { // if (board [m][n] == 0 && m != && n != j) { // boolean [] array2 = candidates (m, n); // (int l = 1; l < array2.length; l++) // { // if (array [l] == array2 [l] && array [l]) // { // countcandidates [l] = countcandidates [l]++; // } // } // } // } // } // // (int l = 1; l < countcandidates.length; l++) // { // if (countcandidates [l] == 0) // { // insert = l; // board [i][j] = insert; // hiddensingle = true; // iscolumn = true; // break; // } // } // } // } // } // } // } homecoming hiddensingle; } public static void main(string[] args) { //string puzzle = ""; string puzzle = "0010002000000304000500060001050102070008000400020907080900070008000509000003000700"; int[][] value = new int [9][9]; int count = 0; (int = 0; < 9; i++) { (int j = 0; j < 9; j++) { value [i][j] = integer.parseint(puzzle.substring(count, count+1)); count++; } } sudoku = new sudoku(value); a.printboard(); a.solve(); } }

the 0's represent blanks in puzzle. can help me find whats wrong in hiddensingles method? can't seem find whats wrong in it...

thanks

java

No comments:

Post a Comment