From 40aab366240cd5a2af55dd76ec5836467ea3778c Mon Sep 17 00:00:00 2001 From: Christopher Beckmann Date: Wed, 18 Nov 2015 04:22:35 +0100 Subject: [PATCH] Hint Button is now working and will reveal the selected cell for you. Not sure if I should check the board for errors as well. --- .../sudoku/controller/GameController.java | 39 ++++++++++--------- .../sudoku/ui/view/SudokuCellView.java | 2 + .../sudoku/ui/view/SudokuFieldLayout.java | 23 +++++------ .../ui/view/SudokuSpecialButtonLayout.java | 16 +++++++- 4 files changed, 50 insertions(+), 30 deletions(-) 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 ef73a91..4d788eb 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java @@ -14,7 +14,6 @@ import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; import tu_darmstadt.sudoku.game.GameType; import tu_darmstadt.sudoku.game.ICellAction; import tu_darmstadt.sudoku.game.solver.Solver; -import tu_darmstadt.sudoku.game.solver.ISolver; /** * Created by Chris on 06.11.2015. @@ -26,6 +25,7 @@ public class GameController { private int sectionWidth; private GameBoard gameBoard; private Solver solver; + private LinkedList solvedBoards = new LinkedList<>(); private GameType gameType; private int selectedRow; private int selectedCol; @@ -141,20 +141,23 @@ public class GameController { } public LinkedList solve() { - switch(gameType) { - case Default_9x9: - case Default_6x6: - case Default_12x12: - solver = new Solver(gameBoard); - break; - default: - throw new UnsupportedOperationException("No Solver for this GameType defined."); - } + if(solvedBoards.size() == 0) { + switch (gameType) { + case Default_9x9: + case Default_6x6: + case Default_12x12: + solver = new Solver(gameBoard); + break; + default: + throw new UnsupportedOperationException("No Solver for this GameType defined."); + } - if(solver.solve(solver.getGameBoard())) { - return solver.getSolutions(); + if (solver.solve(solver.getGameBoard())) { + solvedBoards.addAll(solver.getSolutions()); + return solvedBoards; + } } - return null; + return solvedBoards; } /*public boolean loadLevel(GameBoard level) { @@ -362,19 +365,19 @@ public class GameController { } public void selectValue(int value) { - if(isCellSelected()) setValue(selectedRow, selectedCol, value); + if(isValidCellSelected()) setValue(selectedRow, selectedCol, value); } public void deleteSelectedValue() { - if(isCellSelected()) deleteValue(selectedRow, selectedCol); + if(isValidCellSelected()) deleteValue(selectedRow, selectedCol); } public void toggleSelectedNote(int value) { - if(isCellSelected()) toggleNote(selectedRow, selectedCol, value); + if(isValidCellSelected()) toggleNote(selectedRow, selectedCol, value); } - public boolean isCellSelected() { - return selectedRow != -1 && selectedCol != -1; + public boolean isValidCellSelected() { + return selectedRow != -1 && selectedCol != -1 && !getGameCell(selectedRow, selectedCol).isFixed(); } // public void registerListener(IModelChangeListener l) { diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuCellView.java b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuCellView.java index 70694ce..4e8d72b 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuCellView.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuCellView.java @@ -102,6 +102,8 @@ public class SudokuCellView extends View { case Value_Highlighted: p.setColor(Color.YELLOW); break; + case Value_Highlighted_Selected: + p.setColor(Color.CYAN); default: p.setColor(Color.WHITE); } 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 b4c6493..0d970f3 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 @@ -70,19 +70,20 @@ public class SudokuFieldLayout extends RelativeLayout { return false; } // Set connected Fields + if(gameController.isValidCellSelected()) { + //String syncConnPref = sharedPref.getString(SettingsActivity., ""); + boolean highlightConnectedRow = settings.getBoolean("pref_highlight_rows", true); + boolean highlightConnectedColumn = settings.getBoolean("pref_highlight_cols", true); + boolean highlightConnectedSection = settings.getBoolean("pref_highlight_secs", true); - //String syncConnPref = sharedPref.getString(SettingsActivity., ""); - boolean highlightConnectedRow = settings.getBoolean("pref_highlight_rows", true); - boolean highlightConnectedColumn = settings.getBoolean("pref_highlight_cols", true); - boolean highlightConnectedSection = settings.getBoolean("pref_highlight_secs", true); - - for(GameCell c : gameController.getConnectedCells(row,col, highlightConnectedRow, highlightConnectedColumn, highlightConnectedSection)) { - gamecells[c.getRow()][c.getCol()].setHighlightType(CellHighlightTypes.Connected); + for (GameCell c : gameController.getConnectedCells(row, col, highlightConnectedRow, highlightConnectedColumn, highlightConnectedSection)) { + gamecells[c.getRow()][c.getCol()].setHighlightType(CellHighlightTypes.Connected); + } + // Select touched Cell + scv.setHighlightType(CellHighlightTypes.Selected); + } else { + scv.setHighlightType(CellHighlightTypes.Value_Highlighted_Selected); } - // Select touched Cell - scv.setHighlightType(CellHighlightTypes.Selected); - - } return false; } diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuSpecialButtonLayout.java b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuSpecialButtonLayout.java index 02d5400..6a0733a 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuSpecialButtonLayout.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuSpecialButtonLayout.java @@ -8,8 +8,10 @@ import android.view.ViewGroup; import android.widget.LinearLayout; import java.util.ArrayList; +import java.util.LinkedList; import tu_darmstadt.sudoku.controller.GameController; +import tu_darmstadt.sudoku.game.GameBoard; /** * Created by TMZ_LToP on 17.11.2015. @@ -28,6 +30,9 @@ public class SudokuSpecialButtonLayout extends LinearLayout { if(v instanceof SudokuButton) { SudokuButton btn = (SudokuButton)v; + int row = gameController.getSelectedRow(); + int col = gameController.getSelectedCol(); + switch(btn.getType()) { case Delete: gameController.deleteSelectedValue(); @@ -43,7 +48,16 @@ public class SudokuSpecialButtonLayout extends LinearLayout { // TODO: not implemented break; case Hint: - // TODO: not implemented + if(gameController.isValidCellSelected()) { + LinkedList solved = gameController.solve(); + if(solved.size() >= 1) { + GameBoard solvedBoard = solved.get(0); + // test every placed value so far + + // and reveal the selected value. + gameController.selectValue(solvedBoard.getCell(row, col).getValue()); + } + } break; case NumberOrCellFirst: // TODO: not implemented