Skip to content
Snippets Groups Projects
Commit 8b85f769 authored by koz01's avatar koz01
Browse files

solution

parent 6f5b7c83
Branches
No related merge requests found
Pipeline #87 failed with stages
in 0 seconds
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
.settings/ .settings/
.project .project
.classpath .classpath
/game-score.csv
package lab; 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.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import javafx.animation.AnimationTimer; import javafx.animation.AnimationTimer;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
public class GameController { public class GameController {
private static final String FILE_NAME = "game-score.csv";
private World world; private World world;
@FXML @FXML
...@@ -26,16 +44,30 @@ public class GameController { ...@@ -26,16 +44,30 @@ public class GameController {
@FXML @FXML
private Label hits; private Label hits;
@FXML
private TextField nameField;
@FXML
private ListView<Score> scoreList;
private List<Score> highScores;
private AnimationTimer animationTimer; private AnimationTimer animationTimer;
private final Random rnd = new Random();
public GameController() { public GameController() {
} }
public void startGame() { public void startGame() {
scoreList.setItems(FXCollections.observableList(new ArrayList<>()));
highScores = scoreList.getItems();
this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world = new World(canvas.getWidth(), canvas.getHeight());
this.world.setGameListener(new GameListenerImpl()); this.world.setGameListener(new GameListenerImpl());
//Draw scene on a separate thread to avoid blocking UI. //Draw scene on a separate thread to avoid blocking UI.
animationTimer = new DrawingThread(canvas, world); animationTimer = new DrawingThread(canvas, world);
angleSlider.valueProperty().addListener(this::angleChanged); angleSlider.valueProperty().addListener(this::angleChanged);
world.setCannonAngle(angleSlider.getValue()); world.setCannonAngle(angleSlider.getValue());
...@@ -55,6 +87,55 @@ public class GameController { ...@@ -55,6 +87,55 @@ public class GameController {
world.fire(); world.fire();
} }
@FXML
private void gameOver() {
int score = rnd.nextInt(11);
String name = nameField.getText();
highScores.add(new Score(name, score));
//highScores.sort((o1, o2)->Integer.compare(o1.getScore(), o2.getScore()));
highScores.sort(comparingInt(Score::getScore).reversed());
}
@FXML
private void highScore() {
Set<Score> tempSet = new LinkedHashSet<>();
tempSet.addAll(highScores);
highScores.clear();
highScores.addAll(tempSet);
}
@FXML
private void save() {
try(PrintWriter pw = new PrintWriter(new FileWriter(FILE_NAME))) {
for (Score score: highScores) {
pw.printf("%s;%d\n", score.getName(), score.getScore());
}
/*
Iterator<Score> iterator = highScores.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() {
highScores.clear();
try(Scanner scanner = new Scanner(new File(FILE_NAME)).useDelimiter(";\\n")) {
while(scanner.hasNext()) {
String name = scanner.next();
int score = scanner.nextInt();
highScores.add(new Score(name, score));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void angleChanged(ObservableValue<? extends Number> observable private void angleChanged(ObservableValue<? extends Number> observable
, Number oldValue, Number newValue) { , Number oldValue, Number newValue) {
......
package lab;
public class Score {
private final String name;
private final int score;
public Score(String name, int score) {
this.name = name;
this.score = score;
}
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 other) {
return this.name.equals(other.name);
}
return false;
}
@Override
public int hashCode() {
return name.hashCode();
}
}
...@@ -5,14 +5,17 @@ ...@@ -5,14 +5,17 @@
<?import javafx.scene.canvas.Canvas?> <?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Slider?> <?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?> <?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> <bottom>
<HBox alignment="CENTER" BorderPane.alignment="CENTER_LEFT"> <HBox alignment="CENTER" BorderPane.alignment="CENTER_LEFT">
<children> <children>
...@@ -41,11 +44,25 @@ ...@@ -41,11 +44,25 @@
<Label text="Shoots:" /> <Label text="Shoots:" />
<Label fx:id="shoots" text="0" GridPane.columnIndex="1" /> <Label fx:id="shoots" text="0" GridPane.columnIndex="1" />
<Label text="Hits:" GridPane.rowIndex="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> </children>
</GridPane> </GridPane>
<Canvas fx:id="canvas" height="400.0" width="600.0" /> <Canvas fx:id="canvas" height="400.0" width="600.0" />
</children> </children>
</Group> </Group>
</center> </center>
<left>
<VBox prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<TextField fx:id="nameField" />
<Button mnemonicParsing="false" onAction="#gameOver" prefWidth="100.0" text="Game Over" />
<Button mnemonicParsing="false" onAction="#highScore" prefWidth="100.0" text="High Score" />
<Button mnemonicParsing="false" onAction="#save" prefWidth="100.0" text="Save" />
<Button mnemonicParsing="false" onAction="#load" prefWidth="100.0" text="Load" />
</children>
</VBox>
</left>
<right>
<ListView fx:id="scoreList" prefWidth="200.0" BorderPane.alignment="CENTER" />
</right>
</BorderPane> </BorderPane>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment