diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index 9d52ea1e382775d3ee6ea3edaa3da27dc2c2ad85..5fe297c5ed3edbf959e3ce3c80d819225c87e9ab 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,23 @@ public class GameController { @FXML private Label hits; + @FXML + private TextField name; + + @FXML + private ListView<Score> highScores; + + private List<Score> scores; + + private AnimationTimer animationTimer; public GameController() { } public void startGame() { + scores = new LinkedList<>(); + highScores.setItems(FXCollections.observableList(scores)); this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world.setGameListener(new GameListenerImpl()); //Draw scene on a separate thread to avoid blocking UI. @@ -80,4 +106,61 @@ public class GameController { } } + + @FXML + private void gameOver() { + Random rnd = new Random(); + int value = rnd.nextInt(101); + Score score = new Score(value, name.getText()); + scores.add(score); + Collections.sort(scores,(o1, o2) -> Integer.compare(o2.getScore(), o1.getScore())); + //Collections.sort(scores, comparing((Score::getScore))); + highScores.setItems(FXCollections.observableList(scores)); + } + + @FXML + private void highScore() { + Set<Score> tempSet= new HashSet<>(scores); + /* + Iterator<Score> iter = scores.iterator(); + while (iter.hasNext()) { + Score val = iter.next(); + + + } + for(Score score: scores) { + tempSet.add(score); + } + */ + scores.clear(); + scores.addAll(tempSet); + highScores.setItems(FXCollections.observableList(scores)); + } + + @FXML + private void load() { + scores.clear(); + try (BufferedReader br = new BufferedReader(new FileReader("data.csv"))){ + String line; + while (null != (line = br.readLine())) { + String [] token = line.split(";"); + Score score = new Score(Integer.parseInt(token[1]),token[0]); + scores.add(score); + } + } catch (IOException e) { + e.printStackTrace(); + } + highScores.setItems(FXCollections.observableList(scores)); + } + + @FXML + private void save() { + try (PrintWriter pw = new PrintWriter(new FileWriter("data.csv"))) { + for (Score score: scores) { + pw.printf("%s;%d\n", score.getName(), score.getScore()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/lab/Score.java b/src/main/java/lab/Score.java new file mode 100644 index 0000000000000000000000000000000000000000..ad31f4eda10e69f5a6799d36c2fa24d17d4efcd2 --- /dev/null +++ b/src/main/java/lab/Score.java @@ -0,0 +1,41 @@ +package lab; + +final public class Score { + + private final int score; + + private final String name; + + public Score(int score, String name) { + this.score = score; + this.name = name; + } + + public int getScore() { + return score; + } + + public String getName() { + return name; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Score other) { + return name.equals(other.name); + } + return false; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + + @Override + public String toString() { + return name + ": " + score; + } + +} diff --git a/src/main/resources/lab/GameView.fxml b/src/main/resources/lab/GameView.fxml index e0732c4641c1ecbf46598064b90e1aa66c27711e..ae527f3fd1295afbe72b10f06ac8427b11f71fe8 100644 --- a/src/main/resources/lab/GameView.fxml +++ b/src/main/resources/lab/GameView.fxml @@ -5,14 +5,17 @@ <?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"> +<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController"> <bottom> <HBox alignment="CENTER" BorderPane.alignment="CENTER_LEFT"> <children> @@ -41,11 +44,25 @@ <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> + <TextField fx:id="name" /> + <Button fx:id="gameOverBtn" mnemonicParsing="false" onAction="#gameOver" prefHeight="25.0" prefWidth="114.0" text="Game over" VBox.vgrow="ALWAYS" /> + <Button fx:id="highScoresBtn" mnemonicParsing="false" onAction="#highScore" prefHeight="25.0" prefWidth="108.0" text="High scores" /> + <Button fx:id="saveScoreBtn" mnemonicParsing="false" onAction="#save" prefHeight="25.0" prefWidth="113.0" text="Save Score" /> + <Button fx:id="loadScoreBtn" mnemonicParsing="false" onAction="#load" prefHeight="25.0" prefWidth="118.0" text="Load Score" /> + </children> + </VBox> + </left> + <right> + <ListView fx:id="highScores" BorderPane.alignment="CENTER" /> + </right> </BorderPane>