rendered paste bodyimport java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sudoku{
private JFrame frame;
private JPanel area;
private JPanel buttons;
private JButton solve;
private JButton clear;
private int[][] matrix;
private JTextField[][] JTF;
private boolean[][] change;
private int count=0;
public class Solve implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(readMatrix()){
if(solveNext(matrix,change,0,-1)==true){
showSolution();
System.out.println("Done");
}else{
System.out.println("Inga lösningar");
}
}else{
System.out.println("Matat in fel");
}
}
}
public boolean readMatrix(){
for(int i =0; i<=8; i++){
for(int j=0; j<=8; j++){
if(JTF[i][j].getText().equals("")){
matrix[i][j]=-1;
change[i][j]=true;
JTF[i][j].setForeground(Color.black);
}else{
matrix[i][j]=Integer.parseInt(JTF[i][j].getText());
change[i][j]=false;
JTF[i][j].setForeground(Color.blue);
}
}
}
for(int i =0; i<=8; i++){
for(int j=0; j<=8; j++){
if(chk(matrix, i,j)==false){
return false;
}
}
}
return true;
}
public void showSolution(){
String s = "";
for(int i =0; i<=8; i++){
for(int j=0; j<=8; j++){
s = ""+matrix[i][j];
JTF[i][j].setText(s);
}
}
}
public class Clear implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i=0; i<=8;i++){
for(int j =0; j<=8;j++){
JTF[i][j].setText("");
}
}
}
}
public Sudoku(){
//Create matrix
matrix = new int[9][9];
change = new boolean[9][9];
}
public void createWindow(){
frame = new JFrame("test");
area = new JPanel(new GridLayout(9, 9));
frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
area.setPreferredSize(new Dimension(200, 200));
JTF = new JTextField[9][9];
Color C = Color.white;
for(int i=0; i<=8;i++){
for(int j =0; j<=8;j++){
if(j == 3 || j ==6){
C=changeColor(C);
}
if((i == 3|| i == 6) && j==0){
C=changeColor(C);
}
JTF[i][j] = new JTextField(1);
JTF[i][j].setHorizontalAlignment(0);
JTF[i][j].setBackground(C);
area.add(JTF[i][j]);
}
}
frame.add(area, BorderLayout.NORTH);
buttons = new JPanel(new GridLayout(1, 2));
buttons.setPreferredSize(new Dimension(100,20));
solve = new JButton("Lös");
clear = new JButton("Rensa");
buttons.add(solve);
buttons.add(clear);
frame.add(buttons, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
area.setVisible(true);
solve.addActionListener(new Solve());
clear.addActionListener(new Clear());
}
public static void main(String [] arg) {
Sudoku S = new Sudoku();
S.createWindow();
}
public static Color changeColor(Color C){
if (C == Color.lightGray){
return Color.white;
}
return Color.lightGray;
}
public boolean solveNext(int[][] matrix,boolean[][]change, int row, int col) {
col++;
if(col == 9){
row++;
col = 0;
}
while(change[row][col]== false ){
col++;
if(col == 9){
row++;
col = 0;
}
}
for(int k=1; k<=9;k++){
matrix[row][col] = k;
boolean chkk = chk(matrix,row,col);
if(chkk){
if(row==8 && col==8){
return true;
}
if(solveNext(matrix,change,row,col)==true){
return true;
}
}
}
matrix[row][col] = -1;
return false;
}
public boolean chk(int[][] matrix, int row, int col){
for(int i = 0; i<=8;i++){
for(int k = 0; k<=8;k++){
if(matrix[row][i] == matrix[row][k]&& i!=k && matrix[row][i]!=-1)
return false;
if(matrix[i][col] == matrix[k][col] && i!=k && matrix[i][col]!=-1){
return false;
}
}
}
for (int x = 0; x <= 6; x=x+3){
for (int y = 0;y <= 6; y=y+3){
if(row == x || row == x+1 || row == x+2){
if(col == y|| col == y+1|| col == y+2){
for(int i = x; i<=(2+x); i++){
for(int j = y; j<= (2+y); j++){
if(row != i && col !=j){
if (matrix[row][col]==matrix[i][j] && matrix[row][col]!=-1){
return false;
}
}
}
}
}
}
}
}
return true;
}
}