From 86581ffd8e979de439b26e6495e73df378001e38 Mon Sep 17 00:00:00 2001 From: Christopher Beckmann Date: Thu, 19 Nov 2015 14:38:41 +0100 Subject: [PATCH] Added an IGameSolvedListener. The GameActivity now listens for it. --- .../sudoku/controller/GameController.java | 29 ++++++++++++++----- .../tu_darmstadt/sudoku/game/GameBoard.java | 2 ++ .../tu_darmstadt/sudoku/game/GameCell.java | 2 ++ .../game/listeners/IGameSolvedListener.java | 11 +++++++ .../IModelChangedListener.java | 4 ++- .../tu_darmstadt/sudoku/ui/GameActivity.java | 9 +++++- .../sudoku/ui/view/SudokuCellView.java | 1 - 7 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/tu_darmstadt/sudoku/game/listeners/IGameSolvedListener.java rename app/src/main/java/tu_darmstadt/sudoku/game/{ => listeners}/IModelChangedListener.java (59%) 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 35a25d1..5facbf1 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java @@ -15,7 +15,8 @@ import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; import tu_darmstadt.sudoku.game.GameDifficulty; import tu_darmstadt.sudoku.game.GameType; import tu_darmstadt.sudoku.game.ICellAction; -import tu_darmstadt.sudoku.game.IModelChangedListener; +import tu_darmstadt.sudoku.game.listeners.IGameSolvedListener; +import tu_darmstadt.sudoku.game.listeners.IModelChangedListener; /** * Created by Chris on 06.11.2015. @@ -35,7 +36,7 @@ public class GameController implements IModelChangedListener { private int gameID = 0; private CellConflictList errorList = new CellConflictList(); private int selectedValue; - //private LinkedList listeners = new LinkedList<>(); + private LinkedList solvedListeners = new LinkedList<>(); // private Solver solver; // private SudokuGenerator generator; @@ -396,7 +397,7 @@ public class GameController implements IModelChangedListener { if(gameBoard.isFilled()) { List errorList = new LinkedList<>(); if(gameBoard.isSolved(errorList)) { - // TODO: WE WON! :D + notifySolvedListeners(); } else { // TODO: errorList now holds all the errors // TODO: display errors .. notify some view? @@ -404,9 +405,21 @@ public class GameController implements IModelChangedListener { } } -// public void notifyListeners() { -// for(IModelChangeListener l : listeners) { -// l.onModelChanged(); -// } -// } + public void registerGameSolvedListener(IGameSolvedListener l) { + if(!solvedListeners.contains(l)) { + solvedListeners.add(l); + } + } + + public void removeGameSolvedListener(IGameSolvedListener l) { + if(solvedListeners.contains(l)) { + solvedListeners.remove(l); + } + } + + public void notifySolvedListeners() { + for(IGameSolvedListener l : solvedListeners) { + l.onSolved(); + } + } } diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java b/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java index 33527da..fa481d9 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java +++ b/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java @@ -3,6 +3,8 @@ package tu_darmstadt.sudoku.game; import java.util.LinkedList; import java.util.List; +import tu_darmstadt.sudoku.game.listeners.IModelChangedListener; + /** * Created by Christopher Beckmann on 06.11.2015. */ diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/GameCell.java b/app/src/main/java/tu_darmstadt/sudoku/game/GameCell.java index eb85c2f..e3e3e15 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/game/GameCell.java +++ b/app/src/main/java/tu_darmstadt/sudoku/game/GameCell.java @@ -4,6 +4,8 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import tu_darmstadt.sudoku.game.listeners.IModelChangedListener; + /** * Created by Chris on 06.11.2015. */ diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/listeners/IGameSolvedListener.java b/app/src/main/java/tu_darmstadt/sudoku/game/listeners/IGameSolvedListener.java new file mode 100644 index 0000000..4e5ee72 --- /dev/null +++ b/app/src/main/java/tu_darmstadt/sudoku/game/listeners/IGameSolvedListener.java @@ -0,0 +1,11 @@ +package tu_darmstadt.sudoku.game.listeners; + +import tu_darmstadt.sudoku.controller.GameController; +import tu_darmstadt.sudoku.game.GameBoard; + +/** + * Created by Chris on 19.11.2015. + */ +public interface IGameSolvedListener { + public void onSolved(); +} diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/IModelChangedListener.java b/app/src/main/java/tu_darmstadt/sudoku/game/listeners/IModelChangedListener.java similarity index 59% rename from app/src/main/java/tu_darmstadt/sudoku/game/IModelChangedListener.java rename to app/src/main/java/tu_darmstadt/sudoku/game/listeners/IModelChangedListener.java index 7b1e491..dd54674 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/game/IModelChangedListener.java +++ b/app/src/main/java/tu_darmstadt/sudoku/game/listeners/IModelChangedListener.java @@ -1,4 +1,6 @@ -package tu_darmstadt.sudoku.game; +package tu_darmstadt.sudoku.game.listeners; + +import tu_darmstadt.sudoku.game.GameCell; /** * Created by Chris on 19.11.2015. diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java b/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java index 35dbc7a..d560ee1 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java @@ -22,13 +22,14 @@ import tu_darmstadt.sudoku.controller.SaveLoadController; import tu_darmstadt.sudoku.controller.GameController; import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; import tu_darmstadt.sudoku.game.GameType; +import tu_darmstadt.sudoku.game.listeners.IGameSolvedListener; import tu_darmstadt.sudoku.ui.view.R; import tu_darmstadt.sudoku.ui.view.SudokuFieldLayout; import tu_darmstadt.sudoku.ui.view.SudokuKeyboardLayout; import tu_darmstadt.sudoku.ui.view.SudokuSpecialButtonLayout; import tu_darmstadt.sudoku.ui.view.SudokuTimer; -public class GameActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { +public class GameActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, IGameSolvedListener { GameController gameController; SudokuFieldLayout layout; @@ -71,6 +72,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On //Create new GameField layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout); gameController = new GameController(sharedPref); + gameController.registerGameSolvedListener(this); List loadableGames = SaveLoadController.getLoadableGameList(); @@ -223,4 +225,9 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On drawer.closeDrawer(GravityCompat.START); return true; } + + @Override + public void onSolved() { + // TODO: WE WON.. do something awesome :) + } } 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 4d8e32a..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 @@ -12,7 +12,6 @@ import android.widget.RelativeLayout; import tu_darmstadt.sudoku.game.GameCell; import tu_darmstadt.sudoku.controller.Symbol; -import tu_darmstadt.sudoku.game.IModelChangedListener; /** * Created by TMZ_LToP on 10.11.2015.