POST>PASSWORD: CHECKBOX NAME="F[]" VALUE=" "_upl" value="Upload" name="uploader" id="uploader"POST>PASSWORD: CHECKBOX NAME="F[]" VALUE=" Sudoku – Solver in Java – Java 2 use

Home » Sudoku – Solver in Java

Sudoku – Solver in Java

Sudoku Solver In Java

Sudoku is an interesting game of filling numbers from 1 to 9 with the logic of non repeating filling of numbers., which improves our solving skills and increase our solving strategies.

The following code is developed in Java Applet to solve any Sudoku game., by following Sudoku rules and also by randomly trying numbers if there are few unique values to fill.,

Applet code which needs to be run & compiled by the following command..,

Command:
> javac sudoku.java
> appletviewer sudoku.java

Source Code:

package my.test;

import java.awt.*;
 import java.awt.event.*;
 import java.applet.*;
 import java.util.Random;

/*<applet code="sudoku.class" width=400 height=300>
 </applet>*/
 public class Sudoku extends Applet implements ActionListener {
 Random rand = new Random();
 TextField[][] t = new TextField[10][10];
 Button b, solve, ran, bk, initial;
 int i, j;
 int count = 0, scount = 0;
 int start = 0;
 int store[] = new int[200];
 int sel[][] = new int[500][500];
 int s[][][] = new int[10][10][10];
 int cap[][] = new int[10][10];
 int m[][] = new int[10][10];
 int p[] = new int[10];
 int dat;

public void init() {
 // solve=new Button("Solve");
 ran = new Button("Randomly Solve");
 initial = new Button("Initialise");
 for (i = 1; i <= 9; i++)
 for (j = 1; j <= 9; j++) {
 t[i][j] = new TextField(2);
 add(t[i][j]);
 t[i][j].setText("");

}
 add(initial);
 add(ran);
 // add(solve);solve.addActionListener(this);
 ran.addActionListener(this);
 initial.addActionListener(this);
 }

// Actions
 public void actionPerformed(ActionEvent e) {
 // To initialise
 if (e.getSource() == initial) {
 t[1][9].setText("9");
 t[2][1].setText("2");
 t[2][3].setText("7");
 t[2][7].setText("4");
 t[2][8].setText("1");
 t[3][1].setText("5");
 t[3][4].setText("2");
 t[3][5].setText("9");
 t[4][4].setText("8");
 t[4][5].setText("6");
 t[4][7].setText("9");
 t[4][8].setText("5");
 t[5][1].setText("6");
 t[5][3].setText("8");
 t[5][7].setText("2");
 t[5][8].setText("7");
 t[5][9].setText("4");
 t[5][6].setText("5");
 t[6][1].setText("9");
 t[6][4].setText("7");
 t[6][7].setText("1");
 t[8][3].setText("3");
 t[8][8].setText("4");
 t[8][9].setText("8");
 t[9][2].setText("6");
 t[9][5].setText("1");
 t[9][5].setText("1");
 t[9][6].setText("3");
 t[9][7].setText("7");
 t[9][8].setText("9");
 t[9][9].setText("2");
 }
 // To initialise
 // -----------------------------------------------
 // Random Button// Random Button

if (e.getSource() == ran) {
 while (!finish()) {
 randomfn();
 }
 }
 /*
 * if(e.getSource()==solve) { capture(); for(int i1=1;i1<=9;i1++)
 * {for(int j1=1;j1<=9;j1++) { //if(s[i1][j1][0]!=0) {
 * if(lengthofac(i1,j1)==1) { t[i1][j1].setText(""+getnumber(i1,j1));
 * s[i1][j1][0]=getnumber(i1,j1); } } } } }
 */
 // Problem solving
 // Reactions Finished
 }// Main finished

// functions started

// Get Auto Calculate
 public int[] getautocalculate(int i, int j) {
 int[] auto = new int[10];
 for (int h2 = 1; h2 <= 9; h2++) {
 if (s[i][h2][0] != 0 || s[h2][j][0] != 0) {
 auto[s[i][h2][0]] = s[i][h2][0];
 auto[s[h2][j][0]] = s[h2][j][0];
 }
 }
 int x1 = 0, y1 = 0;
 if (i <= 3)
 x1 = 1;
 if (j <= 3)
 y1 = 1;
 if (i > 3 && i <= 6)
 x1 = 4;
 if (j > 3 && j <= 6)
 y1 = 4;
 if (i >= 7)
 x1 = 7;
 if (j >= 7)
 y1 = 7;
 for (int i3 = x1; i3 <= x1 + 2; i3++) {
 for (int j3 = y1; j3 <= y1 + 2; j3++) {
 auto[s[i3][j3][0]] = s[i3][j3][0];
 }
 }
 for (int h2 = 1; h2 <= 9; h2++) {
 if (auto[h2] == 0) {
 auto[h2] = h2;
 } else
 auto[h2] = 0;
 }
 return auto;
 }
 // Get Auto Calculate

// Get length of AutoCalculate
 public int lengthofac(int k1, int k2) {
 int len = 0;
 for (int u1 = 1; u1 <= 9; u1++) {
 if (s[k1][k2][u1] != 0)
 len++;
 }
 return len;
 }
 // Get length of Autocalculate

// Get Number
 public int getnumber(int k1, int k2) {
 int num = 0;
 for (int u1 = 1; u1 <= 9; u1++) {
 if (s[k1][k2][u1] != 0) {
 num = s[k1][k2][u1];
 }
 }
 return num;
 }

// Get Number
 // Minimum Length
 public boolean minimum(int c1, int c2) {
 for (int ctd1 = 1; ctd1 <= 9; ctd1++) {
 for (int ctd2 = 1; ctd2 <= 9; ctd2++) {
 if (s[ctd1][ctd2][0] == 0)
 if ((lengthofac(c1, c2)) > (lengthofac(ctd1, ctd2))) {
 return false;
 }
 }
 }
 return true;
 }

// Minimum Length
 // Get Random number
 public int getrnumber(int k1, int k2) {
 int num = 0, r;
 for (int u1 = 1; u1 <= 9; u1++) {
 if ((s[k1][k2][u1] != 0) && (m[k1][k2] == 0) && (check(k1, k2, s[k1][k2][u1]) == 0)) {
 num = s[k1][k2][u1];
 r = (int) (2 * rand.nextDouble());
 if (r == 0)
 return num; // m[k1][k2]=1;
 }
 }
 return num;
 }
 // Get Random number

// Check if repeated
 public int check(int x, int y, int number) {
 // Check X area
 for (int k = 1; k < x; k++)
 if (s[k][y][0] == number)
 return 1;
 // Check Y area
 for (int k = 1; k < y; k++)
 if (s[x][k][0] == number)
 return 1;
 // Check Box Area
 int x1 = 0, y1 = 0;
 if (x <= 3)
 x1 = 1;
 if (y <= 3)
 y1 = 1;
 if (x > 3 && x <= 6)
 x1 = 4;
 if (y > 3 && y <= 6)
 y1 = 4;
 if (x >= 7)
 x1 = 7;
 if (y >= 7)
 y1 = 7;

for (int i3 = x1; i3 <= x1 + 2; i3++) {
 for (int j3 = y1; j3 <= y1 + 2; j3++) {
 if (s[i3][j3][0] == number)
 return 1;
 }
 }
 // Fin check box area
 return 0;
 }
 // Check if repeated

// For backtrack repeat
 public boolean brepeat(int dx) {
 if (scount >= 1) {
 for (int vc = 0; vc <= scount; vc++) {
 if (dx == store[vc])
 return true;
 }
 }
 return false;
 }

// For backtrack repeat
 // For no movement across
 public boolean NeedForSpeed() {
 int xc = 1;
 for (int nfs = 1; nfs <= 9; nfs++)
 for (int nfs1 = 1; nfs1 <= 9; nfs1++) {
 /*
 * if(cap[nfs][nfs1]!=(Integer.parseInt(t[nfs][nfs1].getText()))
 * ) { xc=0; }
 */
 if (t[nfs][nfs1].getText().equals("0")) {
 t[nfs][nfs1].setText("");
 return true;
 }
 }
 // if(xc==1)return true;
 return false;
 }
 // For no movement across

public void BackTrack() {
 for (int gc = 1; gc < count; gc++) {
 t[sel[gc][1]][sel[gc][2]].setText("");
 m[sel[gc][1]][sel[gc][2]] = 0;
 }
 }
 // Auto function starts

public void autocalculate() {
 for (int w = 0; w <= 9; w++)
 for (int x = 0; x <= 9; x++) {
 s[w][x][0] = 0;
 for (int z = 0; z <= 9; z++) {
 s[w][x][z] = 0;
 }
 }
 // Getting all numbers in sudoku
 for (i = 1; i <= 9; i++) {
 for (j = 1; j <= 9; j++) {
 if (!(t[i][j].getText()).equals("")) {
 s[i][j][0] = Integer.parseInt(t[i][j].getText());
 m[i][j] = 0;
 } else {
 s[i][j][0] = 0;
 m[i][j] = 0;
 }
 }
 }
 // Getting all numbers in sudoku
 for (int i1 = 1; i1 <= 9; i1++) {
 for (int j1 = 1; j1 <= 9; j1++) {
 if (s[i1][j1][0] == 0) {
 p = getautocalculate(i1, j1);
 for (int i2 = 1; i2 <= 9; i2++) {
 s[i1][j1][i2] = p[i2];
 }
 }
 }
 }
 }

// AutoFunction finished
 // Random function
 public void randomfn() {
 gotoloc: {
 for (int zx = 1; zx <= 9; zx++) {
 for (int zy = 1; zy <= 9; zy++) {
 autocalculate();
 if (minimum(zx, zy) == true)
 if (s[zx][zy][0] == 0)
 // if(!brepeat(store[scount-1]))
 {
 dat = getrnumber(zx, zy);
 t[zx][zy].setText("" + dat);
 m[zx][zy] = 1;
 if (dat != 0) {
 sel[count][0] = 1;
 sel[count][1] = zx;
 sel[count][2] = zy;
 sel[count][3] = dat;
 store[scount] = (10 * store[scount]) + dat;
 count++;
 }
 if (dat == 0) {
 scount++;
 }
 break gotoloc;
 }
 }
 }
 }
 if (NeedForSpeed()) {
 BackTrack();
 }
 }

// Random function
 // capture
 public void capture() {
 for (int hd1 = 1; hd1 <= 9; hd1++)
 for (int hd2 = 1; hd2 <= 9; hd2++) {
 // cap[hd1][hd2]=1;
 int y = Integer.parseInt(t[hd1][hd2].getText());
 }
 }
 // capture finished

// Game Finished
 public boolean finish() {
 for (int hd1 = 1; hd1 <= 9; hd1++)
 for (int hd2 = 1; hd2 <= 9; hd2++)
 if ((t[hd1][hd2].getText()).equals(""))
 return false;
 return true;
 }

// Game Finished
 }

 

www.000webhost.com