src/de/uapcore/sudoku/ActionHandler.java

Sat, 26 Jan 2013 17:42:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 17:42:07 +0100
changeset 2
5179eff8a9b6
child 3
ed931970b4ac
permissions
-rw-r--r--

check functions

2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 package de.uapcore.sudoku;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 import java.awt.event.ActionEvent;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 import java.awt.event.ActionListener;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 import javax.swing.JOptionPane;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 /**
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * @author mike
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 */
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 public final class ActionHandler implements ActionListener {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 public static final String SAVE = "save";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 public static final String CHECK = "check";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 public static final String SOLVE = "solve";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 private Field field;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 private Solver solver;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 public ActionHandler(Field f) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 field = f;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 solver = new Solver();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 private void save() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 if (solver.check(field)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 field.setAllCellsModified(false);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 // TODO: save to file
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 JOptionPane.showMessageDialog(field,
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 "Das Feld kann mit Fehlern nicht gespeichert werden!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 "Sudoku", JOptionPane.ERROR_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 private void check() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 if (solver.check(field)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 "Sudoku", JOptionPane.WARNING_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 private void solve() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 // TODO: solve
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 @Override
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 public void actionPerformed(ActionEvent e) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 switch (e.getActionCommand()) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 case "save":
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 save();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 break;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 case "check":
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 check();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 break;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 case "solve":
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 solve();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 break;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 default:
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 throw new UnsupportedOperationException(
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 "unknown action: "+e.getActionCommand());
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }

mercurial