Sat, 26 Jan 2013 19:34:31 +0100
moved field methods to field class + added (parts of the) document handler
1.1 --- a/src/de/uapcore/sudoku/ActionHandler.java Sat Jan 26 18:43:49 2013 +0100 1.2 +++ b/src/de/uapcore/sudoku/ActionHandler.java Sat Jan 26 19:34:31 2013 +0100 1.3 @@ -48,13 +48,25 @@ 1.4 1.5 private Field field; 1.6 private Solver solver; 1.7 + private DocumentHandler doc; 1.8 1.9 public ActionHandler(Field f) { 1.10 field = f; 1.11 solver = new Solver(); 1.12 + doc = new DocumentHandler(); 1.13 + } 1.14 + 1.15 + private boolean chooseSaveFilename() { 1.16 + // TODO: fileselector 1.17 + return false; 1.18 } 1.19 1.20 private boolean save() { 1.21 + if (!doc.isFilenameSet()) { 1.22 + if (!chooseSaveFilename()) { 1.23 + return false; 1.24 + } 1.25 + } 1.26 if (solver.check(field)) { 1.27 field.setAllCellsModified(false); 1.28 // TODO: save to file
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/de/uapcore/sudoku/DocumentHandler.java Sat Jan 26 19:34:31 2013 +0100 2.3 @@ -0,0 +1,77 @@ 2.4 +/* 2.5 + * Copyright 2013 Mike Becker. All rights reserved. 2.6 + * 2.7 + * Redistribution and use in source and binary forms, with or without 2.8 + * modification, are permitted provided that the following conditions are met: 2.9 + * 2.10 + * 1. Redistributions of source code must retain the above copyright 2.11 + * notice, this list of conditions and the following disclaimer. 2.12 + * 2.13 + * 2. Redistributions in binary form must reproduce the above copyright 2.14 + * notice, this list of conditions and the following disclaimer in the 2.15 + * documentation and/or other materials provided with the distribution. 2.16 + * 2.17 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.18 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.20 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.21 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.22 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.23 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.24 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.25 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.26 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.27 + * POSSIBILITY OF SUCH DAMAGE. 2.28 + */ 2.29 + 2.30 +package de.uapcore.sudoku; 2.31 + 2.32 +import java.io.BufferedWriter; 2.33 +import java.io.FileOutputStream; 2.34 +import java.io.IOException; 2.35 +import java.io.OutputStreamWriter; 2.36 + 2.37 +/** 2.38 + * 2.39 + * @author mike 2.40 + */ 2.41 +public class DocumentHandler { 2.42 + 2.43 + private String filename; 2.44 + 2.45 + public void load(Field field) throws IOException { 2.46 + if (!isFilenameSet()) { 2.47 + throw new IOException("no filename supplied"); 2.48 + } 2.49 + // TODO: implement 2.50 + } 2.51 + 2.52 + public void save(Field field) throws IOException { 2.53 + if (!isFilenameSet()) { 2.54 + throw new IOException("no filename supplied"); 2.55 + } 2.56 + 2.57 + try (BufferedWriter out = new BufferedWriter( 2.58 + new OutputStreamWriter(new FileOutputStream(filename)))) { 2.59 + for (int i = 0 ; i < 9 ; i++) { 2.60 + int[] row = field.getRow(i); 2.61 + for (int j = 0 ; j < 9 ; j++) { 2.62 + out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); 2.63 + out.append(j == 8 ? '\n': ' '); 2.64 + } 2.65 + } 2.66 + } 2.67 + } 2.68 + 2.69 + public void setFilename(String filename) { 2.70 + this.filename = filename; 2.71 + } 2.72 + 2.73 + public void clearFilename() { 2.74 + filename = null; 2.75 + } 2.76 + 2.77 + public boolean isFilenameSet() { 2.78 + return filename != null; 2.79 + } 2.80 +}
3.1 --- a/src/de/uapcore/sudoku/Field.java Sat Jan 26 18:43:49 2013 +0100 3.2 +++ b/src/de/uapcore/sudoku/Field.java Sat Jan 26 19:34:31 2013 +0100 3.3 @@ -62,7 +62,6 @@ 3.4 3.5 @Override 3.6 public void paint(Graphics graphics) { 3.7 - super.paint(graphics); 3.8 final int w = getWidth(); 3.9 final int h = getHeight(); 3.10 final int cw = w / 9; 3.11 @@ -90,6 +89,7 @@ 3.12 } 3.13 3.14 graphics.drawImage(img, 0, 0, this); 3.15 + super.paintChildren(graphics); 3.16 } 3.17 3.18 public int getCellValue(int x, int y) { 3.19 @@ -126,4 +126,45 @@ 3.20 } 3.21 } 3.22 } 3.23 + 3.24 + public int[][] getSquare(int x, int y) { 3.25 + if (x < 0 || x > 2 || y < 0 || y > 2) { 3.26 + throw new IllegalArgumentException("Invalid square coordinates"); 3.27 + } 3.28 + int[][] square = new int[3][3]; 3.29 + 3.30 + for (int u = 0 ; u < 3 ; u++) { 3.31 + for (int v = 0 ; v < 3 ; v++) { 3.32 + square[u][v] = getCellValue(3*x+u, 3*y+v); 3.33 + } 3.34 + } 3.35 + 3.36 + return square; 3.37 + } 3.38 + 3.39 + public int[] getRow(int y) { 3.40 + if (y < 0 || y > 8) { 3.41 + throw new IllegalArgumentException("Invalid row number"); 3.42 + } 3.43 + int row[] = new int[9]; 3.44 + 3.45 + for (int x = 0 ; x < 9 ; x++) { 3.46 + row[x] = getCellValue(x, y); 3.47 + } 3.48 + 3.49 + return row; 3.50 + } 3.51 + 3.52 + public int[] getColumn(int x) { 3.53 + if (x < 0 || x > 8) { 3.54 + throw new IllegalArgumentException("Invalid column number"); 3.55 + } 3.56 + int column[] = new int[9]; 3.57 + 3.58 + for (int y = 0 ; y < 9 ; y++) { 3.59 + column[y] = getCellValue(x, y); 3.60 + } 3.61 + 3.62 + return column; 3.63 + } 3.64 }
4.1 --- a/src/de/uapcore/sudoku/Solver.java Sat Jan 26 18:43:49 2013 +0100 4.2 +++ b/src/de/uapcore/sudoku/Solver.java Sat Jan 26 19:34:31 2013 +0100 4.3 @@ -38,11 +38,11 @@ 4.4 public boolean check(Field f) { 4.5 int line[]; 4.6 for (int i = 0 ; i < 9 ; i++) { 4.7 - line = getRow(f, i); 4.8 + line = f.getRow(i); 4.9 if (!valid(line)) { 4.10 return false; 4.11 } 4.12 - line = getColumn(f, i); 4.13 + line = f.getColumn(i); 4.14 if (!valid(line)) { 4.15 return false; 4.16 } 4.17 @@ -51,7 +51,7 @@ 4.18 int square[][]; 4.19 for (int x = 0 ; x < 3 ; x++) { 4.20 for (int y = 0 ; y < 3 ; y++) { 4.21 - square = getSquare(f, x, y); 4.22 + square = f.getSquare(x, y); 4.23 if (!valid(square)) { 4.24 return false; 4.25 } 4.26 @@ -82,11 +82,12 @@ 4.27 } 4.28 4.29 private boolean valid(int[] line) { 4.30 - int numbers[] = new int[9]; 4.31 + int numbers[]; 4.32 + numbers = new int[9]; 4.33 for (int i = 0 ; i < 9 ; i++) { 4.34 int l = line[i]-1; 4.35 if (l >= 0) { 4.36 - if (++numbers[l] > 1) { 4.37 + if ((++numbers[l]) > 1) { 4.38 return false; 4.39 } 4.40 } 4.41 @@ -98,51 +99,8 @@ 4.42 private boolean valid(int[][] square) { 4.43 int[] line = new int[9]; 4.44 for (int x = 0 ; x < 3 ; x++) { 4.45 - for (int y = 0 ; y < 3 ; y++) { 4.46 - line[3*x+y] = square[x][y]; 4.47 - } 4.48 + System.arraycopy(square[x], 0, line, 3*x, 3); 4.49 } 4.50 return valid(line); 4.51 } 4.52 - 4.53 - private int[][] getSquare(Field f, int x, int y) { 4.54 - if (x < 0 || x > 2 || y < 0 || y > 2) { 4.55 - throw new IllegalArgumentException("Invalid square coordinates"); 4.56 - } 4.57 - int[][] square = new int[3][3]; 4.58 - 4.59 - for (int u = 0 ; u < 3 ; u++) { 4.60 - for (int v = 0 ; v < 3 ; v++) { 4.61 - square[u][v] = f.getCellValue(3*x+u, 3*y+v); 4.62 - } 4.63 - } 4.64 - 4.65 - return square; 4.66 - } 4.67 - 4.68 - private int[] getRow(Field f, int y) { 4.69 - if (y < 0 || y > 8) { 4.70 - throw new IllegalArgumentException("Invalid row number"); 4.71 - } 4.72 - int row[] = new int[9]; 4.73 - 4.74 - for (int x = 0 ; x < 9 ; x++) { 4.75 - row[x] = f.getCellValue(x, y); 4.76 - } 4.77 - 4.78 - return row; 4.79 - } 4.80 - 4.81 - private int[] getColumn(Field f, int x) { 4.82 - if (x < 0 || x > 8) { 4.83 - throw new IllegalArgumentException("Invalid column number"); 4.84 - } 4.85 - int column[] = new int[9]; 4.86 - 4.87 - for (int y = 0 ; y < 9 ; y++) { 4.88 - column[y] = f.getCellValue(x, y); 4.89 - } 4.90 - 4.91 - return column; 4.92 - } 4.93 }