ghorardim

Suduko solver 3

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