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 e9b88f6..a41797c 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java @@ -141,7 +141,7 @@ public class GameController implements IModelChangedListener { boolean[][] setNotes = gic.getSetNotes(); this.gameID = gic.getID(); this.difficulty = gic.getDifficulty(); - this.time = gic.getTime(); + this.time = gic.getTimePlayed(); setGameType(gic.getGameType()); this.gameBoard = new GameBoard(gic.getGameType()); diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/QQWingController.java b/app/src/main/java/tu_darmstadt/sudoku/controller/QQWingController.java index 337f69f..95c9298 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/QQWingController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/QQWingController.java @@ -1,5 +1,6 @@ package tu_darmstadt.sudoku.controller; +import android.os.*; import android.util.Log; import java.util.LinkedList; diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java index 71c4d14..c0f6a9a 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java @@ -24,6 +24,7 @@ public class SaveLoadGameStateController { private static String FILE_EXTENSION = ".txt"; private static String SAVE_PREFIX = "save_"; private static String SAVES_DIR = "saves"; + private static final int MAX_NUM_OF_SAVED_GAMES = 10; private static List list = new LinkedList<>(); @@ -42,7 +43,7 @@ public class SaveLoadGameStateController { } File dir = context.getDir(SAVES_DIR, 0); - List result = new LinkedList<>(); + LinkedList result = new LinkedList<>(); // go through every file for(File file : dir.listFiles()) { @@ -98,7 +99,75 @@ public class SaveLoadGameStateController { editor.putBoolean("savesChanged", false); editor.commit(); - list = result; + list = sortListByLastPlayedDate(result); + + LinkedList removeList = new LinkedList<>(); + + for(int i = 0; i < list.size(); i++) { + if(i >= MAX_NUM_OF_SAVED_GAMES) { + deleteGameStateFile(list.get(i)); + removeList.add(list.get(i)); + } + } + + for(GameInfoContainer gic : removeList) { + list.remove(gic); + } + + return list; + } + + private void deleteGameStateFile(GameInfoContainer gic) { + File dir = context.getDir(SAVES_DIR, 0); + + File file = new File(dir, SAVE_PREFIX+gic.getID()+FILE_EXTENSION); + + file.delete(); + } + + public LinkedList sortListByLastPlayedDate(LinkedList list) { + + if(list.size() < 2) { + return list; + } + + LinkedList listL = new LinkedList<>(); + LinkedList listR = new LinkedList<>(); + + int middle = list.size()/2; + + for(int i = 0; i < list.size(); i++) { + if(i < middle) { + listL.add(list.get(i)); + } else { + listR.add(list.get(i)); + } + } + + listL = sortListByLastPlayedDate(listL); + listR = sortListByLastPlayedDate(listR); + + return sortListByLastPlayedDateMerge(listL, listR); + } + + public LinkedList sortListByLastPlayedDateMerge(LinkedList list1, LinkedList list2) { + + LinkedList result = new LinkedList<>(); + + while(!(list1.isEmpty() && list2.isEmpty())) { + GameInfoContainer gic1 = list1.peek(); + GameInfoContainer gic2 = list2.peek(); + if(gic1 == null) { + result.add(list2.pop()); + } else if(gic2 == null) { + result.add(list1.pop()); + } else if(gic1.getLastTimePlayed().after(gic2.getLastTimePlayed())) { + result.add(list1.pop()); + } else { + result.add(list2.pop()); + } + } + return result; } diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/helper/GameInfoContainer.java b/app/src/main/java/tu_darmstadt/sudoku/controller/helper/GameInfoContainer.java index 1c3f717..6b13da8 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/helper/GameInfoContainer.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/helper/GameInfoContainer.java @@ -51,10 +51,14 @@ public class GameInfoContainer { } } - public int getTime() { + public int getTimePlayed() { return timePlayed; } + public Date getLastTimePlayed() { + return lastTimePlayed; + } + public void parseTime(String s) { try { this.timePlayed = Integer.valueOf(s); diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java b/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java index 46e853b..88fb0e7 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java @@ -13,12 +13,19 @@ import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; +import android.widget.RatingBar; import android.widget.TextView; +import java.text.DateFormat; +import java.text.FieldPosition; +import java.text.ParsePosition; +import java.util.Date; import java.util.List; +import java.util.TimeZone; import tu_darmstadt.sudoku.controller.SaveLoadGameStateController; import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; +import tu_darmstadt.sudoku.game.GameDifficulty; import tu_darmstadt.sudoku.ui.view.R; public class LoadGameActivity extends AppCompatActivity { @@ -98,8 +105,11 @@ public class LoadGameActivity extends AppCompatActivity { GameInfoContainer gic = loadableGameList.get(position); - TextView name = (TextView)convertView.findViewById(R.id.loadgame_listentry_gametype); - TextView summary=(TextView)convertView.findViewById(R.id.loadgame_listentry_id); + TextView gameType = (TextView)convertView.findViewById(R.id.loadgame_listentry_gametype); + TextView difficulty =(TextView)convertView.findViewById(R.id.loadgame_listentry_difficultytext); + RatingBar difficultyBar =(RatingBar)convertView.findViewById(R.id.loadgame_listentry_difficultybar); + TextView playedTime = (TextView)convertView.findViewById(R.id.loadgame_listentry_timeplayed); + TextView lastTimePlayed = (TextView)convertView.findViewById(R.id.loadgame_listentry_lasttimeplayed); ImageView image = (ImageView)convertView.findViewById(R.id.loadgame_listentry_gametypeimage); switch(gic.getGameType()) { @@ -115,8 +125,26 @@ public class LoadGameActivity extends AppCompatActivity { default: image.setImageResource(R.drawable.icon_default_9x9); } - name.setText(gic.getGameType().name()); - summary.setText(String.valueOf(gic.getID())); + gameType.setText(gic.getGameType().getStringResID()); + difficulty.setText(gic.getDifficulty().getStringResID()); + difficultyBar.setRating(GameDifficulty.getValidDifficultyList().indexOf(gic.getDifficulty())+1); + + int time = gic.getTimePlayed(); + 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); + playedTime.setText(h + ":" + m + ":" + s); + + Date lastTimePlayedDate = gic.getLastTimePlayed(); + + DateFormat format = DateFormat.getDateTimeInstance(); + format.setTimeZone(TimeZone.getDefault()); + + lastTimePlayed.setText(format.format(lastTimePlayedDate)); return convertView; } diff --git a/app/src/main/res/layout/list_entry_layout.xml b/app/src/main/res/layout/list_entry_layout.xml index 00821d7..b4b7968 100644 --- a/app/src/main/res/layout/list_entry_layout.xml +++ b/app/src/main/res/layout/list_entry_layout.xml @@ -15,23 +15,61 @@ android:src="@drawable/icon_default_9x9"/> - + - + + + + + + + + + + + + + + \ No newline at end of file