Java Applet implementation of UI, to solve the sudoku Puzzle.
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
package my.sudoku; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Random; import javax.swing.*; import javax.swing.JOptionPane; /* <applet code="sudoku.class" width=500 height=400> </applet> */ public class Sudoku extends Applet implements ActionListener,TextListener { Random rand=new Random(); TextField [][]t=new TextField[10][10]; Button b,solve,ran,bk,initial,restart; int i,j,count=0; String txt[][]=new String[10][10]; int sel[][]=new int[281][281]; int s[][][]=new int[10][10][10]; int fnd[][]=new int[10][10]; int p[]=new int[10]; int dat; public void init() { setBackground(Color.cyan); Panel pan=new Panel(); Panel bod=new Panel(); pan.setLayout(new GridLayout(9,9)); bod.setLayout(new BorderLayout()); setFont(new Font("SansSerif",Font.BOLD,18)); ran=new Button("Solve"); restart=new Button("Restart"); for( i=1;i<=9;i++) for( j=1;j<=9;j++) { t[i][j]=new TextField(2); pan.add(t[i][j]); t[i][j].setText(""); t[i][j].addTextListener(this); t[i][j].setForeground(Color.red); t[i][j].setBackground(Color.green); } bod.add(ran,BorderLayout.EAST); bod.add(restart,BorderLayout.SOUTH); add(bod); add(pan); restart.addActionListener(this); ran.addActionListener(this); } public void textValueChanged(TextEvent te) { int yh; for(int te1=1;te1<=9;te1++) for(int te2=1;te2<=9;te2++){ if(te.getSource()==t[te1][te2]){ if(spellcheck(t[te1][te2].getText())==1) t[te1][te2].setText(""); singlecheck(te1,te2,t[te1][te2].getText()); } } } public void actionPerformed(ActionEvent e){ if(e.getSource()==restart){ for(int hj=1;hj<=9;hj++) for(int hj1=1;hj1<=9;hj1++){ txt[hj][hj1]=t[hj][hj1].getText(); } for(int hj=1;hj<=9;hj++) for(int hj1=1;hj1<=9;hj1++) { t[hj][hj1].setForeground(Color.red); fnd[hj][hj1]=10; t[hj][hj1].setText(""); } for(int hj=1;hj<=9;hj++) for(int hj1=1;hj1<=9;hj1++) for(int hj2=1;hj2<=9;hj2++) { s[hj][hj1][hj2]=0; } for(int hj=1;hj<=count;hj++) { sel[hj][0]=0;sel[hj][1]=0; sel[hj][2]=0;sel[hj][3]=0; } count=0; } if(e.getSource()==ran) { for(int hy1=1;hy1<=9;hy1++) for(int hy2=1;hy2<=9;hy2++) { txt[hy1][hy2]=t[hy1][hy2].getText(); } int cjk=0; mainexe(); Muru: { while(!finish()) { for(int hy1=1;hy1<=9;hy1++) for(int hy2=1;hy2<=9;hy2++) { t[hy1][hy2].setText(""+txt[hy1][hy2]); } mainexe(); cjk++; if(finish())break Muru; if(cjk>100) {msg bx=new msg(); break Muru;} } } } } public int[] getautocalculate(int i,int j) { int []auto=new int[10]; for(int h2=1;h2<=9;h2++) { if(s[i][h2][0]!=0 || s[h2][j][0]!=0) { auto[s[i][h2][0]]=s[i][h2][0]; auto[s[h2][j][0]]=s[h2][j][0]; } } int x1=0,y1=0; if(i<=3)x1=1;if(j<=3)y1=1; if(i>3 && i<=6)x1=4;if(j>3 && j<=6)y1=4; if(i>=7)x1=7;if(j>=7)y1=7; for(int i3=x1;i3<=x1+2;i3++) { for(int j3=y1;j3<=y1+2;j3++) { auto[s[i3][j3][0]]=s[i3][j3][0]; } } for(int h2=1;h2<=9;h2++) { if(auto[h2]==0) { auto[h2]=h2; } else auto[h2]=0; } return auto; } public int lengthofac(int k1,int k2) { int len=0; for(int u1=1;u1<=9;u1++) { if(s[k1][k2][u1]!=0)len++; } return len; } public boolean minimum(int c1,int c2) { for(int ctd1=1;ctd1<=9;ctd1++) { for(int ctd2=1;ctd2<=9;ctd2++) { if(s[ctd1][ctd2][0]==0) if((lengthofac(c1,c2))>(lengthofac(ctd1,ctd2))) {return false;} } } return true; } public int getrnumber(int k1,int k2) { int num=0,r; for(int u1=1;u1<=9;u1++) { if((s[k1][k2][u1]!=0) && (check(k1,k2,s[k1][k2][u1])==0)) { num= s[k1][k2][u1]; r=(int)(2*rand.nextDouble()); if(r==0)return num; //m[k1][k2]=1; } } return num; } public int singlecheck(int x,int y,String number) { if(!number.equals("")) { for(int k=1;k<=9;k++) if(k!=x) if((t[k][y].getText()).equals(number)) { t[x][y].setText(""); t[x][y].setForeground(Color.red); return 1; } for(int k=1;k<=9;k++) if(k!=y) if((t[x][k].getText()).equals(number)) {t[x][y].setText(""); return 1; } int x1=0,y1=0; if(x<=3)x1=1;if(y<=3)y1=1; if(x>3 && x<=6)x1=4;if(y>3 && y<=6)y1=4; if(x>=7)x1=7;if(y>=7)y1=7; for(int i3=x1;i3<=x1+2;i3++) { for(int j3=y1;j3<=y1+2;j3++) { if((i3!=x) && (j3!=y)) if((t[i3][j3].getText()).equals(number)) { t[x][y].setText(""); return 1; } } } } return 0; } public int spellcheck(String s) { if(s.equals("1") ||s.equals("2")) return 0; else if(s.equals("3") ||s.equals("4") ||s.equals("6")) return 0; else if(s.equals("5") ||s.equals("7") ||s.equals("8") ||s.equals("9")) return 0; else return 1; } public int check(int x,int y,int number) { //Check X area for(int k=1;k<x;k++) if(s[k][y][0]==number) return 1; //Check Y area for(int k=1;k<y;k++) if(s[x][k][0]==number) return 1; //Check Box Area int x1=0,y1=0; if(x<=3)x1=1;if(y<=3)y1=1; if(x>3 && x<=6)x1=4;if(y>3 && y<=6)y1=4; if(x>=7)x1=7;if(y>=7)y1=7; for(int i3=x1;i3<=x1+2;i3++) { for(int j3=y1;j3<=y1+2;j3++) { if(s[i3][j3][0]==number)return 1; } } return 0; } public boolean NeedForSpeed() { int xc=1; for(int nfs=1;nfs<=9;nfs++) for(int nfs1=1;nfs1<=9;nfs1++) { if(fnd[nfs][nfs1]==0) { fnd[nfs][ nfs1]=10; return true; } } return false; } public void BackTrack() { for(int gc=1;gc<count;gc++) { fnd[sel[gc][1]][sel[gc][2]]=10; sel[gc][0]=0;sel[gc][1]=0;sel[gc][2]=0; } count=0; } public void autocalculate() { for(int w=0;w<=9;w++) for(int x=0;x<=9;x++) { s[w][x][0]=0; for(int z=0;z<=9;z++) { s[w][x][z]=0; } } for(i=1;i<=9;i++) { for(j=1;j<=9;j++) { if(fnd[i][j]!=10) { s[i][j][0]=fnd[i][j]; } else { s[i][j][0]=0; } } } for(int i1=1;i1<=9;i1++) { for(int j1=1;j1<=9;j1++) { if(s[i1][j1][0]==0) { p=getautocalculate(i1,j1); for(int i2=1;i2<=9;i2++) { s[i1][j1][i2]=p[i2]; } } } } } public void randomfn() { muruga: { for(int zx=1;zx<=9;zx++) { for(int zy=1;zy<=9;zy++) { autocalculate(); if(minimum(zx,zy)==true) if(s[zx][zy][0]==0) { dat=getrnumber(zx,zy); fnd[zx][zy]=dat; t[zx][zy].setForeground(Color.blue); if(dat!=0) { sel[count][0]=1; sel[count][1]=zx; sel[count][2]=zy; sel[count][3]=dat; count++; } break muruga; } } } } if(NeedForSpeed()){BackTrack();} } public boolean finish() { for(int hd1=1;hd1<=9;hd1++) for(int hd2=1;hd2<=9;hd2++) if(fnd[hd1][hd2]==10) return false; return true; } public void mainexe() { int cd=0; for(int hj=1;hj<=9;hj++) for(int hj1=1;hj1<=9;hj1++) { if(!(t[hj][hj1].getText()).equals("")) fnd[hj][hj1]=Integer.parseInt(t[hj][hj1].getText()); else fnd[hj][hj1]=10; } Lord: { while(!finish()) { randomfn(); cd++; if(cd>600){break Lord;} } } for(int te1=1;te1<=9;te1++) for(int te2=1;te2<=9;te2++) t[te1][te2].setText(""+fnd[te1][te2]); } } class msg extends JDialog { msg() { JOptionPane pane=new JOptionPane("sudoku BY D.Karthick"); pane.setForeground(Color.red); pane.showMessageDialog(this,"Not Able To Find"); } } |