moved field methods to field class + added (parts of the) document handler

Sat, 26 Jan 2013 19:34:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 19:34:31 +0100
changeset 5
8ddf4af937d7
parent 4
b8588e318001
child 6
5bab2e971333

moved field methods to field class + added (parts of the) document handler

src/de/uapcore/sudoku/ActionHandler.java file | annotate | diff | comparison | revisions
src/de/uapcore/sudoku/DocumentHandler.java file | annotate | diff | comparison | revisions
src/de/uapcore/sudoku/Field.java file | annotate | diff | comparison | revisions
src/de/uapcore/sudoku/Solver.java file | annotate | diff | comparison | revisions
--- a/src/de/uapcore/sudoku/ActionHandler.java	Sat Jan 26 18:43:49 2013 +0100
+++ b/src/de/uapcore/sudoku/ActionHandler.java	Sat Jan 26 19:34:31 2013 +0100
@@ -48,13 +48,25 @@
     
     private Field field;
     private Solver solver;
+    private DocumentHandler doc;
     
     public ActionHandler(Field f) {
         field = f;
         solver = new Solver();
+        doc = new DocumentHandler();
+    }
+    
+    private boolean chooseSaveFilename() {
+        // TODO: fileselector
+        return false;
     }
     
     private boolean save() {
+        if (!doc.isFilenameSet()) {
+            if (!chooseSaveFilename()) {
+                return false;
+            }
+        }
         if (solver.check(field)) {
             field.setAllCellsModified(false);
             // TODO: save to file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/uapcore/sudoku/DocumentHandler.java	Sat Jan 26 19:34:31 2013 +0100
@@ -0,0 +1,77 @@
+/*
+ * 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.BufferedWriter;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+
+/**
+ *
+ * @author mike
+ */
+public class DocumentHandler {
+    
+    private String filename;
+    
+    public void load(Field field) throws IOException {
+        if (!isFilenameSet()) {
+            throw new IOException("no filename supplied");
+        }
+        // TODO: implement
+    }
+    
+    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;
+    }
+}
--- a/src/de/uapcore/sudoku/Field.java	Sat Jan 26 18:43:49 2013 +0100
+++ b/src/de/uapcore/sudoku/Field.java	Sat Jan 26 19:34:31 2013 +0100
@@ -62,7 +62,6 @@
 
     @Override
     public void paint(Graphics graphics) {
-        super.paint(graphics);
         final int w = getWidth();
         final int h = getHeight();
         final int cw = w / 9;
@@ -90,6 +89,7 @@
         }
         
         graphics.drawImage(img, 0, 0, this);
+        super.paintChildren(graphics);
     }
     
     public int getCellValue(int x, int y) {
@@ -126,4 +126,45 @@
             }
         }
     }
+    
+    public int[][] getSquare(int x, int y) {
+        if (x < 0 || x > 2 || y < 0 || y > 2) {
+            throw new IllegalArgumentException("Invalid square coordinates");
+        }
+        int[][] square = new int[3][3];
+        
+        for (int u = 0 ; u < 3 ; u++) {
+            for (int v = 0 ; v < 3 ; v++) {
+                square[u][v] = getCellValue(3*x+u, 3*y+v);
+            }
+        }
+        
+        return square;
+    }
+    
+    public int[] getRow(int y) {
+        if (y < 0 || y > 8) {
+            throw new IllegalArgumentException("Invalid row number");
+        }
+        int row[] = new int[9];
+        
+        for (int x = 0 ; x < 9 ; x++) {
+            row[x] = getCellValue(x, y);
+        }
+        
+        return row;
+    }
+    
+    public int[] getColumn(int x) {
+        if (x < 0 || x > 8) {
+            throw new IllegalArgumentException("Invalid column number");
+        }
+        int column[] = new int[9];
+        
+        for (int y = 0 ; y < 9 ; y++) {
+            column[y] = getCellValue(x, y);
+        }
+        
+        return column;
+    }
 }
--- a/src/de/uapcore/sudoku/Solver.java	Sat Jan 26 18:43:49 2013 +0100
+++ b/src/de/uapcore/sudoku/Solver.java	Sat Jan 26 19:34:31 2013 +0100
@@ -38,11 +38,11 @@
     public boolean check(Field f) {
         int line[];
         for (int i = 0 ; i < 9 ; i++) {
-            line = getRow(f, i);
+            line = f.getRow(i);
             if (!valid(line)) {
                 return false;
             }
-            line = getColumn(f, i);
+            line = f.getColumn(i);
             if (!valid(line)) {
                 return false;
             }
@@ -51,7 +51,7 @@
         int square[][];
         for (int x = 0 ; x < 3 ; x++) {
             for (int y = 0 ; y < 3 ; y++) {
-                square = getSquare(f, x, y);
+                square = f.getSquare(x, y);
                 if (!valid(square)) {
                     return false;
                 }
@@ -82,11 +82,12 @@
     }
     
     private boolean valid(int[] line) {
-        int numbers[] = new int[9];
+        int numbers[];
+        numbers = new int[9];
         for (int i = 0 ; i < 9 ; i++) {
             int l = line[i]-1;
             if (l >= 0) {
-                if (++numbers[l] > 1) {
+                if ((++numbers[l]) > 1) {
                     return false;
                 }
             }
@@ -98,51 +99,8 @@
     private boolean valid(int[][] square) {
         int[] line = new int[9];
         for (int x = 0 ; x < 3 ; x++) {
-            for (int y = 0 ; y < 3 ; y++) {
-                line[3*x+y] = square[x][y];
-            }    
+            System.arraycopy(square[x], 0, line, 3*x, 3);
         }
         return valid(line);
     }
-    
-    private int[][] getSquare(Field f, int x, int y) {
-        if (x < 0 || x > 2 || y < 0 || y > 2) {
-            throw new IllegalArgumentException("Invalid square coordinates");
-        }
-        int[][] square = new int[3][3];
-        
-        for (int u = 0 ; u < 3 ; u++) {
-            for (int v = 0 ; v < 3 ; v++) {
-                square[u][v] = f.getCellValue(3*x+u, 3*y+v);
-            }
-        }
-        
-        return square;
-    }
-    
-    private int[] getRow(Field f, int y) {
-        if (y < 0 || y > 8) {
-            throw new IllegalArgumentException("Invalid row number");
-        }
-        int row[] = new int[9];
-        
-        for (int x = 0 ; x < 9 ; x++) {
-            row[x] = f.getCellValue(x, y);
-        }
-        
-        return row;
-    }
-    
-    private int[] getColumn(Field f, int x) {
-        if (x < 0 || x > 8) {
-            throw new IllegalArgumentException("Invalid column number");
-        }
-        int column[] = new int[9];
-        
-        for (int y = 0 ; y < 9 ; y++) {
-            column[y] = f.getCellValue(x, y);
-        }
-        
-        return column;
-    }
 }

mercurial