good evening, i tried to create an application who generate an initial sudoku and trying to complete it using A* algorithm, while executing, the initial state it shows, but when it starts to solve, the program closes without showing a result, why?
UPDATE EXPLANATION: I created the initial sudoku and from it, I have to reach the final solution using linkedlists & Astar algorithm, the heuristic used here is minimum remaining values. I hope I explained the problem.
=============================
Console preview :
0 0 0 0 6 6 0 0 0
3 0 0 0 9 7 0 0 7
0 0 5 1 0 6 0 0 0
0 0 0 7 3 0 0 6 7
8 0 0 0 0 0 8 0 0
0 5 9 0 0 0 6 2 9
0 0 0 0 9 5 1 8 0
0 0 0 0 0 1 0 0 0
0 0 0 7 0 9 0 7 0
=============================
package sudoku;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.*;
public class Sudoku extends JFrame implements Comparable<Object>
{
/**
*
*/
private static final long serialVersionUID = 1L;
private int idp;
private int id;
private int[][] sudoku;
private int astar;
private LinkedList <Sudoku> sons ;
private boolean isFinished;
private boolean isValid;
public Sudoku()
{
isValid = false;
isFinished = false;
idp = 0;
id = 0;
sudoku = new int[9][9];
astar = 0;
sons = new LinkedList<Sudoku>();
init();
}
public Sudoku(int id,int[][] s)
{
isValid = false;
isFinished = false;
idp = id;
id = id+1;
sudoku = s;
astar = astar();
sons = new LinkedList<Sudoku>();
}
public void init()
{
int diff = 31;
Random r = new Random();
while(diff>0)
{
int x = r.nextInt(9);
int y = r.nextInt(9);
sudoku[x][y] = 1+ r.nextInt(9);
astar += sudoku[x][y];
diff--;
}
astar = 405 - astar;
}
public int astar()
{
int res=0;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
res += sudoku[i][j];
}
}
res = 405-res;
return res;
}
public void checkFinish()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(sudoku[i][j]!=0)
{
isFinished = true;
}
else
{
isFinished = false;
return;
}
}
}
}
public boolean isValidSudoku(int[][] sudoku)
{
if(isFinished)
{
if (sudoku == null || sudoku.length != 9 || sudoku[0].length != 9)
return false;
// check each column
for (int i = 0; i < 9; i++)
{
boolean[] m = new boolean[9];
for (int j = 0; j < 9; j++)
{
if (sudoku[i][j] != 0)
{
if (m[ (sudoku[i][j] - 1)])
{
return false;
}
m[ (sudoku[i][j] - 1)] = true;
}
}
}
//check each row
for (int j = 0; j < 9; j++)
{
boolean[] m = new boolean[9];
for (int i = 0; i < 9; i++)
{
if (sudoku[i][j] != 0)
{
if (m[ (sudoku[i][j] - 1)])
{
return false;
}
m[ (sudoku[i][j] - 1)] = true;
}
}
}
//check each 3*3 matrix
for (int block = 0; block < 9; block++)
{
boolean[] m = new boolean[9];
for (int i = block / 3 * 3; i < block / 3 * 3 + 3; i++)
{
for (int j = block % 3 * 3; j < block % 3 * 3 + 3; j++)
{
if (sudoku[i][j] != 0)
{
if (m[(int) (sudoku[i][j] - 1)])
{
return false;
}
m[(int) (sudoku[i][j] - 1)] = true;
}
}
}
}
return true;
}
else
{
System.out.println("not finished yet");
}
return false;
}
public void checkSolution()
{
if(isValidSudoku(sudoku))
{
isValid = true;
}
else
{
isValid = false;
}
}
public void viewSolution()
{
checkSolution();
if(isValid)
{
JFrame frame = new JFrame("Sudoku");
JTextField[][] s = new JTextField[9][9];
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(500, 500);
for(int i =0;i<9;i++)
{
for(int j =0;j<9;j++)
{
s[i][j].setText(String.valueOf(sudoku[i][j]));
s[i][j].setVisible(true);
s[i][j].setSize(50, 50);
s[i][j].setLocation(50*i, 50*j);
frame.add(s[i][j]);
}
}
}
}
public void show()
{
for(int i =0;i<9;i++)
{
for(int j =0;j<9;j++)
{
System.out.print(sudoku[i][j]+" ");
}
System.out.println("");
}
}
public void solve(Sudoku sudoku)
{
for(int i = 0;i<9;i++)
{
for(int j = 0;j<9;j++)
{
int[][] ts = sudoku.getSudoku();
if(ts[i][j]==0)
{
for(int k=0;k<9;k++)
{
ts[i][j] = k+1;
Sudoku t = new Sudoku(id,ts);
sons.add(t);
}
}
}
}
Iterator<Sudoku> iter = sons.iterator();
Collections.sort(sons);
while(iter.hasNext() && iter.next().getAstar()>0)
{
Sudoku temp = iter.next();
temp.solve(temp);
}
checkFinish();
if(sudoku.isFinished)
{
viewSolution();
}
}
public int getIdp()
{
return idp;
}
public int getId()
{
return id;
}
public int[][] getSudoku()
{
return sudoku;
}
public int getAstar()
{
return astar;
}
public LinkedList<Sudoku> getSons()
{
return sons;
}
public int compareTo(Object other)
{
Sudoku temp = (Sudoku)other;
if (this.astar < temp.astar)
return 1;
else if (this.astar > temp.astar)
return -1;
else
return 0;
}
}
==================================================
package sudoku;
public class Main
{
public static void main(String[] args)
{
Sudoku s = new Sudoku();
s.show();
s.solve(s);
}
}
Your show() method is overriding the java.awt.Window.show() method. You need to call super.show() as the first line in that method. So your new method would be:
public void show()
{
super.show();//Call to super class to let it paint the window
for(int i =0;i<9;i++)
{
for(int j =0;j<9;j++)
{
System.out.print(sudoku[i][j]+" ");
}
System.out.println("");
}
}
After I made this change it shows the JFrame. I also made the JFrame centered and fixed it so that when the X is clicked the application closes. Below is my modified Main class. The JFrame doesn't show anything but a gray background. Looks like you still have some work to do to render your components. I hope this gets you closer to your solution.
package sudoku;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
Sudoku s = new Sudoku();
s.setSize(400, 400);
//Center the JFrame
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
s.setLocation(dim.width/2-s.getSize().width/2, dim.height/2-s.getSize().height/2);
//Close application when the user clicks the X
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.show();
s.solve(s);
}