Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JTextArea;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class Sudoku extends javax.swing.JFrame {
- public static boolean goAgain = false;
- public static boolean[][][] possibilities = new boolean[9][9][9];
- public static boolean[] noPossibilities = {false, false, false, false, false, false, false, false, false};
- public static int[][] definite = new int[9][9];
- public static JTextArea[][] field = new JTextArea[9][9];
- public static Font font = new Font("Veranda", Font.BOLD, 48);
- public static void initGUI() {
- JFrame frame = new JFrame();
- frame.setTitle("Sudoku");
- frame.setLocation(500, 0);
- frame.setLayout(new GridLayout(10,9));
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //Initiate faces
- for( int j = 0; j < 9; j++) {
- for( int i = 0; i < 9; i++) {
- field[i][j] = new JTextArea();
- field[i][j].setVisible(true);
- field[i][j].setFont(font);
- field[i][j].setText("");
- field[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
- frame.add(field[i][j]);
- if((i<=2&&(j<=2||j>=6))||(i>=6&&(j<=2||j>=6))||((i>2&&i<6)&&(j>2&&j<6))) {
- field[i][j].setBackground(Color.LIGHT_GRAY);
- }
- }
- }
- //Initiate buttons
- JButton solveMe = new JButton("Generate");
- JButton resetMe = new JButton("Reset");
- JButton genSudoku = new JButton("New");
- solveMe.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- for( int j = 0; j < 9; j++) {
- for( int i = 0; i < 9; i++) {
- if(!field[i][j].getText().isEmpty()) {
- String numberString = field[i][j].getText();
- definite[i][j] = Integer.parseInt(numberString);
- } else {
- definite[i][j] = 0;
- }
- }
- }
- generateSudoku(definite);
- }
- });
- resetMe.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- for( int j = 0; j < 9; j++) {
- for( int i = 0; i < 9; i++) {
- field[i][j].setText("");
- definite[i][j] = 0;
- }
- }
- }
- });
- frame.add(solveMe);
- frame.add(resetMe);
- frame.add(genSudoku);
- frame.setSize(650,750);
- frame.setVisible(true);
- }
- public static void getPossibilities() {
- boolean[] noPossibilities = {false, false, false, false, false, false, false, false, false};
- for( int j = 0; j < 9; j++) {
- for( int i = 0; i < 9; i++) {
- if(definite[i][j] == 0) {
- boolean[] defaultPossibilities = {true, true, true, true, true, true, true, true, true};
- possibilities[i][j] = defaultPossibilities;
- for(int x = 0; x < 9; x++) {
- if(definite[x][j] != 0) {
- possibilities[i][j][definite[x][j]-1] = false;
- }
- }
- for(int y = 0; y < 9; y++) {
- if(definite[i][y] != 0) {
- possibilities[i][j][definite[i][y]-1] = false;
- }
- }
- if(i <= 2) {
- if(j <= 2) {
- for(int x = 0; x < 3; x++) {
- for(int y = 0; y < 3; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- } else if(j > 2 && j < 6) {
- for(int x = 0; x < 3; x++) {
- for(int y = 3; y < 6; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- } else if(j > 5) {
- for(int x = 0; x < 3; x++) {
- for(int y = 6; y < 9; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- }
- } else if(i > 2 && i < 6) {
- if(j <= 2) {
- for(int x = 3; x < 6; x++) {
- for(int y = 0; y < 3; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- } else if(j > 2 && j < 6) {
- for(int x = 3; x < 6; x++) {
- for(int y = 3; y < 6; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- } else if(j > 5) {
- for(int x = 3; x < 6; x++) {
- for(int y = 6; y < 9; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- }
- } else if(i > 5) {
- if(j <= 2) {
- for(int x = 6; x < 9; x++) {
- for(int y = 0; y < 3; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- } else if(j > 2 && j < 6) {
- for(int x = 6; x < 9; x++) {
- for(int y = 3; y < 6; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- } else if(j > 5) {
- for(int x = 6; x < 9; x++) {
- for(int y = 6; y < 9; y++) {
- if(definite[x][y] != 0) {
- possibilities[i][j][definite[x][y]-1] = false;
- }
- }
- }
- }
- }
- } else {
- possibilities[i][j] = noPossibilities;
- }
- }
- }
- }
- public static void fillIn(int i, int j) {
- int count = 0;
- int value = 0;
- for(int n = 0; n < 9; n++) {
- if(possibilities[i][j][n]) {
- count++;
- value = n;
- }
- }
- if(count != 1) {
- value = 0;
- count = 0;
- } else {
- possibilities[i][j] = noPossibilities;
- value += 1;
- definite[i][j] = value;
- field[i][j].setText(""+value);
- System.out.println("Value: "+value);
- value = 0;
- count = 0;
- goAgain = true;
- }
- }
- public static void fillIn2() {
- getPossibilities();
- while(goAgain) {
- getPossibilities();
- goAgain = false;
- for(int i = 0; i < 9; i++) {
- for(int j = 0; j < 9; j++) {
- fillIn(i, j);
- }
- }
- }
- }
- public static void fillIn3() {
- getPossibilities();
- boolean[][][] goHereHori = new boolean[9][9][9];
- boolean[][][] goHereVert = new boolean[9][9][9];
- boolean[][][] goHereGrid = new boolean[9][9][9];
- for(int a = 0; a < 9; a++) {
- for(int b = 0; b < 9; b++) {
- for(int c = 0; c < 9; c++) {
- goHereHori[a][b][c] = true;
- goHereVert[a][b][c] = true;
- goHereGrid[a][b][c] = true;
- }
- }
- }
- for(int i = 0; i < 9; i++) {
- for(int j = 0; j < 9; j++) {
- for(int n = 0; n < 9; n++) {
- if(possibilities[i][j][n]) {
- for(int x = 0; x < 9; x++) {
- if(possibilities[x][j][n] && x != i) {
- goHereHori[i][j][n] = false;
- }
- }
- for(int y = 0; y < 9; y++) {
- if(possibilities[i][y][n] && y != j) {
- goHereVert[i][j][n] = false;
- }
- }
- if(j < 3) {
- if(i < 3) {
- for(int x = 0; x < 3; x++) {
- for(int y = 0; y < 3; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- } else if(i < 6 && i > 2) {
- for(int x = 3; x < 6; x++) {
- for(int y = 0; y < 3; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- } else {
- for(int x = 6; x < 9; x++) {
- for(int y = 0; y < 3; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- }
- } else if(j < 6 && j > 2) {
- if(i < 3) {
- for(int x = 0; x < 3; x++) {
- for(int y = 3; y < 6; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- } else if(i < 6 && i > 2) {
- for(int x = 3; x < 6; x++) {
- for(int y = 3; y < 6; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- } else {
- for(int x = 6; x < 9; x++) {
- for(int y = 3; y < 6; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- }
- } else {
- if(i < 3) {
- for(int x = 0; x < 3; x++) {
- for(int y = 6; y < 9; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- } else if(i < 6 && i > 2) {
- for(int x = 3; x < 6; x++) {
- for(int y = 6; y < 9; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- } else {
- for(int x = 6; x < 9; x++) {
- for(int y = 6; y < 9; y++) {
- if(possibilities[x][y][n] && !(y == j && x == i)) {
- goHereGrid[i][j][n] = false;
- }
- }
- }
- }
- }
- } else {
- goHereHori[i][j][n] = false;
- goHereVert[i][j][n] = false;
- goHereGrid[i][j][n] = false;
- }
- }
- }
- }
- for(int i = 0; i < 9; i++) {
- for(int j = 0; j < 9; j++) {
- for(int n = 0; n < 9; n++) {
- System.out.println("goHere "+i+" "+j+" "+n+" = "+(goHereVert[i][j][n] || goHereHori[i][j][n] || goHereGrid[i][j][n]));
- if(goHereVert[i][j][n] || goHereHori[i][j][n] || goHereGrid[i][j][n]) {
- definite[i][j] = n+1;
- field[i][j].setText(""+(n+1));
- possibilities[i][j] = noPossibilities;
- goAgain = true;
- }
- }
- }
- }
- }
- public static void fillIn4() {
- getPossibilities();
- while(goAgain) {
- getPossibilities();
- goAgain = false;
- fillIn3();
- }
- }
- public static void generateSudoku(int[][] num) {
- getPossibilities();
- for(int i = 0; i < 9; i++) {
- for(int j = 0; j < 9; j++) {
- fillIn(i, j);
- }
- }
- //FIX THIS.
- fillIn2();
- fillIn3();
- fillIn4();
- }
- public static void main(String[] args) {
- initGUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement