Thu, 31 Jan 2013 18:44:44 +0100
added solving algorithm
universe@3 | 1 | /* |
universe@3 | 2 | * Copyright 2013 Mike Becker. All rights reserved. |
universe@3 | 3 | * |
universe@3 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@3 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@3 | 6 | * |
universe@3 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@3 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@3 | 9 | * |
universe@3 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@3 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@3 | 12 | * documentation and/or other materials provided with the distribution. |
universe@3 | 13 | * |
universe@3 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@3 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@3 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@3 | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@3 | 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@3 | 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@3 | 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@3 | 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@3 | 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@3 | 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@3 | 24 | * POSSIBILITY OF SUCH DAMAGE. |
universe@3 | 25 | */ |
universe@3 | 26 | |
universe@2 | 27 | package de.uapcore.sudoku; |
universe@2 | 28 | |
universe@2 | 29 | import java.awt.event.ActionEvent; |
universe@2 | 30 | import java.awt.event.ActionListener; |
universe@6 | 31 | import java.io.File; |
universe@6 | 32 | import java.io.IOException; |
universe@6 | 33 | import javax.swing.JFileChooser; |
universe@2 | 34 | import javax.swing.JOptionPane; |
universe@2 | 35 | |
universe@2 | 36 | /** |
universe@2 | 37 | * |
universe@2 | 38 | * @author mike |
universe@2 | 39 | */ |
universe@2 | 40 | public final class ActionHandler implements ActionListener { |
universe@2 | 41 | |
universe@2 | 42 | public static final String SAVE = "save"; |
universe@2 | 43 | public static final String CHECK = "check"; |
universe@2 | 44 | public static final String SOLVE = "solve"; |
universe@2 | 45 | |
universe@3 | 46 | public static final String NEW = "new"; |
universe@3 | 47 | public static final String OPEN = "open"; |
universe@3 | 48 | public static final String SAVE_AS = "save as"; |
universe@3 | 49 | public static final String QUIT = "quit"; |
universe@3 | 50 | public static final String ABOUT = "about"; |
universe@3 | 51 | |
universe@2 | 52 | private Field field; |
universe@2 | 53 | private Solver solver; |
universe@5 | 54 | private DocumentHandler doc; |
universe@2 | 55 | |
universe@2 | 56 | public ActionHandler(Field f) { |
universe@2 | 57 | field = f; |
universe@2 | 58 | solver = new Solver(); |
universe@5 | 59 | doc = new DocumentHandler(); |
universe@5 | 60 | } |
universe@5 | 61 | |
universe@5 | 62 | private boolean chooseSaveFilename() { |
universe@6 | 63 | JFileChooser fc = new JFileChooser("."); |
universe@6 | 64 | fc.setMultiSelectionEnabled(false); |
universe@6 | 65 | if (fc.showSaveDialog(field) == JFileChooser.APPROVE_OPTION) { |
universe@6 | 66 | File f = fc.getSelectedFile(); |
universe@6 | 67 | if (f.exists()) { |
universe@6 | 68 | int result = JOptionPane.showConfirmDialog(field, |
universe@6 | 69 | "Bereits existierende Datei überschreiben?", "Sudoku", |
universe@6 | 70 | JOptionPane.YES_NO_OPTION); |
universe@6 | 71 | if (result == JOptionPane.YES_OPTION) { |
universe@6 | 72 | doc.setFilename(f.getAbsolutePath()); |
universe@6 | 73 | return true; |
universe@6 | 74 | } else { |
universe@6 | 75 | return false; |
universe@6 | 76 | } |
universe@6 | 77 | } else { |
universe@6 | 78 | doc.setFilename(f.getAbsolutePath()); |
universe@6 | 79 | return true; |
universe@6 | 80 | } |
universe@6 | 81 | } else { |
universe@6 | 82 | return false; |
universe@6 | 83 | } |
universe@2 | 84 | } |
universe@2 | 85 | |
universe@6 | 86 | private void open() { |
universe@6 | 87 | JFileChooser fc = new JFileChooser("."); |
universe@6 | 88 | fc.setMultiSelectionEnabled(false); |
universe@6 | 89 | if (fc.showOpenDialog(field) == JFileChooser.APPROVE_OPTION) { |
universe@6 | 90 | File f = fc.getSelectedFile(); |
universe@6 | 91 | doc.setFilename(f.getAbsolutePath()); |
universe@6 | 92 | try { |
universe@6 | 93 | doc.load(field); |
universe@6 | 94 | } catch (IOException e) { |
universe@6 | 95 | JOptionPane.showMessageDialog(field, |
universe@6 | 96 | "Datei konnte nicht geladen werden: "+e.getMessage(), |
universe@6 | 97 | "Sudoku", JOptionPane.ERROR_MESSAGE); |
universe@6 | 98 | } |
universe@6 | 99 | } |
universe@6 | 100 | } |
universe@6 | 101 | |
universe@6 | 102 | private boolean save(boolean rename) { |
universe@6 | 103 | if (!doc.isFilenameSet() || rename) { |
universe@5 | 104 | if (!chooseSaveFilename()) { |
universe@5 | 105 | return false; |
universe@5 | 106 | } |
universe@5 | 107 | } |
universe@2 | 108 | if (solver.check(field)) { |
universe@2 | 109 | field.setAllCellsModified(false); |
universe@6 | 110 | try { |
universe@6 | 111 | doc.save(field); |
universe@6 | 112 | } catch (IOException e) { |
universe@6 | 113 | JOptionPane.showMessageDialog(field, |
universe@6 | 114 | "Datei konnte nicht gespeichert werden: "+e.getMessage(), |
universe@6 | 115 | "Sudoku", JOptionPane.ERROR_MESSAGE); |
universe@6 | 116 | } |
universe@3 | 117 | return true; |
universe@2 | 118 | } else { |
universe@2 | 119 | JOptionPane.showMessageDialog(field, |
universe@2 | 120 | "Das Feld kann mit Fehlern nicht gespeichert werden!", |
universe@2 | 121 | "Sudoku", JOptionPane.ERROR_MESSAGE); |
universe@3 | 122 | return false; |
universe@2 | 123 | } |
universe@2 | 124 | } |
universe@2 | 125 | |
universe@2 | 126 | private void check() { |
universe@2 | 127 | if (solver.check(field)) { |
universe@2 | 128 | JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!", |
universe@2 | 129 | "Sudoku", JOptionPane.INFORMATION_MESSAGE); |
universe@2 | 130 | } else { |
universe@2 | 131 | JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!", |
universe@2 | 132 | "Sudoku", JOptionPane.WARNING_MESSAGE); |
universe@2 | 133 | } |
universe@2 | 134 | } |
universe@2 | 135 | |
universe@2 | 136 | private void solve() { |
universe@7 | 137 | if (!solver.check(field) || !solver.solve(field)) { |
universe@7 | 138 | JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!", |
universe@7 | 139 | "Sudoku", JOptionPane.WARNING_MESSAGE); |
universe@7 | 140 | } |
universe@2 | 141 | } |
universe@4 | 142 | |
universe@4 | 143 | private boolean saveUnsaved() { |
universe@4 | 144 | boolean proceed = false; |
universe@4 | 145 | if (field.isAnyCellModified()) { |
universe@4 | 146 | int result = JOptionPane.showConfirmDialog(field, |
universe@4 | 147 | "Das Feld ist ungespeichert - jetzt speichern?", |
universe@4 | 148 | "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); |
universe@4 | 149 | if (result == JOptionPane.YES_OPTION) { |
universe@6 | 150 | if (save(false)) { |
universe@4 | 151 | proceed = true; |
universe@4 | 152 | } |
universe@4 | 153 | } else if (result == JOptionPane.NO_OPTION) { |
universe@4 | 154 | proceed = true; |
universe@4 | 155 | } |
universe@4 | 156 | } else { |
universe@4 | 157 | proceed = true; |
universe@4 | 158 | } |
universe@4 | 159 | |
universe@4 | 160 | return proceed; |
universe@4 | 161 | } |
universe@2 | 162 | |
universe@2 | 163 | @Override |
universe@2 | 164 | public void actionPerformed(ActionEvent e) { |
universe@2 | 165 | switch (e.getActionCommand()) { |
universe@4 | 166 | case NEW: |
universe@4 | 167 | if (saveUnsaved()) { |
universe@7 | 168 | doc.clearFilename(); |
universe@4 | 169 | field.clear(); |
universe@4 | 170 | } |
universe@4 | 171 | break; |
universe@6 | 172 | case OPEN: |
universe@6 | 173 | open(); |
universe@6 | 174 | break; |
universe@3 | 175 | case SAVE: |
universe@6 | 176 | save(false); |
universe@6 | 177 | break; |
universe@6 | 178 | case SAVE_AS: |
universe@6 | 179 | save(true); |
universe@2 | 180 | break; |
universe@3 | 181 | case CHECK: |
universe@2 | 182 | check(); |
universe@2 | 183 | break; |
universe@3 | 184 | case SOLVE: |
universe@2 | 185 | solve(); |
universe@2 | 186 | break; |
universe@3 | 187 | case QUIT: |
universe@4 | 188 | if (saveUnsaved()) { |
universe@3 | 189 | System.exit(0); |
universe@3 | 190 | } |
universe@3 | 191 | break; |
universe@3 | 192 | case ABOUT: |
universe@3 | 193 | JOptionPane.showMessageDialog(field, |
universe@3 | 194 | "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ |
universe@3 | 195 | "\nPublished under the BSD License", |
universe@3 | 196 | "Sudoku", JOptionPane.INFORMATION_MESSAGE); |
universe@3 | 197 | break; |
universe@2 | 198 | default: |
universe@2 | 199 | throw new UnsupportedOperationException( |
universe@2 | 200 | "unknown action: "+e.getActionCommand()); |
universe@2 | 201 | } |
universe@2 | 202 | } |
universe@2 | 203 | |
universe@2 | 204 | } |