Fri, 01 Feb 2013 10:18:47 +0100
removed completeness test - the solver automatically returns false when the field is incomplete
/* * Copyright 2013 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.uapcore.sudoku; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author mike */ public class DocumentHandler { private String filename; public void load(Field field) throws IOException { if (!isFilenameSet()) { throw new IOException("no filename supplied"); } int row = 0; try (BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(filename)))) { Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$"); String line; while ((line = in.readLine()) != null) { if (line.matches("^\\s*$")) { continue; } Matcher m = pat.matcher(line); if (m.matches()) { String c[] = line.trim().split(" "); if (c.length != 9) { break; } for (int i = 0 ; i < 9 ; i++) { field.setCellValue(i, row, c[i].equals("_") ? 0 : Integer.valueOf(c[i])); } row++; } else { break; } } if (row != 9) { throw new IOException("Kein Sudoku-Feld enthalten!"); } } field.setAllCellsModified(false); } public void save(Field field) throws IOException { if (!isFilenameSet()) { throw new IOException("no filename supplied"); } try (BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(filename)))) { for (int i = 0 ; i < 9 ; i++) { int[] row = field.getRow(i); for (int j = 0 ; j < 9 ; j++) { out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); out.append(j == 8 ? '\n': ' '); } } } } public void setFilename(String filename) { this.filename = filename; } public void clearFilename() { filename = null; } public boolean isFilenameSet() { return filename != null; } }