summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-03-02 11:02:50 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-03-02 11:02:50 +0100
commit547f5d5fce350b6889d1165eb4387c16e0de2688 (patch)
treece2906ffa3dfe68084f058c0033cba4c79cb05f0 /src
parent5fa3dbd488523dd02c9363c2f3598363971674eb (diff)
downloadtdde30-547f5d5fce350b6889d1165eb4387c16e0de2688.tar.gz
analysis
Diffstat (limited to 'src')
-rw-r--r--src/se/liu/gusso230/tetris/Board.java20
-rw-r--r--src/se/liu/gusso230/tetris/Highscore.java20
-rw-r--r--src/se/liu/gusso230/tetris/HighscoreComparator.java2
-rw-r--r--src/se/liu/gusso230/tetris/HighscoreList.java11
-rw-r--r--src/se/liu/gusso230/tetris/TetrisComponent.java8
-rw-r--r--src/se/liu/gusso230/tetris/TetrisViewer.java4
6 files changed, 48 insertions, 17 deletions
diff --git a/src/se/liu/gusso230/tetris/Board.java b/src/se/liu/gusso230/tetris/Board.java
index f405884..56e39a6 100644
--- a/src/se/liu/gusso230/tetris/Board.java
+++ b/src/se/liu/gusso230/tetris/Board.java
@@ -4,6 +4,7 @@ import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@@ -36,6 +37,11 @@ public class Board {
private final static Random RND = new Random();
+ private final static int POINTS_ONE_LINE = 100;
+ private final static int POINTS_TWO_LINES = 300;
+ private final static int POINTS_THREE_LINES = 500;
+ private final static int POINTS_FOUR_LINES = 800;
+
public Board(int width, int height, HighscoreList highscores) {
this.height = height;
this.width = width;
@@ -87,7 +93,7 @@ public class Board {
try {
highscores.saveToFile(highscoreFile);
return;
- } catch (FileNotFoundException | SecurityException ignored) {
+ } catch (SecurityException | IOException ignored) {
if (JOptionPane.showConfirmDialog(null,
"Couldn't write highscore file. Try again?",
"",
@@ -100,7 +106,7 @@ public class Board {
}
}
- private void gameOver() {
+ private void setGameOver() {
state = GameState.GAME_OVER;
//highscores.addHighscore(new Highscore(JOptionPane.showInputDialog("Your name: "), points));
highscores.addHighscore(new Highscore("woo", points));
@@ -124,7 +130,7 @@ public class Board {
fallingX = (width - falling.getBoundingBoxSize()) / 2;
fallingY = -1;
if (hasFallingCollision()) {
- gameOver();
+ setGameOver();
}
notifyListeners();
}
@@ -201,16 +207,16 @@ public class Board {
}
switch (clearedLines) {
case 1:
- points += 100;
+ points += POINTS_ONE_LINE;
break;
case 2:
- points += 300;
+ points += POINTS_TWO_LINES;
break;
case 3:
- points += 500;
+ points += POINTS_THREE_LINES;
break;
case 4:
- points += 800;
+ points += POINTS_FOUR_LINES;
break;
default:
break;
diff --git a/src/se/liu/gusso230/tetris/Highscore.java b/src/se/liu/gusso230/tetris/Highscore.java
index 83d697b..596ee5d 100644
--- a/src/se/liu/gusso230/tetris/Highscore.java
+++ b/src/se/liu/gusso230/tetris/Highscore.java
@@ -1,8 +1,8 @@
package se.liu.gusso230.tetris;
public class Highscore {
- String name;
- int points;
+ private String name = "";
+ private int points = 0;
public Highscore() {}
@@ -10,4 +10,20 @@ public class Highscore {
this.name = name;
this.points = points;
}
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+ public int getPoints() {
+ return points;
+ }
+
+ public void setPoints(final int points) {
+ this.points = points;
+ }
}
diff --git a/src/se/liu/gusso230/tetris/HighscoreComparator.java b/src/se/liu/gusso230/tetris/HighscoreComparator.java
index 86b346d..0c208ca 100644
--- a/src/se/liu/gusso230/tetris/HighscoreComparator.java
+++ b/src/se/liu/gusso230/tetris/HighscoreComparator.java
@@ -6,6 +6,6 @@ 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;
+ return Integer.compare(highscore.getPoints(), other.getPoints()) * -1;
}
}
diff --git a/src/se/liu/gusso230/tetris/HighscoreList.java b/src/se/liu/gusso230/tetris/HighscoreList.java
index 3ae14f9..27543e9 100644
--- a/src/se/liu/gusso230/tetris/HighscoreList.java
+++ b/src/se/liu/gusso230/tetris/HighscoreList.java
@@ -4,7 +4,11 @@ import com.google.gson.Gson;
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.PrintWriter;
+import java.nio.file.DirectoryNotEmptyException;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -22,11 +26,14 @@ public class HighscoreList {
highscores.add(highscore);
}
- public void saveToFile(File file) throws FileNotFoundException, SecurityException {
- try (PrintWriter writer = new PrintWriter(file)) {
+ public void saveToFile(File file) throws FileNotFoundException, SecurityException, IOException, DirectoryNotEmptyException {
+ File tmpFile = new File(".highscores.json.tmp");
+ try (PrintWriter writer = new PrintWriter(tmpFile)) {
Gson gson = new Gson();
writer.println(gson.toJson(this));
}
+
+ Files.move(tmpFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
public void readFromFile(File file) throws FileNotFoundException, NoSuchElementException {
diff --git a/src/se/liu/gusso230/tetris/TetrisComponent.java b/src/se/liu/gusso230/tetris/TetrisComponent.java
index 52fc1ed..9d8d704 100644
--- a/src/se/liu/gusso230/tetris/TetrisComponent.java
+++ b/src/se/liu/gusso230/tetris/TetrisComponent.java
@@ -9,6 +9,8 @@ import java.util.List;
public class TetrisComponent extends JComponent implements BoardListener {
private static final int TILE_SIZE = 28;
private static final int TILE_GAP = 4;
+ private static final int LEFT_MARGIN = 10;
+ private static final int LINE_HEIGHT = 40;
private static final EnumMap<SquareType, Color> SQUARE_TYPE_COLORS = getDefaultColors();
private Board board;
@@ -68,15 +70,15 @@ public class TetrisComponent extends JComponent implements BoardListener {
g.setColor(Color.BLACK);
g.setFont(new Font("serif", Font.PLAIN, 22));
- g.drawString(Integer.toString(board.getPoints()), 10, 40);
+ g.drawString(Integer.toString(board.getPoints()), LEFT_MARGIN, LINE_HEIGHT);
break;
case SHOWING_HIGHSCORES:
List<Highscore> highscores = board.getHighscores();
g.setColor(Color.BLACK);
g.setFont(new Font("serif", Font.PLAIN, 22));
- g.drawString("Highscores: ", 10, 40);
+ g.drawString("Highscores: ", LEFT_MARGIN, LINE_HEIGHT);
for (int i = 0; i < highscores.size(); i++) {
- g.drawString(String.format("%s: %d", highscores.get(i).name, highscores.get(i).points), 10, 40 * (i + 2));
+ g.drawString(String.format("%s: %d", highscores.get(i).getName(), highscores.get(i).getPoints()), LEFT_MARGIN, LINE_HEIGHT * (i + 2));
}
break;
case GAME_OVER:
diff --git a/src/se/liu/gusso230/tetris/TetrisViewer.java b/src/se/liu/gusso230/tetris/TetrisViewer.java
index 3ba6e0f..bb65734 100644
--- a/src/se/liu/gusso230/tetris/TetrisViewer.java
+++ b/src/se/liu/gusso230/tetris/TetrisViewer.java
@@ -11,14 +11,14 @@ public class TetrisViewer {
private Board board;
public TetrisViewer(final Board board) {
- menuBar = setupMenuBar();
+ menuBar = setUpMenuBar();
imageComponent = new ImageComponent("images/hello_world.png");
tetrisComponent = new TetrisComponent(board);
this.board = board;
board.addListener(tetrisComponent);
}
- private JMenuBar setupMenuBar() {
+ private JMenuBar setUpMenuBar() {
final JMenuBar menuBar = new JMenuBar();
final JMenu file = new JMenu("File");
final JMenuItem quit = new JMenuItem("Quit");