diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d7551eb..2c81252 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="tu_darmstadt.sudoku.ui.view" > - + @@ -16,19 +16,17 @@ - + android:parentActivityName="tu_darmstadt.sudoku.ui.MainActivity"> - + + diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java index 073d851..ad7c7de 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java @@ -63,14 +63,24 @@ public class GameController { this.sectionHeight = sectionHeight; this.sectionWidth = sectionWidth; this.gameField = new GameField(size, sectionHeight, sectionWidth); + + if(fixedValues == null) throw new IllegalArgumentException("fixedValues may not be null."); + gameField.initCells(fixedValues); // now set the values that are not fixed - for(int i = 0; i < size*size; i++) { - int row = (int)Math.floor(i/size); - int col = i%size; - setValue(row, col, setValues[i]); + if (setValues != null) { + for (int i = 0; i < size * size; i++) { + int row = (int) Math.floor(i / size); + int col = i % size; + setValue(row, col, setValues[i]); + } } + + if(setNotes != null) { + // set notes. + } + } public void setSettings(SharedPreferences pref) { @@ -129,7 +139,7 @@ public class GameController { /** Use with care. */ public GameCell getGameCell(int row, int col) { - return gameField.getCell(row,col); + return gameField.getCell(row, col); } public boolean isSolved() { @@ -240,13 +250,36 @@ public class GameController { return gameField.toString(); } + public int getSelectedRow() { + return selectedRow; + } + + public int getSelectedCol() { + return selectedCol; + } + public void selectCell(int row, int col) { - this.selectedRow = row; - this.selectedCol = col; + if(selectedRow == row && selectedCol == col) { + // if we select the same field 2ce -> deselect it + selectedRow = -1; + selectedCol = -1; + } else { + // else we set it to the new selected field + selectedRow = row; + selectedCol = col; + } } public void setSelectedValue(int value) { - setValue(selectedRow, selectedCol, value); + if(selectedRow != -1 && selectedCol != -1) setValue(selectedRow, selectedCol, value); + } + + public void deleteSelectedValue() { + if(selectedRow != -1 && selectedCol != -1) setValue(selectedRow, selectedCol, 0); + } + + public void toggleSelectedNote(int value) { + if(selectedRow != -1 && selectedCol != -1) toggleNote(selectedRow, selectedCol, value); } // public void registerListener(IModelChangeListener l) { diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/GameField.java b/app/src/main/java/tu_darmstadt/sudoku/game/GameField.java index 7b1ddeb..a7a12d7 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/game/GameField.java +++ b/app/src/main/java/tu_darmstadt/sudoku/game/GameField.java @@ -51,14 +51,16 @@ public class GameField implements Cloneable { { 7, 0, 0, 0, 1, 0, 3, 0, 5 }}; // Initit the game field + int[] oneDimension = new int[size*size]; for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { - field[i][j] = new GameCell(i,j,size,placeholder[i][j]); + oneDimension[i*size+j] = placeholder[i][j]; } } } public void initCells(int[] level) { + int count = 0; if(level.length != size*size) { throw new IllegalArgumentException("Levelarray must have length of "+size*size+"."); } @@ -66,8 +68,10 @@ public class GameField implements Cloneable { for(int i = 0; i < size*size; i++) { int row = (int)Math.floor(i/size); int col = i%size; + if(level[i] != 0) count++; field[row][col] = new GameCell(row,col,size,level[i]); } + if(count < 17) throw new IllegalArgumentException("There must be at least 17 fixed values."); } public GameCell getCell(int row, int col) { diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuFieldLayout.java b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuFieldLayout.java index cefd49d..9807264 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuFieldLayout.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuFieldLayout.java @@ -53,12 +53,22 @@ public class SudokuFieldLayout extends RelativeLayout { int row = scv.getRow(); int col = scv.getCol(); + gameController.selectCell(row, col); + row = gameController.getSelectedRow(); + col = gameController.getSelectedCol(); + // Reset everything for(int i = 0; i < gameController.getSize(); i++) { for(int j = 0; j < gameController.getSize(); j++) { gamecells[i][j].setHighlightType(CellHighlightTypes.Default); } } + + if(row == -1 || col == -1) { + // we clicked on the same cell 2 times. + // means it got deselected and we dont highlight any cells. + return false; + } // Set connected Fields //String syncConnPref = sharedPref.getString(SettingsActivity., ""); @@ -70,7 +80,6 @@ public class SudokuFieldLayout extends RelativeLayout { gamecells[c.getRow()][c.getCol()].setHighlightType(CellHighlightTypes.Connected); } // Select touched Cell - gameController.selectCell(row, col); scv.setHighlightType(CellHighlightTypes.Selected); diff --git a/app/src/test/java/tu_darmstadt/sudoku/game/solver/SolverTest.java b/app/src/test/java/tu_darmstadt/sudoku/game/solver/SolverTest.java index 9745627..1217f11 100644 --- a/app/src/test/java/tu_darmstadt/sudoku/game/solver/SolverTest.java +++ b/app/src/test/java/tu_darmstadt/sudoku/game/solver/SolverTest.java @@ -26,6 +26,8 @@ public class SolverTest { { 0, 0, 0, 0, 7, 0, 4, 0, 9 }, { 6, 4, 0, 0, 0, 0, 0, 0, 0 }, { 7, 0, 0, 0, 1, 0, 3, 0, 5 }}; + + controller.loadLevel(9,3,3,new int[]{0,0,0,0,4,1,0,0,0,0,6,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,2,0,6,0,0,0,0,0,0,0,0,0,5,0,0,4,1,7,0,0,0,0,0,0,0,0,0,0,0,2,0,0,3,0,0,0,4,8,0,0,0,0,0,0,5,0,1,0,0,0,0,0,0},null,null); } //000041000060000200000000000320600000000050041700000000000200300048000000501000000