Thu, 31 Jan 2013 18:44:44 +0100
added solving algorithm
universe@5 | 1 | /* |
universe@5 | 2 | * Copyright 2013 Mike Becker. All rights reserved. |
universe@5 | 3 | * |
universe@5 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@5 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@5 | 6 | * |
universe@5 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@5 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@5 | 9 | * |
universe@5 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@5 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@5 | 12 | * documentation and/or other materials provided with the distribution. |
universe@5 | 13 | * |
universe@5 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@5 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@5 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@5 | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@5 | 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@5 | 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@5 | 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@5 | 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@5 | 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@5 | 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@5 | 24 | * POSSIBILITY OF SUCH DAMAGE. |
universe@5 | 25 | */ |
universe@5 | 26 | |
universe@5 | 27 | package de.uapcore.sudoku; |
universe@5 | 28 | |
universe@6 | 29 | import java.io.BufferedReader; |
universe@5 | 30 | import java.io.BufferedWriter; |
universe@6 | 31 | import java.io.FileInputStream; |
universe@5 | 32 | import java.io.FileOutputStream; |
universe@5 | 33 | import java.io.IOException; |
universe@6 | 34 | import java.io.InputStreamReader; |
universe@5 | 35 | import java.io.OutputStreamWriter; |
universe@6 | 36 | import java.util.regex.Matcher; |
universe@6 | 37 | import java.util.regex.Pattern; |
universe@5 | 38 | |
universe@5 | 39 | /** |
universe@5 | 40 | * |
universe@5 | 41 | * @author mike |
universe@5 | 42 | */ |
universe@5 | 43 | public class DocumentHandler { |
universe@5 | 44 | |
universe@5 | 45 | private String filename; |
universe@5 | 46 | |
universe@5 | 47 | public void load(Field field) throws IOException { |
universe@5 | 48 | if (!isFilenameSet()) { |
universe@5 | 49 | throw new IOException("no filename supplied"); |
universe@5 | 50 | } |
universe@6 | 51 | int row = 0; |
universe@6 | 52 | try (BufferedReader in = new BufferedReader( |
universe@6 | 53 | new InputStreamReader(new FileInputStream(filename)))) { |
universe@6 | 54 | Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$"); |
universe@6 | 55 | String line; |
universe@6 | 56 | while ((line = in.readLine()) != null) { |
universe@6 | 57 | if (line.matches("^\\s*$")) { |
universe@6 | 58 | continue; |
universe@6 | 59 | } |
universe@6 | 60 | Matcher m = pat.matcher(line); |
universe@6 | 61 | if (m.matches()) { |
universe@6 | 62 | String c[] = line.trim().split(" "); |
universe@6 | 63 | if (c.length != 9) { |
universe@6 | 64 | break; |
universe@6 | 65 | } |
universe@6 | 66 | for (int i = 0 ; i < 9 ; i++) { |
universe@6 | 67 | field.setCellValue(i, row, |
universe@6 | 68 | c[i].equals("_") ? 0 : Integer.valueOf(c[i])); |
universe@6 | 69 | } |
universe@6 | 70 | row++; |
universe@6 | 71 | } else { |
universe@6 | 72 | break; |
universe@6 | 73 | } |
universe@6 | 74 | } |
universe@6 | 75 | if (row != 9) { |
universe@6 | 76 | throw new IOException("Kein Sudoku-Feld enthalten!"); |
universe@6 | 77 | } |
universe@6 | 78 | } |
universe@7 | 79 | field.setAllCellsModified(false); |
universe@5 | 80 | } |
universe@5 | 81 | |
universe@5 | 82 | public void save(Field field) throws IOException { |
universe@5 | 83 | if (!isFilenameSet()) { |
universe@5 | 84 | throw new IOException("no filename supplied"); |
universe@5 | 85 | } |
universe@5 | 86 | |
universe@5 | 87 | try (BufferedWriter out = new BufferedWriter( |
universe@5 | 88 | new OutputStreamWriter(new FileOutputStream(filename)))) { |
universe@5 | 89 | for (int i = 0 ; i < 9 ; i++) { |
universe@5 | 90 | int[] row = field.getRow(i); |
universe@5 | 91 | for (int j = 0 ; j < 9 ; j++) { |
universe@5 | 92 | out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); |
universe@5 | 93 | out.append(j == 8 ? '\n': ' '); |
universe@5 | 94 | } |
universe@5 | 95 | } |
universe@5 | 96 | } |
universe@5 | 97 | } |
universe@5 | 98 | |
universe@5 | 99 | public void setFilename(String filename) { |
universe@5 | 100 | this.filename = filename; |
universe@5 | 101 | } |
universe@5 | 102 | |
universe@5 | 103 | public void clearFilename() { |
universe@5 | 104 | filename = null; |
universe@5 | 105 | } |
universe@5 | 106 | |
universe@5 | 107 | public boolean isFilenameSet() { |
universe@5 | 108 | return filename != null; |
universe@5 | 109 | } |
universe@5 | 110 | } |