summaryrefslogtreecommitdiffstats
path: root/src/se
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-03-02 09:02:29 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-03-02 09:02:29 +0100
commit5fb2af723fca2f7103db6dfca868206d0edfaf07 (patch)
treeb64421a08412a4ebdf6d404adb82b25f9a52bd07 /src/se
parent71f5dca36ec89722f921971a9846a715b178d057 (diff)
downloadtdde30-5fb2af723fca2f7103db6dfca868206d0edfaf07.tar.gz
sort highscores
Diffstat (limited to 'src/se')
-rw-r--r--src/se/liu/gusso230/tetris/Board.java8
-rw-r--r--src/se/liu/gusso230/tetris/HighscoreComparator.java11
-rw-r--r--src/se/liu/gusso230/tetris/HighscoreList.java5
3 files changed, 19 insertions, 5 deletions
diff --git a/src/se/liu/gusso230/tetris/Board.java b/src/se/liu/gusso230/tetris/Board.java
index b164792..aa3a424 100644
--- a/src/se/liu/gusso230/tetris/Board.java
+++ b/src/se/liu/gusso230/tetris/Board.java
@@ -42,12 +42,13 @@ public class Board {
tetrominoMaker = new TetrominoMaker();
this.highscores = highscores;
highscores.addHighscore(new Highscore("a", 100));
- highscores.addHighscore(new Highscore("b", 200));
highscores.addHighscore(new Highscore("c", 300));
+ highscores.addHighscore(new Highscore("b", 200));
}
public void tick() {
if (state == GameState.RUNNING) {
+ gameOver();
if (falling == null) {
createFalling();
} else {
@@ -60,6 +61,7 @@ public class Board {
private void gameOver() {
state = GameState.GAME_OVER;
highscores.addHighscore(new Highscore(JOptionPane.showInputDialog("Your name: "), points));
+ highscores.sort(new HighscoreComparator());
state = GameState.SHOWING_HIGHSCORES;
}
@@ -246,10 +248,6 @@ public class Board {
this.state = state;
}
- public void storeHighscore(String name) {
- highscores.addHighscore(new Highscore(name, points));
- }
-
/**
* Randomize all squares inside this board.
*/
diff --git a/src/se/liu/gusso230/tetris/HighscoreComparator.java b/src/se/liu/gusso230/tetris/HighscoreComparator.java
new file mode 100644
index 0000000..86b346d
--- /dev/null
+++ b/src/se/liu/gusso230/tetris/HighscoreComparator.java
@@ -0,0 +1,11 @@
+package se.liu.gusso230.tetris;
+
+import java.util.Comparator;
+
+public class HighscoreComparator implements Comparator<Highscore> {
+
+ @Override public int compare(final Highscore highscore, final Highscore other) {
+ // flip the sorting since we want large scores first
+ return Integer.compare(highscore.points, other.points) * -1;
+ }
+}
diff --git a/src/se/liu/gusso230/tetris/HighscoreList.java b/src/se/liu/gusso230/tetris/HighscoreList.java
index 31b88d7..6bd656c 100644
--- a/src/se/liu/gusso230/tetris/HighscoreList.java
+++ b/src/se/liu/gusso230/tetris/HighscoreList.java
@@ -1,6 +1,7 @@
package se.liu.gusso230.tetris;
import java.util.ArrayList;
+import java.util.Comparator;
import java.util.List;
public class HighscoreList {
@@ -17,4 +18,8 @@ public class HighscoreList {
public List<Highscore> getHighscores() {
return highscores;
}
+
+ public void sort(final Comparator<? super Highscore> c) {
+ highscores.sort(c);
+ }
}