Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sudoku;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableModel;
- public class GUI extends JFrame{
- private static final long serialVersionUID = 50551516923204139L;
- static String[] colomn = {"kol_1","kol_2","kol_3","kol_4","kol_5","kol_6","kol_7","kol_8","kol_9"};
- JButton check= new JButton("Pārbaudīt");
- JButton new_field= new JButton("Jauna spēle");
- JButton give= new JButton("Padodos");
- JPanel panel= new JPanel();
- static int[][] test= sudoku1.set();
- static Object[][] data = { ///static mainja. check it.
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0}};
- DefaultTableModel tableModel = new DefaultTableModel((Object[][]) set_data(data,test), colomn);
- JTable table= new JTable(tableModel);
- public static Object set_data(Object[][] data, int[][] test)
- {
- sudoku1.remove(test);
- for(int i=0;i<9;i++)
- {
- for(int j=0;j<9;j++)
- {
- if(test[i][j]==0)
- {
- data[i][j]=null;
- } else
- {
- data[i][j]=test[i][j];
- }
- }
- }
- return data;
- }
- public GUI()
- {
- panel.add(table);
- panel.add(check);
- panel.add(new_field);
- panel.add(give);
- panel.setLayout(null);
- table.setTableHeader(null);
- table.setRowHeight(30);
- table.setBounds(100, 50, 270, 270);
- check.setBounds(370, 50, 100,30);
- give.setBounds(370,150,100,30);
- new_field.setBounds(370,100,200,30);
- add(panel);
- new_field.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- // http://stackoverflow.com/questions/8020102/update-jtable-content-based-on-actionperformed
- table.setModel(newTable());
- }
- });
- check.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e) {
- if(check(table)==true)
- {
- JOptionPane.showMessageDialog(null, "Pareizi!");
- } else
- {
- JOptionPane.showMessageDialog(null, "Nav pareizi!");
- }
- }
- });
- give.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0)
- {
- table.setModel(give());
- }
- });
- }
- public static void main(String args[])
- {
- GUI gui= new GUI();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- gui.setSize(600,500);
- gui.setVisible(true);
- gui.setTitle("Sudoku. Austris Baumanis");
- }
- public static TableModel newTable()
- {
- test=sudoku1.set();
- set_data(data, test);
- return new DefaultTableModel(data, colomn);
- }
- public static TableModel give()
- {
- sudoku1.Solve(test);
- for(int i=0;i<9;i++)
- {
- for(int j=0;j<9;j++)
- {
- data[i][j]=test[i][j];
- }
- }
- return new DefaultTableModel(data, colomn);
- }
- static boolean check(JTable table)
- {
- DefaultTableModel dtm = (DefaultTableModel) table.getModel();
- int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
- Object[][] tableData = new Object[nRow][nCol];
- for (int i = 0 ; i < nRow ; i++)
- {
- for (int j = 0 ; j < nCol ; j++)
- {
- if(dtm.getValueAt(i, j) instanceof Integer)
- {
- if((int)dtm.getValueAt(i, j)>0||(int)dtm.getValueAt(i, j)<10)
- {
- tableData[i][j]= dtm.getValueAt(i, j);
- }
- } else
- if(dtm.getValueAt(i, j) instanceof String)
- {
- if(dtm.getValueAt(i, j).toString().length()==0||dtm.getValueAt(i, j).toString().length()>1)
- {
- tableData[i][j]= 0;
- }
- else
- { if(!dtm.getValueAt(i, j).toString().matches("[a-zA-Z\\S+]"))
- {
- tableData[i][j] = Integer.valueOf((String) dtm.getValueAt(i,j));
- } else
- {
- tableData[i][j]= 0; //tests .+\\D.+
- }
- }
- } else
- {
- tableData[i][j]= 0;
- }
- }
- }
- boolean tester= true;
- for(int i=0;i<9;i++)
- {
- boolean[] checker= {false, false, false, false, false, false, false, false, false};
- for(int j=0; j<9;j++)
- { if(tableData[i][j]!=null)
- {
- if((int) tableData[i][j]!=0)
- {
- int value=(int) tableData[i][j];
- checker[value-1]=true;
- }
- }
- }
- for(int j=0; j<9;j++)
- {
- if(checker[j]!=true)
- {
- tester= false;
- }
- }
- for (int j=0; j<9;j++)
- {
- checker[j]=false;
- }
- for(int j=0; j<9;j++)
- {
- if(tableData[j][i]!=null)
- {
- if((int) tableData[j][i]!=0)
- {
- int value= (int) tableData[j][i];
- checker[value-1]=true;
- }
- }
- }
- for(int j=0; j<9;j++)
- {
- if(checker[j]!=true)
- {
- tester= false;
- }
- }
- }
- return tester;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment