ghorardim

Suduko solver 2

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