Advertisement
balispy

Sudoku help

Mar 24th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.04 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.*;
  4.  
  5. public class Panel extends JPanel {
  6.    
  7.     public int x0y0value = 0;
  8.     public int x0y1value = 0;
  9.     public int x0y2value = 0;
  10.     public int x0y3value = 0;
  11.     public int x0y4value = 0;
  12.     public int x0y5value = 0;
  13.     public int x0y6value = 0;
  14.     public int x0y7value = 0;
  15.     public int x0y8value = 0;
  16.     public int x1y0value = 0;
  17.     public int x1y1value = 0;
  18.     public int x1y2value = 0;
  19.     public int x1y3value = 0;
  20.     public int x1y4value = 0;
  21.     public int x1y5value = 0;
  22.     public int x1y6value = 0;
  23.     public int x1y7value = 0;
  24.     public int x1y8value = 0;
  25.     public int x2y0value = 0;
  26.     public int x2y1value = 0;
  27.     public int x2y2value = 0;
  28.     public int x2y3value = 0;
  29.     public int x2y4value = 0;
  30.     public int x2y5value = 0;
  31.     public int x2y6value = 0;
  32.     public int x2y7value = 0;
  33.     public int x2y8value = 0;
  34.     public int x3y0value = 0;
  35.     public int x3y1value = 0;
  36.     public int x3y2value = 0;
  37.     public int x3y3value = 0;
  38.     public int x3y4value = 0;
  39.     public int x3y5value = 0;
  40.     public int x3y6value = 0;
  41.     public int x3y7value = 0;
  42.     public int x3y8value = 0;
  43.     public int x4y0value = 0;
  44.     public int x4y1value = 0;
  45.     public int x4y2value = 0;
  46.     public int x4y3value = 0;
  47.     public int x4y4value = 0;
  48.     public int x4y5value = 0;
  49.     public int x4y6value = 0;
  50.     public int x4y7value = 0;
  51.     public int x4y8value = 0;
  52.     public int x5y0value = 0;
  53.     public int x5y1value = 0;
  54.     public int x5y2value = 0;
  55.     public int x5y3value = 0;
  56.     public int x5y4value = 0;
  57.     public int x5y5value = 0;
  58.     public int x5y6value = 0;
  59.     public int x5y7value = 0;
  60.     public int x5y8value = 0;
  61.     public int x6y0value = 0;
  62.     public int x6y1value = 0;
  63.     public int x6y2value = 0;
  64.     public int x6y3value = 0;
  65.     public int x6y4value = 0;
  66.     public int x6y5value = 0;
  67.     public int x6y6value = 0;
  68.     public int x6y7value = 0;
  69.     public int x6y8value = 0;
  70.     public int x7y0value = 0;
  71.     public int x7y1value = 0;
  72.     public int x7y2value = 0;
  73.     public int x7y3value = 0;
  74.     public int x7y4value = 0;
  75.     public int x7y5value = 0;
  76.     public int x7y6value = 0;
  77.     public int x7y7value = 0;
  78.     public int x7y8value = 0;
  79.     public int x8y0value = 0;
  80.     public int x8y1value = 0;
  81.     public int x8y2value = 0;
  82.     public int x8y3value = 0;
  83.     public int x8y4value = 0;
  84.     public int x8y5value = 0;
  85.     public int x8y6value = 0;
  86.     public int x8y7value = 0;
  87.     public int x8y8value = 0;
  88.     public int countCount = 0;
  89.    
  90.     JTextField text1 = new JTextField();
  91.     JTextField text2 = new JTextField();
  92.     JTextField text3 = new JTextField();
  93.     JTextField text4 = new JTextField();
  94.     JTextField text5 = new JTextField();
  95.     JTextField text6 = new JTextField();
  96.     JTextField text7 = new JTextField();
  97.     JTextField text8 = new JTextField();
  98.     JTextField text9 = new JTextField();
  99.     JTextField text10 = new JTextField();
  100.     JTextField text11 = new JTextField();
  101.     JTextField text12 = new JTextField();
  102.     JTextField text13 = new JTextField();
  103.     JTextField text14 = new JTextField();
  104.     JTextField text15 = new JTextField();
  105.     JTextField text16 = new JTextField();
  106.     JTextField text17 = new JTextField();
  107.     JTextField text18 = new JTextField();
  108.     JTextField text19 = new JTextField();
  109.     JTextField text20 = new JTextField();
  110.     JTextField text21 = new JTextField();
  111.     JTextField text22 = new JTextField();
  112.     JTextField text23 = new JTextField();
  113.     JTextField text24 = new JTextField();
  114.     JTextField text25 = new JTextField();
  115.    
  116.     JTextField[] text = { text1, text2, text3, text4, text5, text6, text7, text8, text9, text10, text11, text12, text13, text14, text15, text16,
  117.             text17, text18, text19, text20, text21, text22, text23, text24, text25
  118.     };
  119.    
  120.     JLabel x0y0label = new JLabel();
  121.     JLabel x0y1label = new JLabel();
  122.     JLabel x0y2label = new JLabel();
  123.     JLabel x0y3label = new JLabel();
  124.     JLabel x0y4label = new JLabel();
  125.     JLabel x0y5label = new JLabel();
  126.     JLabel x0y6label = new JLabel();
  127.     JLabel x0y7label = new JLabel();
  128.     JLabel x0y8label = new JLabel();
  129.     JLabel x1y0label = new JLabel();
  130.     JLabel x1y1label = new JLabel();
  131.     JLabel x1y2label = new JLabel();
  132.     JLabel x1y3label = new JLabel();
  133.     JLabel x1y4label = new JLabel();
  134.     JLabel x1y5label = new JLabel();
  135.     JLabel x1y6label = new JLabel();
  136.     JLabel x1y7label = new JLabel();
  137.     JLabel x1y8label = new JLabel();
  138.     JLabel x2y0label = new JLabel();
  139.     JLabel x2y1label = new JLabel();
  140.     JLabel x2y2label = new JLabel();
  141.     JLabel x2y3label = new JLabel();
  142.     JLabel x2y4label = new JLabel();
  143.     JLabel x2y5label = new JLabel();
  144.     JLabel x2y6label = new JLabel();
  145.     JLabel x2y7label = new JLabel();
  146.     JLabel x2y8label = new JLabel();
  147.     JLabel x3y0label = new JLabel();
  148.     JLabel x3y1label = new JLabel();
  149.     JLabel x3y2label = new JLabel();
  150.     JLabel x3y3label = new JLabel();
  151.     JLabel x3y4label = new JLabel();
  152.     JLabel x3y5label = new JLabel();
  153.     JLabel x3y6label = new JLabel();
  154.     JLabel x3y7label = new JLabel();
  155.     JLabel x3y8label = new JLabel();
  156.     JLabel x4y0label = new JLabel();
  157.     JLabel x4y1label = new JLabel();
  158.     JLabel x4y2label = new JLabel();
  159.     JLabel x4y3label = new JLabel();
  160.     JLabel x4y4label = new JLabel();
  161.     JLabel x4y5label = new JLabel();
  162.     JLabel x4y6label = new JLabel();
  163.     JLabel x4y7label = new JLabel();
  164.     JLabel x4y8label = new JLabel();
  165.     JLabel x5y0label = new JLabel();
  166.     JLabel x5y1label = new JLabel();
  167.     JLabel x5y2label = new JLabel();
  168.     JLabel x5y3label = new JLabel();
  169.     JLabel x5y4label = new JLabel();
  170.     JLabel x5y5label = new JLabel();
  171.     JLabel x5y6label = new JLabel();
  172.     JLabel x5y7label = new JLabel();
  173.     JLabel x5y8label = new JLabel();
  174.     JLabel x6y0label = new JLabel();
  175.     JLabel x6y1label = new JLabel();
  176.     JLabel x6y2label = new JLabel();
  177.     JLabel x6y3label = new JLabel();
  178.     JLabel x6y4label = new JLabel();
  179.     JLabel x6y5label = new JLabel();
  180.     JLabel x6y6label = new JLabel();
  181.     JLabel x6y7label = new JLabel();
  182.     JLabel x6y8label = new JLabel();
  183.     JLabel x7y0label = new JLabel();
  184.     JLabel x7y1label = new JLabel();
  185.     JLabel x7y2label = new JLabel();
  186.     JLabel x7y3label = new JLabel();
  187.     JLabel x7y4label = new JLabel();
  188.     JLabel x7y5label = new JLabel();
  189.     JLabel x7y6label = new JLabel();
  190.     JLabel x7y7label = new JLabel();
  191.     JLabel x7y8label = new JLabel();
  192.     JLabel x8y0label = new JLabel();
  193.     JLabel x8y1label = new JLabel();
  194.     JLabel x8y2label = new JLabel();
  195.     JLabel x8y3label = new JLabel();
  196.     JLabel x8y4label = new JLabel();
  197.     JLabel x8y5label = new JLabel();
  198.     JLabel x8y6label = new JLabel();
  199.     JLabel x8y7label = new JLabel();
  200.     JLabel x8y8label = new JLabel();
  201.    
  202.     JLabel[] x0label = { x0y0label, x0y1label, x0y2label, x0y3label, x0y4label, x0y5label, x0y6label, x0y7label, x0y8label };
  203.    
  204.     JLabel[] x1label = { x1y0label, x1y1label, x1y2label, x1y3label, x1y4label, x1y5label, x1y6label, x1y7label, x1y8label };
  205.    
  206.     JLabel[] x2label = { x2y0label, x2y1label, x2y2label, x2y3label, x2y4label, x2y5label, x2y6label, x2y7label, x2y8label };
  207.    
  208.     JLabel[] x3label = { x3y0label, x3y1label, x3y2label, x3y3label, x3y4label, x3y5label, x3y6label, x3y7label, x3y8label };
  209.    
  210.     JLabel[] x4label = { x4y0label, x4y1label, x4y2label, x4y3label, x4y4label, x4y5label, x4y6label, x4y7label, x4y8label };
  211.    
  212.     JLabel[] x5label = { x5y0label, x5y1label, x5y2label, x5y3label, x5y4label, x5y5label, x5y6label, x5y7label, x5y8label };
  213.    
  214.     JLabel[] x6label = { x6y0label, x6y1label, x6y2label, x6y3label, x6y4label, x6y5label, x6y6label, x6y7label, x6y8label };
  215.    
  216.     JLabel[] x7label = { x7y0label, x7y1label, x7y2label, x7y3label, x7y4label, x7y5label, x7y6label, x7y7label, x7y8label };
  217.    
  218.     JLabel[] x8label = { x8y0label, x8y1label, x8y2label, x8y3label, x8y4label, x8y5label, x8y6label, x8y7label, x8y8label };
  219.    
  220.     JLabel[][] label = { x0label, x1label, x2label, x3label, x4label, x5label, x6label, x7label, x8label };
  221.    
  222. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223.    
  224.     int[] x0value = { x0y0value, x0y1value, x0y2value, x0y3value, x0y4value, x0y5value, x0y6value, x0y7value, x0y8value };
  225.    
  226.     int[] x1value = { x1y0value, x1y1value, x1y2value, x1y3value, x1y4value, x1y5value, x1y6value, x1y7value, x1y8value };
  227.    
  228.     int[] x2value = { x2y0value, x2y1value, x2y2value, x2y3value, x2y4value, x2y5value, x2y6value, x2y7value, x2y8value };
  229.    
  230.     int[] x3value = { x3y0value, x3y1value, x3y2value, x3y3value, x3y4value, x3y5value, x3y6value, x3y7value, x3y8value };
  231.    
  232.     int[] x4value = { x4y0value, x4y1value, x4y2value, x4y3value, x4y4value, x4y5value, x4y6value, x4y7value, x4y8value };
  233.    
  234.     int[] x5value = { x5y0value, x5y1value, x5y2value, x5y3value, x5y4value, x5y5value, x5y6value, x5y7value, x5y8value };
  235.    
  236.     int[] x6value = { x6y0value, x6y1value, x6y2value, x6y3value, x6y4value, x6y5value, x6y6value, x6y7value, x6y8value };
  237.    
  238.     int[] x7value = { x7y0value, x7y1value, x7y2value, x7y3value, x7y4value, x7y5value, x7y6value, x7y7value, x7y8value };
  239.    
  240.     int[] x8value = { x8y0value, x8y1value, x8y2value, x8y3value, x8y4value, x8y5value, x8y6value, x8y7value, x8y8value };
  241.    
  242.     int[][] xvalue = { x0value, x1value, x2value, x3value, x4value, x5value, x6value, x7value, x8value };
  243.    
  244. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  245.    
  246.     int[] y0value = { x0y0value, x1y0value, x2y0value, x3y0value, x4y0value, x5y0value, x6y0value, x7y0value, x8y0value };
  247.    
  248.     int[] y1value = { x0y1value, x1y1value, x2y1value, x3y1value, x4y1value, x5y1value, x6y1value, x7y1value, x8y1value };
  249.    
  250.     int[] y2value = { x0y2value, x1y2value, x2y2value, x3y2value, x4y2value, x5y2value, x6y2value, x7y2value, x8y2value };
  251.    
  252.     int[] y3value = { x0y3value, x1y3value, x2y3value, x3y3value, x4y3value, x5y3value, x6y3value, x7y3value, x8y3value };
  253.    
  254.     int[] y4value = { x0y4value, x1y4value, x2y4value, x3y4value, x4y4value, x5y4value, x6y4value, x7y4value, x8y4value };
  255.    
  256.     int[] y5value = { x0y5value, x1y5value, x2y5value, x3y5value, x4y5value, x5y5value, x6y5value, x7y5value, x8y5value };
  257.    
  258.     int[] y6value = { x0y6value, x1y6value, x2y6value, x3y6value, x4y6value, x5y6value, x6y6value, x7y6value, x8y6value };
  259.    
  260.     int[] y7value = { x0y7value, x1y7value, x2y7value, x3y7value, x4y7value, x5y7value, x6y7value, x7y7value, x8y7value };
  261.    
  262.     int[] y8value = { x0y8value, x1y8value, x2y8value, x3y8value, x4y8value, x5y8value, x6y8value, x7y8value, x8y8value };
  263.    
  264.     int[][] yvalue = { y0value, y1value, y2value, y3value, y4value, y5value, y6value, y7value, y8value };
  265.    
  266. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  267.    
  268.     int count1 = 0;
  269.     int count2 = 0;
  270.     int count3 = 0;
  271.     int count4 = 0;
  272.     int count5 = 0;
  273.     int count6 = 0;
  274.     int count7 = 0;
  275.     int count8 = 0;
  276.     int count9 = 0;
  277.    
  278.     int[] countArray = { 0, count1, count2, count3, count4, count5, count6, count7, count8, count9 };
  279.    
  280. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  281.    
  282.     Random random = new Random();
  283.    
  284.     public int numOfText = random.nextInt(24) + 8;
  285.    
  286.     public Panel() {       
  287.        
  288.         if (numOfText >= 25) {
  289.             numOfText = numOfText - 8;
  290.         }
  291.        
  292.         System.out.println(numOfText);
  293.        
  294.         Dimension size = getPreferredSize();
  295.         size.width = 500;
  296.         size.height = 500;
  297.         setPreferredSize(size);
  298.        
  299.         setLayout(new GridBagLayout());
  300.         GridBagConstraints gc = new GridBagConstraints();
  301.        
  302.         for (int i = 0; i < 9; i++) {
  303.             for (int i2 = 0; i2 < 9; i2++) {
  304.                 gc.anchor = GridBagConstraints.CENTER;
  305.                 gc.weightx = 0.15;
  306.                 gc.weighty = 0.15;
  307.                 gc.gridx = i;
  308.                 gc.gridy = i2;
  309.                 add(label[i][i2], gc);
  310.             }
  311.         }
  312.        
  313.         for (int i = 0; i < 9; i++) {
  314.             for (int i2 = 0; i2 < 9; i2++) {
  315.                 label[i][i2].setText("" +xvalue[i][i2]);
  316.             }
  317.         }
  318.        
  319.         for (int i = 0; i <= numOfText;) {
  320.             gc.anchor = GridBagConstraints.CENTER;
  321.             gc.weightx = 0.15;
  322.             gc.weighty = 0.15;
  323.             int randomx = random.nextInt(8);
  324.             int randomy = random.nextInt(8);
  325.             gc.gridx = randomx;
  326.             gc.gridy = randomy;
  327.            
  328.             if (xvalue[randomx][randomy] < 1) {
  329. //              remove(label[randomx][randomy]);
  330.                 add(text[i], gc);
  331.                 text[i].setPreferredSize(new Dimension(25, 25));
  332.                 text[i].setHorizontalAlignment(JTextField.CENTER);
  333.                 text[i].setEditable(true);
  334.                 text[i].setVisible(true);
  335.                 text[i].setColumns(0);
  336.                 revalidate();
  337.                 repaint();
  338.                 xvalue[randomx][randomy]++;
  339.                 i++;
  340.             }
  341.         }
  342.         boolean good1 = false;
  343.         boolean good2 = false;
  344.         boolean restart = true;
  345.  
  346.         for (int i = 0; i <= 8; i++) {
  347.             for (int i2 = 0; i2 <= 8;) {
  348.                 int temp = random.nextInt(9) + 1;
  349.                 if (temp == 10) {
  350.                     temp = temp - 1;
  351.                 }
  352.                 int temp2 = 0;
  353.                
  354.                 good1 = false;
  355.                 good2 = false;
  356.                
  357. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  358.                
  359.                 for (int o = 0; o < countArray.length; o++) {
  360.                     countArray[o] = 0;
  361.                 }
  362.                
  363.                 countArray[temp]++;
  364.                
  365.                 for (int g = 0; g <= 8; g++) {
  366.                     temp2 = yvalue[i][g];
  367.                     countArray[temp2]++;
  368.                 }
  369.                
  370.                 if (countArray[0] < 2 && countArray[1] < 2 && countArray[2] < 2 && countArray[3] < 2 && countArray[4] < 2 && countArray[5] < 2
  371.                             && countArray[6] < 2 && countArray[7] < 2 && countArray[8] < 2) {
  372.                     good1 = true;
  373.                 }
  374.  
  375. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  376.                
  377.                 for (int o = 0; o < countArray.length; o++) {
  378.                     countArray[o] = 0;
  379.                 }
  380.                
  381.                 countArray[temp]++;
  382.                
  383.                 for (int g = 0; g <= 8; g++) {
  384.                     temp2 = xvalue[i][g];
  385.                     countArray[temp2]++;
  386.                 }
  387.                
  388.                 if (countArray[0] < 2 && countArray[1] < 2 && countArray[2] < 2 && countArray[3] < 2 && countArray[4] < 2 && countArray[5] < 2
  389.                         && countArray[6] < 2 && countArray[7] < 2 && countArray[8] < 2) {
  390.                 good2 = true;
  391.                 }
  392.                
  393. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  394.  
  395.                 if (good1 == true && good2 == true) {
  396.                     xvalue[i][i2] = temp;
  397.                     yvalue[i2][i] = temp;
  398.                    
  399.                     i2++;
  400.                 }
  401.                
  402.                
  403.                
  404.                 System.out.println(temp);
  405.             }
  406.         }
  407.  
  408.         for (int i = 0; i < 9; i++) {
  409.             for (int i2 = 0; i2 < 9; i2++) {
  410.                 label[i][i2].setText("" +xvalue[i][i2]);
  411.             }
  412.         }
  413.        
  414.         setVisible(true);
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement