-
Notifications
You must be signed in to change notification settings - Fork 0
/
GabungSudokuNinis.java
561 lines (448 loc) · 16.4 KB
/
GabungSudokuNinis.java
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.omg.Messaging.SyncScopeHelper;
public class GabungSudokuNinis extends JFrame {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
static String inputDimension;
static int n = helo();
static ArrayList<String> inputs = new ArrayList<String>();
static ArrayList<JTextField> texts = new ArrayList<JTextField>();
static ArrayList<String> coordinateX = new ArrayList<String>();
static ArrayList<String> coordinateY = new ArrayList<String>();
static JTextField[][] textfields = new JTextField[n][n];
static JButton solve;
static JButton clear;
public static HashMap<String,Integer> hm = new HashMap<String,Integer>();
public static HashMap<Integer, String> hmresult = new HashMap<Integer, String>();
public static BufferedWriter bw;
public static ArrayList<String> hasilMinisat = new ArrayList<String>();
public static ArrayList<String> isiUser = new ArrayList<String>();
String result;
public GabungSudokuNinis() {
this.setSize(600,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GabungSudokuNinis.addComponentsToPane(this.getContentPane());
this.buttonHandler();
}
public static int helo(){
inputDimension = JOptionPane.showInputDialog("enter the dimention");
n = Integer.parseInt(inputDimension);
System.out.println(n);
//System.out.println(textfields.length);
return n;
}
public static void infoBox(String infoMsg, String title) {
JOptionPane.showMessageDialog(null, infoMsg, "InfoBox: " + title, JOptionPane.INFORMATION_MESSAGE);
}
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridLayout(n+1,n+1));
GridBagConstraints c = new GridBagConstraints();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
textfields[i][j] = new JTextField(j);
pane.add(textfields[i][j]);
//c.fill = GridBagConstraints.NONE;
//c.gridx = j;
//c.gridy = i;
//pane.add(textfields[i][j], c);
}
}
solve = new JButton("Solve");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = GridBagConstraints.EAST;//top padding
c.gridy = GridBagConstraints.EAST; //third row
pane.add(solve, c);
clear = new JButton("Clear");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = GridBagConstraints.EAST;//top padding
c.gridy = GridBagConstraints.EAST; //third row
pane.add(clear, c);
}
public void buttonHandler() {
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
textfields[i][j].setText("");
}
}
// vitri
hasilMinisat.clear();
isiUser.clear();
hmresult.clear();
hm.clear();
//System.out.println(hasilMinisat.size());
}
});
solve.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int p;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(!textfields[i][j].getText().isEmpty()) {
if(textfields[i][j].getText().matches("[a-zA-z]{1}")) {
GabungSudokuNinis.infoBox("Sorry, input must be integer between 1 to " + n, "Wrong input");
return;
}
p = Integer.parseInt(textfields[i][j].getText());
if(p>n || p<1) {
GabungSudokuNinis.infoBox("Sorry, your input must be between 1 to " + n, "Wrong input");
return;
}
result = (i+1) + "," + (j+1) + "," + textfields[i][j].getText();
isiUser.add(result);
}
}
}
try {
carisolusi(n,isiUser); // vitri
for(int i =0; i< hasilMinisat.size();i++){
System.out.println(hasilMinisat.get(i));
}
for(int i =0; i< hasilMinisat.size();i++){
String tmp = hasilMinisat.get(i);
//System.out.println(tmp);
//System.out.println("iniii angkanya "+ hm.get(tmp));
//hm key : koordinat value : angka
// menampilkan text field
String[] splitOrdinat = tmp.split(",");
int x = Integer.parseInt(splitOrdinat[0]) -1;
int y = Integer.parseInt(splitOrdinat[1]) -1;
String value = splitOrdinat[2];
textfields[x][y].setText(value);
//hasilMinisat.clear();
}hasilMinisat.clear();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// nyambung ke minisat
//baca minisat
}
});
}
/**JTextField text = new JTextField(1);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
//bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(text, c);
JTextField text1 = new JTextField(1);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
//bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 1; //third row
pane.add(text1, c);
**/
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
/**private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SudokuBoard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setSize(600, 400);
frame.setVisible(true);
}**/
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
GabungSudokuNinis t = new GabungSudokuNinis();
t.setTitle("Sudoku " + n + "x" + n);
t.setVisible(true);
}
});
}
// vitri
public static void carisolusi(int n, ArrayList<String> arr) throws InterruptedException{
try {
int inputUser = arr.size();
int jumlahVar = (int) Math.pow(n, 3);
int jumlahKlausa = (4*n*n) + (4* n * n * (combination(n,2))) + inputUser;
String content = "p cnf "+ jumlahVar+" "+jumlahKlausa +"\n" ;
File file = new File("filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(content);
encode(n);
hitung(n);
//memasukan constraint yang berasal dari masukan user
if(isiUser.size()!=0){
for(int i =0;i<isiUser.size();i++){
bw.write(hm.get(isiUser.get(i))+" 0"+"\n");
//System.out.println(isiUser.get(i));
}
}
bw.close();
//kode untuk menjalankan minisat
//Process process = Runtime.getRuntime().exec("minisat filename.txt result.txt");
//process.waitFor();
System.out.println("Masih proses minisat");
System.out.println("Done");
/**String[] args = new String[] {"/bin/bash", "-c", "minisat filename.txt result.txt", "", ""};
Process process = new ProcessBuilder(args).start();
process.waitFor();
**/
//membaca output
System.out.println("Masuk br");
BufferedReader br = new BufferedReader(new FileReader("result.txt"));
String line = br.readLine();
if(line.equalsIgnoreCase("SAT")){
System.out.println("Problem Satisfiable");
String barisdua = br.readLine();
System.out.println("Ini baris 2: " +barisdua);
String baristiga = br.readLine();
System.out.println("Ini baris 3: " +baristiga);
String[] angka = barisdua.split(" ");
int hasilSplit=0;
//Meletakkan angka pada koordinat yang benar. Hasil jawaban benar ada pada HashMap hasilMinisat
for(int i = 0;i<angka.length;i++){
hasilSplit= Integer.parseInt(angka[i]);
if(hasilSplit>0){
//System.out.println(""+hasilSplit);
//System.out.println(hasilSplit);
//System.out.println("nilai yang masuk ke minisat"+hmresult.get(hasilSplit));
hasilMinisat.add(""+hmresult.get(hasilSplit));
//System.out.println(""+hmresult.get(hasilSplit));
}
}
System.out.println("KOORDINAT YANG BENAR");
for(int i =0; i< hasilMinisat.size();i++){
String tmp = hasilMinisat.get(i);
//System.out.println(hasilMinisat.get(i));
String[] splitOrdinat = tmp.split(",");
int x = Integer.parseInt(splitOrdinat[0]) -1;
int y = Integer.parseInt(splitOrdinat[1]) -1;
int value = Integer.parseInt(splitOrdinat[2]) ;
}
}else{
System.out.println("Tidak ada solusi!");
GabungSudokuNinis.infoBox("Sorry, your input is unsatisfiable" + n, "Unsatifiable");
hasilMinisat.clear();
isiUser.clear();
}
//System.out.println(line);
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int combination(int n, int k)
{
return (fact(n) / (fact(k) * fact(n - k)));
}
public static int fact(int i)
{
if (i == 1)
{
return 1;
}
return i * fact(i - 1);
}
public static void encode(int n){
int code = 1;
for(int i = 1; i<=n;i++){
for(int j = 1; j<=n;j++){
for(int k =1;k<=n;k++){
hm.put(""+i+","+j+","+k, code); //hm key : koordinat value : angka
hmresult.put(code, ""+i+","+j+","+k); //hmresult key: angka , value : koordinat
code++;
}
}
}
}
public static void hitung(int n) throws IOException{
int hit = 0;
//mengecek pada setiap field at least diisi satu digit
for(int i = 1; i<=n;i++){
for(int j = 1; j<=n;j++){
for(int k =1;k<=n;k++){
if(k==n){
//System.out.println(hm.get(""+i+","+j+","+k)+" 0" );
bw.write(hm.get(""+i+","+j+","+k)+" 0"+"\n");
}else{
//System.out.print(hm.get(""+i+","+j+","+k)+" " );
bw.write(hm.get(""+i+","+j+","+k)+" ");
}
}
hit += 1;
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek pada setiap field hanya ada satu digit
for(int i = 1; i<=n;i++){
for(int j = 1; j<=n;j++){
for(int k =1;k<n;k++){
for (int m = k+1; m <=n; m++){
//System.out.println("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+j+","+ m)+" 0");
bw.write("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+j+","+ m)+" 0"+"\n");
hit += 1;
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap baris at least ada satu digit
for(int j = 1; j<=n;j++){
for(int k = 1; k<=n;k++){
for(int i =1;i<=n;i++){
if(i==n){
//System.out.println(hm.get(""+i+","+j+","+k)+" 0" );
bw.write(hm.get(""+i+","+j+","+k)+" 0"+"\n");
}else{
//System.out.print(hm.get(""+i+","+j+","+k)+" " );
bw.write(hm.get(""+i+","+j+","+k)+" ");
}
}
hit += 1;
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap baris cuma terisi satu digit
for(int j = 1; j<=n;j++){
for(int k = 1; k<=n;k++){
for(int i =1;i<n;i++){
for (int m = (i+1); m<=n; m++){
//System.out.println("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(m+","+j+","+ k)+" 0");
bw.write("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(m+","+j+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap kolom at least ada satu digit
for(int i = 1; i<=n;i++){
for(int k = 1; k<=n;k++){
for(int j =1;j<=n;j++){
if(j==n){
//System.out.println(hm.get(""+i+","+j+","+k)+" 0" );
bw.write(hm.get(""+i+","+j+","+k)+" 0"+"\n");
}else{
//System.out.print(hm.get(""+i+","+j+","+k)+" " );
bw.write(hm.get(""+i+","+j+","+k)+" ");
}
}
hit += 1;
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap kolom hanya ada satu digit
for(int i = 1; i<=n;i++){
for(int k = 1; k<=n;k++){
for(int j =1;j<n;j++){
for (int m = j+1; m<=n; m++){
//System.out.println("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+m+","+ k)+" 0");
bw.write("-"+hm.get(""+i+","+j+","+k)+" -"+hm.get(i+","+m+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap subgrid at least ada satu digit
int akar = (int) Math.sqrt(n);
int kelipatan=1;
for(int k = 1;k<=n;k++){
for(int m = 0;m<=(akar-1);m++){
for(int p =0;p<=(akar-1);p++){
for(int i=1;i<=akar;i++){
for(int j=1;j<=akar;j++){
if(kelipatan%n==0){
kelipatan=0;
//System.out.println((""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" 0" );
//System.out.println(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" 0" );
bw.write(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" 0"+"\n" );
kelipatan++;
hit +=1;
}else{
//System.out.print(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" " );
bw.write(hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" ");;
kelipatan++;
}
}
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
//mengecek tiap subgrid at most cuma ada 1 digit
for(int k = 1; k<=n;k++){
for(int m=0; m<=(akar-1); m++){
for(int p =0; p<=(akar-1);p++){
for(int i = 1;i<=akar;i++){
for(int j=1;j<=akar;j++){
for(int q = j+1;q<=akar;q++){
//System.out.println("-"+(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+(((akar*m)+i)+","+((akar*p)+q)+","+ k)+" 0");
bw.write("-"+hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+hm.get(((akar*m)+i)+","+((akar*p)+q)+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
}
}
//System.out.println("Jumlah hitung = " + hit);
for(int k = 1; k<=n;k++)
for(int m=0; m<=(akar-1); m++){
for(int p =0; p<=(akar-1);p++){
for(int i = 1;i<=akar;i++){
for(int j=1;j<=akar;j++){
for(int q = i+1;q<=akar;q++){
for(int r = 1;r<=akar;r++){
//System.out.println("-"+(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+(((akar*m)+q)+","+((akar*p)+r)+","+ k)+" 0");
bw.write("-"+hm.get(""+((akar*m)+i)+","+((akar*p)+j)+","+k)+" -"+hm.get(((akar*m)+q)+","+((akar*p)+r)+","+ k)+" 0"+"\n");
hit += 1;
}
}
}
}
}
}
}
}