Timer now in GameController and saves
This commit is contained in:
parent
87ec3902a0
commit
b4fb350fa0
7 changed files with 106 additions and 65 deletions
|
@ -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<IGameSolvedListener> solvedListeners = new LinkedList<>();
|
||||
private Timer timer;
|
||||
private android.os.Handler handler = new android.os.Handler();
|
||||
private TimerTask timerTask;
|
||||
private int time = 0;
|
||||
private LinkedList<ITimerListener> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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){
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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<GameInfoContainer> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -53,7 +53,7 @@
|
|||
style="?android:attr/ratingBarStyleSmall"/>
|
||||
</RelativeLayout>
|
||||
|
||||
<tu_darmstadt.sudoku.ui.view.SudokuTimer
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="test"
|
||||
|
|
Loading…
Reference in a new issue