From b4fb350fa049587972e2908e78a4caa2a197174d Mon Sep 17 00:00:00 2001 From: Gongxter Date: Fri, 20 Nov 2015 14:42:14 +0100 Subject: [PATCH] Timer now in GameController and saves --- .../sudoku/controller/GameController.java | 48 +++++++++++++++++++ .../sudoku/controller/SaveLoadStatistics.java | 14 ++++++ .../controller/helper/TimeContainer.java | 14 ++++++ .../sudoku/game/listeners/ITimerListener.java | 8 ++++ .../tu_darmstadt/sudoku/ui/GameActivity.java | 38 ++++++++------- .../sudoku/ui/view/SudokuTimer.java | 47 ------------------ app/src/main/res/layout/app_bar_game_view.xml | 2 +- 7 files changed, 106 insertions(+), 65 deletions(-) create mode 100644 app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java create mode 100644 app/src/main/java/tu_darmstadt/sudoku/controller/helper/TimeContainer.java create mode 100644 app/src/main/java/tu_darmstadt/sudoku/game/listeners/ITimerListener.java delete mode 100644 app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuTimer.java 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 648c5d0..a65fea2 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java @@ -5,6 +5,9 @@ import android.content.SharedPreferences; import java.util.LinkedList; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; +import java.util.logging.Handler; import tu_darmstadt.sudoku.controller.generator.Generator; import tu_darmstadt.sudoku.game.CellConflict; @@ -17,6 +20,7 @@ import tu_darmstadt.sudoku.game.GameType; import tu_darmstadt.sudoku.game.ICellAction; import tu_darmstadt.sudoku.game.listeners.IGameSolvedListener; import tu_darmstadt.sudoku.game.listeners.IModelChangedListener; +import tu_darmstadt.sudoku.game.listeners.ITimerListener; /** * Created by Chris on 06.11.2015. @@ -38,6 +42,11 @@ public class GameController implements IModelChangedListener { private CellConflictList errorList = new CellConflictList(); private int selectedValue; private LinkedList solvedListeners = new LinkedList<>(); + private Timer timer; + private android.os.Handler handler = new android.os.Handler(); + private TimerTask timerTask; + private int time = 0; + private LinkedList timerListeners = new LinkedList<>(); // private Solver solver; // private SudokuGenerator generator; @@ -54,6 +63,7 @@ public class GameController implements IModelChangedListener { setGameType(type); setSettings(pref); gameBoard = new GameBoard(type); + initTimer(); } public int getGameID() { @@ -429,4 +439,42 @@ public class GameController implements IModelChangedListener { l.onSolved(); } } + public void notifyTimerListener(int time) { + for (ITimerListener listener : timerListeners){ + listener.onTick(time); + } + } + + public void registerTimerListener(ITimerListener listener){ + if (!timerListeners.contains(listener)){ + timerListeners.add(listener); + } + } + + public void initTimer() { + + timerTask = new TimerTask() { + @Override + public void run() { + handler.post(new Runnable() { + @Override + public void run() { + notifyTimerListener(time++); + } + }); + + } + }; + timer = new Timer(); + } + + public void startTimer() { + + timer.scheduleAtFixedRate(timerTask,0,1000); + } + + public void pauseTimer(){ + timer.cancel(); + } + } diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java new file mode 100644 index 0000000..11e75d1 --- /dev/null +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadStatistics.java @@ -0,0 +1,14 @@ +package tu_darmstadt.sudoku.controller; + +import tu_darmstadt.sudoku.controller.helper.TimeContainer; +import tu_darmstadt.sudoku.game.GameDifficulty; + +/** + * Created by TMZ_LToP on 19.11.2015. + */ +public class SaveLoadStatistics { + //Difficulty, time, gamemode, #hints, AvTime, amountOf Games per Difficulty, + public SaveLoadStatistics(TimeContainer t,GameDifficulty difficulty,int hints){ + + } +} diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/helper/TimeContainer.java b/app/src/main/java/tu_darmstadt/sudoku/controller/helper/TimeContainer.java new file mode 100644 index 0000000..3463648 --- /dev/null +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/helper/TimeContainer.java @@ -0,0 +1,14 @@ +package tu_darmstadt.sudoku.controller.helper; + +/** + * Created by TMZ_LToP on 19.11.2015. + */ +public class TimeContainer { + int hours,minutes,seconds; + public TimeContainer(int hours, int minutes, int seconds){ + this.hours=hours; + this.minutes=minutes; + this.seconds=seconds; + } + +} diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/listeners/ITimerListener.java b/app/src/main/java/tu_darmstadt/sudoku/game/listeners/ITimerListener.java new file mode 100644 index 0000000..6f9b436 --- /dev/null +++ b/app/src/main/java/tu_darmstadt/sudoku/game/listeners/ITimerListener.java @@ -0,0 +1,8 @@ +package tu_darmstadt.sudoku.game.listeners; + +/** + * Created by TMZ_LToP on 20.11.2015. + */ +public interface ITimerListener { + public void onTick(int time); +} 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 a48aa25..c9ce34a 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/GameActivity.java @@ -17,7 +17,6 @@ import android.widget.TextView; import java.util.List; import java.util.Timer; -import java.util.TimerTask; import tu_darmstadt.sudoku.controller.SaveLoadController; import tu_darmstadt.sudoku.controller.GameController; @@ -25,20 +24,20 @@ import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; import tu_darmstadt.sudoku.game.GameDifficulty; import tu_darmstadt.sudoku.game.GameType; import tu_darmstadt.sudoku.game.listeners.IGameSolvedListener; +import tu_darmstadt.sudoku.game.listeners.ITimerListener; 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, IGameSolvedListener { +public class GameActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, IGameSolvedListener ,ITimerListener { GameController gameController; SudokuFieldLayout layout; SudokuKeyboardLayout keyboard; SudokuSpecialButtonLayout specialButtonLayout; Timer t = new Timer(); - SudokuTimer timerView; + TextView timerView; boolean isActive = true; TextView viewName ; RatingBar ratingBar; @@ -76,6 +75,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout); gameController = new GameController(sharedPref); gameController.registerGameSolvedListener(this); + gameController.registerTimerListener(this); List loadableGames = SaveLoadController.getLoadableGameList(); @@ -107,20 +107,8 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On specialButtonLayout.setButtons(p.x, gameController, keyboard); //set TimerView - timerView = (SudokuTimer)findViewById(R.id.timerView); + timerView = (TextView)findViewById(R.id.timerView); - t.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - // run every second / ever 1000 milsecs - runOnUiThread(new Runnable() { - @Override - public void run() { - if (isActive) timerView.increase(); - } - }); - } - }, 0, 1000); //set GameName viewName = (TextView) findViewById(R.id.gameModeText); @@ -145,6 +133,9 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); + + // start the game + gameController.startTimer(); } @Override @@ -242,4 +233,17 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On public void onSolved() { // TODO: WE WON.. do something awesome :) } + + @Override + public void onTick(int time) { + //do something not so awesome + int seconds = time % 60; + int minutes = ((time -seconds)/60)%60 ; + int hours = (time - minutes - seconds)/(3600); + String h,m,s; + s = (seconds< 10)? "0"+String.valueOf(seconds):String.valueOf(seconds); + m = (minutes< 10)? "0"+String.valueOf(minutes):String.valueOf(minutes); + h = (hours< 10)? "0"+String.valueOf(hours):String.valueOf(hours); + timerView.setText(h+":"+m+":"+s); + } } diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuTimer.java b/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuTimer.java deleted file mode 100644 index 4ec8dcc..0000000 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/view/SudokuTimer.java +++ /dev/null @@ -1,47 +0,0 @@ -package tu_darmstadt.sudoku.ui.view; - -import android.content.Context; -import android.util.AttributeSet; -import android.widget.TextView; - -/** - * Created by TMZ_LToP on 19.11.2015. - */ -public class SudokuTimer extends TextView { - - int seconds=0; - int mins=0; - long hours=0; - - public SudokuTimer(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public int getSeconds(){ - return seconds; - } - public int getMins(){ - return mins; - } - public long getHours(){ - return hours; - } - - public void increase () { - seconds ++; - if(seconds+1 == 60){ - seconds=0; - mins++; - }if (mins == 60) { - mins=0; - hours++; - } - String h,m,s; - s = (seconds<10)? "0"+String.valueOf(seconds) : String.valueOf(seconds); - m = (mins<10)? "0"+String.valueOf(mins) : String.valueOf(mins); - h = (hours<10)? "0"+String.valueOf(hours) : String.valueOf(hours); - StringBuilder sb = new StringBuilder(); - setText(h+":"+m+":"+s); - } - -} diff --git a/app/src/main/res/layout/app_bar_game_view.xml b/app/src/main/res/layout/app_bar_game_view.xml index a175dc4..3906391 100644 --- a/app/src/main/res/layout/app_bar_game_view.xml +++ b/app/src/main/res/layout/app_bar_game_view.xml @@ -53,7 +53,7 @@ style="?android:attr/ratingBarStyleSmall"/> -