From 4bbef6da754e408e81611b9537478ad85f18417b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Tue, 2 Nov 2021 11:22:20 +0100 Subject: [PATCH] Solution for lab08 - use CSV --- .gitignore | 2 + src/main/java/lab/GameController.java | 71 ++++++++++++++++++++++++++ src/main/java/lab/Score.java | 45 ++++++++++++++++ src/main/java/lab/ScoreComparator.java | 12 +++++ src/main/resources/lab/GameView.fxml | 20 +++++++- 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/main/java/lab/Score.java create mode 100644 src/main/java/lab/ScoreComparator.java diff --git a/.gitignore b/.gitignore index db44c8a..9ba8624 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .settings/ .project .classpath +/high_score.obj +/high_score.csv diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index 2a23c20..001db37 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -1,11 +1,26 @@ package lab; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.Set; + import javafx.animation.AnimationTimer; import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.scene.canvas.Canvas; import javafx.scene.control.Label; +import javafx.scene.control.ListView; import javafx.scene.control.Slider; +import javafx.scene.control.TextField; public class GameController { @@ -26,12 +41,22 @@ public class GameController { @FXML private Label hits; + @FXML + private ListView<Score> scoreList; + + @FXML + private TextField nameTextField; + + private List<Score> highScores = new LinkedList<>(); + private AnimationTimer animationTimer; public GameController() { } public void startGame() { + initScoreList(); + this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world.setGameListener(new GameListenerImpl()); //Draw scene on a separate thread to avoid blocking UI. @@ -70,6 +95,50 @@ public class GameController { world.setCannonStrength(newValue.doubleValue()); } + @FXML + private void gameOverPressed() { + Score score = new Score(new Random().nextInt(11), nameTextField.getText()); + scoreList.getItems().add(score); + Collections.sort(scoreList.getItems(), new ScoreComparator().reversed()); + } + + @FXML + private void highScoresPressed() { + Set<Score> tempScores = new HashSet<>(); + tempScores.addAll(highScores); + highScores.clear(); + highScores.addAll(tempScores); + Collections.sort(highScores, new ScoreComparator().reversed()); + initScoreList(); + } + + @FXML + private void savePressed() throws IOException { + try(PrintWriter pw = new PrintWriter(new FileWriter("high_score.csv"))){ + for(Score score: highScores) { + pw.printf("%s;%d\n", score.getName(), score.getScore()); + } + } + } + + + @FXML + private void loadPressed() throws IOException{ + highScores.clear(); + try(BufferedReader br = new BufferedReader(new FileReader("high_score.csv"))){ + String line; + while(null != (line = br.readLine())) { + String [] tokens = line.split(";"); + highScores.add(new Score(Integer.parseInt(tokens[1]), tokens[0])); + } + } + initScoreList(); + } + + private void initScoreList() { + scoreList.setItems(FXCollections.observableList(highScores)); + } + private final class AnimationTimerImpl extends AnimationTimer { private Long previous; @@ -98,4 +167,6 @@ public class GameController { } } + + } diff --git a/src/main/java/lab/Score.java b/src/main/java/lab/Score.java new file mode 100644 index 0000000..e7f8bc7 --- /dev/null +++ b/src/main/java/lab/Score.java @@ -0,0 +1,45 @@ +package lab; + +import java.io.Serializable; + +public class Score implements Serializable{ + + private static final long serialVersionUID = 712213568495641939L; + + private final int score; + private final String name; + public Score(int score, String name) { + super(); + this.score = score; + this.name = name; + } + + + public String getName() { + return name; + } + + public int getScore() { + return score; + } + + @Override + public String toString() { + return name + ": " + score; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Score) { + Score score = (Score) obj; + return this.name.equals(score.name); + } + + return false; + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/src/main/java/lab/ScoreComparator.java b/src/main/java/lab/ScoreComparator.java new file mode 100644 index 0000000..a68e274 --- /dev/null +++ b/src/main/java/lab/ScoreComparator.java @@ -0,0 +1,12 @@ +package lab; + +import java.util.Comparator; + +public class ScoreComparator implements Comparator<Score> { + + @Override + public int compare(Score o1, Score o2) { + return Integer.compare(o1.getScore(), o2.getScore()); + } + +} diff --git a/src/main/resources/lab/GameView.fxml b/src/main/resources/lab/GameView.fxml index e0732c4..78f11fe 100644 --- a/src/main/resources/lab/GameView.fxml +++ b/src/main/resources/lab/GameView.fxml @@ -5,12 +5,15 @@ <?import javafx.scene.canvas.Canvas?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> +<?import javafx.scene.control.ListView?> <?import javafx.scene.control.Slider?> +<?import javafx.scene.control.TextField?> <?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.RowConstraints?> +<?import javafx.scene.layout.VBox?> <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController"> <bottom> @@ -41,11 +44,26 @@ <Label text="Shoots:" /> <Label fx:id="shoots" text="0" GridPane.columnIndex="1" /> <Label text="Hits:" GridPane.rowIndex="1" /> - <Label fx:id="hits" text="0" GridPane.rowIndex="1" GridPane.columnIndex="1" /> + <Label fx:id="hits" text="0" GridPane.columnIndex="1" GridPane.rowIndex="1" /> </children> </GridPane> <Canvas fx:id="canvas" height="400.0" width="600.0" /> </children> </Group> </center> + <left> + <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER"> + <children> + <Label text="Jméno:" /> + <TextField fx:id="nameTextField" promptText="Jméno" /> + <Button mnemonicParsing="false" onAction="#gameOverPressed" text="Game Over" /> + <Button mnemonicParsing="false" onAction="#highScoresPressed" text="High Score" /> + <Button mnemonicParsing="false" onAction="#savePressed" text="Save" /> + <Button mnemonicParsing="false" onAction="#loadPressed" text="Load" /> + </children> + </VBox> + </left> + <right> + <ListView fx:id="scoreList" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /> + </right> </BorderPane> -- GitLab