Guest User

Untitled

a guest
Apr 23rd, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. package sudoku;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6.  
  7. import javax.swing.*;
  8. import javax.swing.table.DefaultTableModel;
  9. import javax.swing.table.TableModel;
  10.  
  11. public class GUI extends JFrame{
  12.  
  13.     private static final long serialVersionUID = 50551516923204139L;
  14.     static String[] colomn = {"kol_1","kol_2","kol_3","kol_4","kol_5","kol_6","kol_7","kol_8","kol_9"};
  15.     JButton check= new JButton("Pārbaudīt");
  16.     JButton new_field= new JButton("Jauna spēle");
  17.     JButton give= new JButton("Padodos");
  18.     JPanel panel= new JPanel();
  19.     static int[][] test= sudoku1.set();
  20.     static Object[][] data = {  ///static mainja. check it.
  21.             {0,0,0,0,0,0,0,0,0},
  22.             {0,0,0,0,0,0,0,0,0},
  23.             {0,0,0,0,0,0,0,0,0},
  24.             {0,0,0,0,0,0,0,0,0},
  25.             {0,0,0,0,0,0,0,0,0},
  26.             {0,0,0,0,0,0,0,0,0},
  27.             {0,0,0,0,0,0,0,0,0},
  28.             {0,0,0,0,0,0,0,0,0},
  29.             {0,0,0,0,0,0,0,0,0}};
  30.     DefaultTableModel tableModel = new DefaultTableModel((Object[][]) set_data(data,test), colomn);
  31.     JTable table= new JTable(tableModel);
  32.    
  33.     public static Object set_data(Object[][] data, int[][] test)
  34.     {
  35.         sudoku1.remove(test);
  36.         for(int i=0;i<9;i++)
  37.         {
  38.             for(int j=0;j<9;j++)
  39.             {
  40.                 if(test[i][j]==0)
  41.                 {
  42.                     data[i][j]=null;
  43.                 } else
  44.                 {
  45.                     data[i][j]=test[i][j];
  46.                 }
  47.             }
  48.         }
  49.        
  50.         return data;
  51.     }
  52.    
  53.     public GUI()
  54.     {
  55.            
  56.                 panel.add(table);
  57.                 panel.add(check);
  58.                 panel.add(new_field);
  59.                 panel.add(give);
  60.        
  61.         panel.setLayout(null);
  62.         table.setTableHeader(null);
  63.        
  64.         table.setRowHeight(30);
  65.         table.setBounds(100, 50, 270, 270);
  66.         check.setBounds(370, 50, 100,30);
  67.         give.setBounds(370,150,100,30);
  68.         new_field.setBounds(370,100,200,30);
  69.  
  70.         add(panel);
  71.        
  72.         new_field.addActionListener(new ActionListener()
  73.         {
  74.  
  75.             @Override
  76.             public void actionPerformed(ActionEvent e)
  77.             {
  78.                 // http://stackoverflow.com/questions/8020102/update-jtable-content-based-on-actionperformed
  79.                 table.setModel(newTable());
  80.                
  81.            
  82.             }
  83.            
  84.         });
  85.  
  86.        
  87.         check.addActionListener(new ActionListener()
  88.         {
  89.  
  90.             @Override
  91.             public void actionPerformed(ActionEvent e) {
  92.                 if(check(table)==true)
  93.                 {
  94.                     JOptionPane.showMessageDialog(null, "Pareizi!");
  95.                 } else
  96.                
  97.                 {
  98.                     JOptionPane.showMessageDialog(null, "Nav pareizi!");
  99.                 }
  100.                
  101.             }
  102.            
  103.         });
  104.        
  105.         give.addActionListener(new ActionListener(){
  106.  
  107.             public void actionPerformed(ActionEvent arg0)
  108.             {
  109.                 table.setModel(give());
  110.                
  111.             }
  112.            
  113.         });
  114.  
  115.        
  116.        
  117.     }
  118.    
  119.     public static void main(String args[])
  120.     {
  121.         GUI gui= new GUI();
  122.         gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  123.         gui.setSize(600,500);
  124.         gui.setVisible(true);
  125.         gui.setTitle("Sudoku. Austris Baumanis");
  126.        
  127.     }  
  128.    
  129.     public static TableModel newTable()
  130.     {
  131.         test=sudoku1.set();
  132.          set_data(data, test);
  133.          return new DefaultTableModel(data, colomn);
  134.        
  135.     }
  136.    
  137.     public static TableModel give()
  138.     {
  139.         sudoku1.Solve(test);
  140.         for(int i=0;i<9;i++)
  141.         {
  142.             for(int j=0;j<9;j++)
  143.             {
  144.                 data[i][j]=test[i][j];
  145.             }
  146.         }
  147.        
  148.         return new DefaultTableModel(data, colomn);
  149.     }
  150.    
  151.     static boolean check(JTable table)
  152.     {
  153.        
  154.          DefaultTableModel dtm = (DefaultTableModel) table.getModel();
  155.          int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
  156.          Object[][] tableData = new Object[nRow][nCol];
  157.          
  158.          for (int i = 0 ; i < nRow ; i++)
  159.          {
  160.              for (int j = 0 ; j < nCol ; j++)
  161.              {
  162.                  if(dtm.getValueAt(i, j) instanceof Integer)
  163.                  {
  164.                      if((int)dtm.getValueAt(i, j)>0||(int)dtm.getValueAt(i, j)<10)
  165.                      {
  166.                          tableData[i][j]= dtm.getValueAt(i, j);
  167.                      }
  168.                  } else
  169.                      if(dtm.getValueAt(i, j) instanceof String)
  170.                              {
  171.                                     if(dtm.getValueAt(i, j).toString().length()==0||dtm.getValueAt(i, j).toString().length()>1)
  172.                                     {
  173.                                         tableData[i][j]= 0;
  174.                                     }
  175.                                     else
  176.                                     {   if(!dtm.getValueAt(i, j).toString().matches("[a-zA-Z\\S+]"))
  177.                                         {
  178.                                             tableData[i][j] = Integer.valueOf((String) dtm.getValueAt(i,j));
  179.                                         } else
  180.                                         {
  181.                                             tableData[i][j]= 0; //tests .+\\D.+
  182.                                         }
  183.                                     }
  184.                              } else
  185.                              {
  186.                                  tableData[i][j]= 0;
  187.                              }
  188.                
  189.              }
  190.          }
  191.          
  192.         boolean tester= true;
  193.         for(int i=0;i<9;i++)
  194.         {
  195.             boolean[] checker= {false, false, false, false, false, false, false, false, false};
  196.             for(int j=0; j<9;j++)
  197.             {   if(tableData[i][j]!=null)
  198.                 {
  199.                     if((int) tableData[i][j]!=0)
  200.                     {
  201.                         int value=(int) tableData[i][j];
  202.                         checker[value-1]=true;
  203.                     }
  204.                 }
  205.             }
  206.            
  207.             for(int j=0; j<9;j++)
  208.             {
  209.                 if(checker[j]!=true)
  210.                 {
  211.                     tester= false;
  212.                 }
  213.             }
  214.            
  215.             for (int j=0; j<9;j++)
  216.             {
  217.                 checker[j]=false;
  218.             }
  219.            
  220.             for(int j=0; j<9;j++)
  221.             {
  222.                 if(tableData[j][i]!=null)
  223.                 {
  224.                     if((int) tableData[j][i]!=0)
  225.                     {
  226.                         int value= (int) tableData[j][i];
  227.                         checker[value-1]=true;
  228.                     }
  229.                 }
  230.             }
  231.            
  232.             for(int j=0; j<9;j++)
  233.             {
  234.                 if(checker[j]!=true)
  235.                 {
  236.                     tester= false;
  237.                 }
  238.             }
  239.            
  240.            
  241.         }
  242.        
  243.         return tester;
  244.     }
  245.    
  246.  
  247.    
  248.  
  249.    
  250.    
  251. }
Advertisement
Add Comment
Please, Sign In to add comment