ghorardim

suduko solver javaswing project

May 21st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. package suduko_solver;
  2.  
  3. import java.awt.Container;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11.  
  12. public class Suduko_solver extends JFrame implements ActionListener {
  13.  
  14.     private final Container C;
  15.     private final int[][] grid = new int[10][10];
  16.     private final JTextField tf[][] = new JTextField[10][10];
  17.     private JButton btn, btn2;
  18.     private Font f;
  19.  
  20.     boolean isSafe(int num, int r, int c) {
  21.         int i, j, rs, cs;
  22.         for (i = 0; i < 9; i++) {
  23.             if (grid[r][i] == num) {
  24.                 return false;
  25.             }
  26.         }
  27.         for (i = 0; i < 9; i++) {
  28.             if (grid[i][c] == num) {
  29.                 return false;
  30.             }
  31.         }
  32.         rs = r - r % 3;
  33.         cs = c - c % 3;
  34.         for (i = 0; i < 3; i++) {
  35.             for (j = 0; j < 3; j++) {
  36.                 if (grid[rs + i][cs + j] == num) {
  37.                     return false;
  38.                 }
  39.             }
  40.         }
  41.         return true;
  42.     }
  43.  
  44.     boolean solve() {
  45.    //     System.out.println("hello");
  46.         boolean flag;
  47.         flag = false;
  48.         int i, j = 0;
  49.         for (i = 0; i < 9; i++) {
  50.             for (j = 0; j < 9; j++) {
  51.                 if (grid[i][j] == 0) {
  52.                     flag = true;
  53.                     break;
  54.                 }
  55.             }
  56.             if (flag) {
  57.                 break;
  58.             }
  59.         }
  60.         int rval = i;
  61.         int cval = j;
  62.         if (!flag) {
  63.             return true;
  64.         } else {
  65.             for (i = 1; i <= 9; i++) {
  66.                 if (isSafe(i, rval, cval)) {
  67.                     grid[rval][cval] = i;
  68.                     if (solve()) {
  69.                         return true;
  70.                     }
  71.                     grid[rval][cval] = 0;
  72.                 }
  73.             }
  74.         }
  75.         return false;
  76.     }
  77.  
  78.     Suduko_solver() {
  79.         C = this.getContentPane();
  80.         C.setLayout(null);
  81.         f = new Font("Arial", Font.BOLD,25);
  82.         JButton btn = new JButton("Solve");
  83.         btn.setBounds(180, 520, 80, 50);
  84.         C.add(btn);
  85.  
  86. //        JButton btn2 = new JButton("Clear");
  87. //        btn2.setBounds(250, 520, 80, 50);
  88. //        C.add(btn2);
  89.  
  90.         int a, b;
  91.         a = 10;
  92.         b = 60;
  93.         for (int i = 0; i < 9; i++) {
  94.             a = 10;
  95.             for (int j = 0; j < 9; j++) {
  96.                 grid[i][j] = 0;
  97.                 tf[i][j] = new JTextField();
  98.                 tf[i][j].setFont(f);
  99.                 tf[i][j].setHorizontalAlignment(JTextField.CENTER);
  100.                 tf[i][j].setBounds(a, b, 50, 50);
  101.                 C.add(tf[i][j]);
  102.                 a += 50;
  103.             }
  104.             b += 50;
  105.         }
  106.         btn.addActionListener(this);
  107.      //   btn2.addActionListener(this);
  108.  
  109.     }
  110.  
  111.     @Override
  112.     public void actionPerformed(ActionEvent e) {
  113.        
  114.        // System.out.println("hellow...2");
  115.  
  116.       //  if (e.getSource() == btn) {
  117.            
  118.             String str, str2;
  119.             boolean key = true;
  120.  
  121.             for (int i = 0; i < 9; i++) {
  122.  
  123.                 for (int j = 0; j < 9; j++) {
  124.                     str = tf[i][j].getText();
  125.                     if (str.length() > 0) {
  126.                         grid[i][j] = Integer.parseInt(str);
  127.                         if (grid[i][j] > 9 && grid[i][j]<1) {
  128.                             key = false;
  129.                             str2 = "Wrong input in row " + (i + 1) + " colom " + (j + 1);
  130.                             JOptionPane.showMessageDialog(null, str2);
  131.                         }
  132.                     }
  133.                 }
  134.  
  135.             }
  136.  
  137.             if (solve() && key) {
  138.                 for (int i = 0; i < 9; i++) {
  139.  
  140.                     for (int j = 0; j < 9; j++) {
  141.                         str = "" + grid[i][j];
  142.                         tf[i][j].setText(str);
  143.                     }
  144.  
  145.                 }
  146.             } else {
  147.                 JOptionPane.showMessageDialog(null, "No solution exists for this input");
  148.  
  149.             }
  150. //        }
  151. //else {
  152. //
  153. //            Suduko_solver frm = new Suduko_solver();
  154. //            frm.setVisible(true);
  155. //            frm.setBounds(100, 100, 480, 600);
  156. //            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  157. //            frm.setResizable(false);
  158. //        }
  159.  
  160.     }
  161.  
  162.     public static void main(String[] args) {
  163.         Suduko_solver frm = new Suduko_solver();
  164.         frm.setVisible(true);
  165.         frm.setBounds(100, 100, 480, 600);
  166.         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  167.         frm.setResizable(false);
  168.     }
  169.  
  170. }
Add Comment
Please, Sign In to add comment