Sunday, April 17, 2016

Sudoku Puzzle Generator

This is just a simple program to generate sudoku puzzle. It demonstrates the use of JTable class. The program ensures that there are no numbers duplicated in the same row, column and 3x3 box. At a minimum, 17 clues will always be generated.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Sudoku extends JFrame{
    private JPanel pmain=null;
    private JTable table=null;
    private int boxcoord[][][]=null;
    private int boxdata[][][]=null;
    
    public Sudoku(){
        super("Sudoku");
        setSize(700,700);
        setLayout(new BorderLayout());
        
        pmain=new JPanel();
        pmain.setPreferredSize(new Dimension(600,600));
        
        table=new JTable(9,9);
        table.setRowHeight(600/9);
        pmain.add(table);
        add("Center",pmain);
        
        setNumbers();
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    private void setNumbers(){
        
        boxcoord=new int[][][]
        {{{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}},
         {{0,3},{0,4},{0,5},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5}},
         {{0,6},{0,7},{0,8},{1,6},{1,7},{1,8},{2,6},{2,7},{2,8}},
         {{3,0},{3,1},{3,2},{4,0},{4,1},{4,2},{5,0},{5,1},{5,2}},
         {{3,3},{3,4},{3,5},{4,3},{4,4},{4,5},{5,3},{5,4},{5,5}},
         {{3,6},{3,7},{3,8},{4,6},{4,7},{4,8},{5,6},{5,7},{5,8}},
         {{6,0},{6,1},{6,2},{7,0},{7,1},{7,2},{8,0},{8,1},{8,2}},
         {{6,3},{6,4},{6,5},{7,3},{7,4},{7,5},{8,3},{8,4},{8,5}},
         {{6,6},{6,7},{6,8},{7,6},{7,7},{7,8},{8,6},{8,7},{8,8}}
        };
        
        boxdata=new int[][][]
        {{{1},{2},{3},{4},{5},{6},{7},{8},{9}},
         {{4},{5},{6},{7},{8},{9},{1},{2},{3}},
         {{7},{8},{9},{1},{2},{3},{4},{5},{6}},
         {{2},{3},{4},{5},{6},{7},{8},{9},{1}},
         {{5},{6},{7},{8},{9},{1},{2},{3},{4}},
         {{8},{9},{1},{2},{3},{4},{5},{6},{7}},
         {{3},{4},{5},{6},{7},{8},{9},{1},{2}},
         {{6},{7},{8},{9},{1},{2},{3},{4},{5}},
         {{9},{1},{2},{3},{4},{5},{6},{7},{8}}
        };            
        
        DefaultTableCellRenderer centerRenderer=new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        for (int i=0; i<table.getColumnCount();i++){
            table.setDefaultRenderer(table.getColumnClass(i),centerRenderer);
        }
        table.setFont(new Font("Serif",Font.BOLD,20));
        table.updateUI();
        
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                table.setValueAt(boxdata[i][j][0],boxcoord[i][j][0],boxcoord[i][j][1]);
            }
        }
        
        for(int spin=0;spin<100;spin++){
            changeRow();
            changeCol();
        }
        
        int num=81;
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                Random rand=new Random();
                int appear=rand.nextInt(2);
                if(appear==0){
                    num=num-1;
                    if(num>=17){
                        table.setValueAt("",boxcoord[i][j][0],boxcoord[i][j][1]);
                    }
                }
            }
        }        
    }
    
    void changeRow(){
        Random rand=new Random();
        int x1=rand.nextInt(9);
        int x2=rand.nextInt(9);
        System.out.println("x1="+x1);
        System.out.println("x2="+x2);
        
        int rowdata1[]=new int[9];
        int rowdata2[]=new int[9];
        
        for(int j=0;j<9;j++){
            rowdata1[j]=(int)table.getValueAt(x1,j);
            rowdata2[j]=(int)table.getValueAt(x2,j);
        }
        
        for(int j=0;j<9;j++){
            table.setValueAt(rowdata2[j],x1,j);
            table.setValueAt(rowdata1[j],x2,j);
        }
        
        
        if(!validcheck()){
            for(int j=0;j<9;j++){
                table.setValueAt(rowdata1[j],x1,j);
                table.setValueAt(rowdata2[j],x2,j);
            }            
        }       
    }
    
    void changeCol(){
        Random rand=new Random();
        int y1=rand.nextInt(9);
        int y2=rand.nextInt(9);
        System.out.println("y1="+y1);
        System.out.println("y2="+y2);
        
        int coldata1[]=new int[9];
        int coldata2[]=new int[9];
        
        for(int i=0;i<9;i++){
            coldata1[i]=(int)table.getValueAt(i,y1);
            coldata2[i]=(int)table.getValueAt(i,y2);
        }
        
        for(int i=0;i<9;i++){
            table.setValueAt(coldata2[i],i,y1);
            table.setValueAt(coldata1[i],i,y2);
        }
        
        
        if(!validcheck()){
            for(int i=0;i<9;i++){
                table.setValueAt(coldata1[i],i,y1);
                table.setValueAt(coldata2[i],i,y2);
            }            
        }       
    }    
    
    boolean validcheck(){
        boolean curval=true;
        
        for(int i=0;i<9;i++){
            int total=0;
            for(int j=0;j<9;j++){
                total=total+(int)table.getValueAt(i,j);
            }
            if(total!=45){
                curval=false;
                break;
            }
        }
        
        for(int i=0;i<9;i++){
            int total=0;
            for(int j=0;j<9;j++){
                total=total+(int)table.getValueAt(j,i);
            }
            if(total!=45){
                curval=false;
                break;
            }
        }        
        
        for(int i=0;i<9;i++){
            int total=0;
            for(int j=0;j<9;j++){
                total=total+(int)table.getValueAt(boxcoord[i][j][0],boxcoord[i][j][1]);
                //System.out.print(table.getValueAt(boxcoord[i][j][0],boxcoord[i][j][1])+" ");
            }
            if(total!=45){
                curval=false;
                break;
            }            
            //System.out.println("");
        }
        
        System.out.println(curval);
        return curval;
    }
    
    public static void main(String[] args){
        new Sudoku();
    }
}

No comments:

Post a Comment