diff --git a/.gitignore b/.gitignore index db44c8a723e017349e18633821c8ee475d15621c..2b1f99e6214e728b5fc39c99b008234adbb0787f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .settings/ .project .classpath +/score.csv diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index 9d52ea1e382775d3ee6ea3edaa3da27dc2c2ad85..bba63aa4dd3cf2542321fe5ce5218aa949111b85 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -1,11 +1,28 @@ package lab; +import static java.util.Comparator.comparingInt; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Random; +import java.util.Scanner; +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 +43,26 @@ public class GameController { @FXML private Label hits; + @FXML + private TextField nameField; + + @FXML + private ListView<Score> scoreList; + + private List<Score> highScore; + private AnimationTimer animationTimer; + + private final Random rnd; public GameController() { + rnd = new Random(); } public void startGame() { + scoreList.setItems(FXCollections.observableList(new ArrayList<>())); + highScore = scoreList.getItems(); + this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world.setGameListener(new GameListenerImpl()); //Draw scene on a separate thread to avoid blocking UI. @@ -55,6 +86,74 @@ public class GameController { world.fire(); } + @FXML + private void gameOver( ) { + int score = rnd.nextInt(11); + String name = nameField.getText(); + highScore.add(new Score(score, name)); + Collections.sort(highScore, comparingInt(Score::getScore).reversed()); + /* + Collections.sort(highScore, new Comparator<Score>() { + + @Override + public int compare(Score o1, Score o2) { + return Integer.compare(o1.getScore(), o2.getScore()); + }}.reversed()); + + Collections.sort(highScore, ((Comparator<Score>)(o1,o2)->Integer.compare(o1.getScore(), o2.getScore())).reversed() ); + + Collections.sort(highScore, (o1,o2)->Integer.compare(o2.getScore(), o1.getScore()) ); + */ + + + } + + + @FXML + private void highScore( ) { + Set<Score> tempSet = new LinkedHashSet<>(); + tempSet.addAll(highScore); + highScore.clear(); + highScore.addAll(tempSet); + } + + @FXML + private void save() { + try(PrintWriter pw = new PrintWriter(new FileWriter("score.csv"))) { + + for (Score score: highScore) { + pw.printf("%s;%d\n", score.getName(), score.getScore()); + } + /* + Iterator<Score> iterator = highScore.iterator(); + while(iterator.hasNext()) { + Score score = iterator.next(); + pw.printf("%s;%d\n", score.getName(), score.getScore()); + //iterator.remove(); + } + */ + } catch (IOException e) { + e.printStackTrace(); + } + } + + @FXML + private void load() { + highScore.clear(); + try(Scanner scanner = new Scanner(new File("score.csv")).useDelimiter("[;\\n]");){ + //String []token = line.split(";"); + while(scanner.hasNext()) { + String name = scanner.next(); + int score = scanner.nextInt(); + + highScore.add(new Score(score, name)); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + private void angleChanged(ObservableValue<? extends Number> observable , Number oldValue, Number newValue) { diff --git a/src/main/java/lab/Score.java b/src/main/java/lab/Score.java new file mode 100644 index 0000000000000000000000000000000000000000..b52aefb6ca9755f6563a4e43d4b06fafc598e320 --- /dev/null +++ b/src/main/java/lab/Score.java @@ -0,0 +1,39 @@ +package lab; + +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 String toString() { + return name + ": " + score; + } + + + @Override + public boolean equals(Object obj) { + if (obj instanceof Score score) { + return this.name.equals(score.getName()); + } + return false; + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/src/main/resources/lab/GameView.fxml b/src/main/resources/lab/GameView.fxml index e0732c4641c1ecbf46598064b90e1aa66c27711e..67183bb155c9272a41df68ebfdbdf148feb09a70 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 BorderPane.alignment="CENTER"> + <children> + <TextField fx:id="nameField" prefWidth="140.0" /> + <Button mnemonicParsing="false" onAction="#gameOver" prefWidth="140.0" text="Game Over" VBox.vgrow="ALWAYS" /> + <Button mnemonicParsing="false" onAction="#highScore" prefWidth="140.0" text="High Scores" /> + <Button mnemonicParsing="false" onAction="#save" prefWidth="140.0" text="Save" /> + <Button mnemonicParsing="false" onAction="#load" prefWidth="140.0" text="Load" /> + </children> + </VBox> + </left> + <right> + <ListView fx:id="scoreList" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /> + </right> </BorderPane>