Skip to content
Snippets Groups Projects
Commit 4bbef6da authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Solution for lab08 - use CSV

parent c7fa6957
Branches lab08-solution
No related merge requests found
......@@ -2,3 +2,5 @@
.settings/
.project
.classpath
/high_score.obj
/high_score.csv
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 {
}
}
}
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();
}
}
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());
}
}
......@@ -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>
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