Skip to content

Commit b092a94

Browse files
committed
Small sout refactor
1 parent ac1c981 commit b092a94

31 files changed

+750
-757
lines changed

src/main/java/com/oopproject/Main.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
import com.oopproject.gui.MainWindow;
44
import javafx.application.Application;
55

6-
/**
7-
* Main class of the application.
8-
* @author Michal
9-
*/
106
public class Main {
11-
/**
12-
* Main method of the application.
13-
* @param args arguments of the application
14-
*/
157
public static void main(String[] args){
168
Application.launch(MainWindow.class, args);
179
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.oopproject;
2+
3+
import com.oopproject.world.locations.Location;
4+
import com.oopproject.world.locations.Path;
5+
import javafx.util.Pair;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.PriorityQueue;
10+
11+
public class Pathfinder {
12+
private HashMap<Pair<Integer, Integer>, Location> map_locations;
13+
public Pathfinder(HashMap<Pair<Integer, Integer>, Location> map_locations){
14+
this.map_locations = map_locations;
15+
}
16+
private int heuristic(int x1, int y1, int x2, int y2){
17+
// euclidean distance
18+
return (int) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
19+
}
20+
private int cost(int x1, int y1, int x2, int y2){
21+
if (this.map_locations.containsKey(new Pair<>(x2, y2))){
22+
if (this.map_locations.get(new Pair<>(x2, y2)) instanceof Path){
23+
return 10;
24+
}
25+
else{
26+
return 10000;
27+
}
28+
}
29+
else{
30+
return 3000;
31+
}
32+
}
33+
public ArrayList<Pair<Integer, Integer>> findPath(int x1, int y1, int x2, int y2){
34+
//returns path which is comprised of pairs of coordinates
35+
ArrayList <Pair<Integer, Integer>> path = new ArrayList<>();
36+
// find path from p1 to p1, and make it look like manhattan distance
37+
// assign very high cost to already occupied cells by other things that path,
38+
// assign very low cost to these nodes
39+
// implementation:
40+
class Tuple implements Comparable<Tuple>{
41+
int x;
42+
int y;
43+
int cost;
44+
int heuristic;
45+
int total;
46+
Tuple parent;
47+
public Tuple(int x, int y, int cost, int heuristic, Tuple parent){
48+
this.x = x;
49+
this.y = y;
50+
this.cost = cost;
51+
this.heuristic = heuristic;
52+
this.total = cost + heuristic;
53+
this.parent = parent;
54+
}
55+
@Override
56+
public int compareTo(Tuple o) {
57+
return this.total - o.total;
58+
}
59+
}
60+
PriorityQueue<Tuple> queue = new PriorityQueue<>();
61+
HashMap<Pair<Integer, Integer>, Tuple> visited = new HashMap<>();
62+
queue.add(new Tuple(x1, y1, 0, heuristic(x1, y1, x2, y2), null));
63+
while (!queue.isEmpty()){
64+
Tuple t = queue.poll();
65+
if (t.x == x2 && t.y == y2){
66+
//found path
67+
while (t.parent != null){
68+
path.add(new Pair<>(t.x, t.y));
69+
t = t.parent;
70+
}
71+
break;
72+
}
73+
if (!visited.containsKey(new Pair<>(t.x, t.y))){
74+
visited.put(new Pair<>(t.x, t.y), t);
75+
if (t.x > 0){
76+
queue.add(new Tuple(t.x - 1, t.y, t.cost + cost(t.x, t.y, t.x - 1, t.y), heuristic(t.x - 1, t.y, x2, y2), t));
77+
}
78+
if (t.x < 24){
79+
queue.add(new Tuple(t.x + 1, t.y, t.cost + cost(t.x, t.y, t.x + 1, t.y), heuristic(t.x + 1, t.y, x2, y2), t));
80+
}
81+
if (t.y > 0){
82+
queue.add(new Tuple(t.x, t.y - 1, t.cost + cost(t.x, t.y, t.x, t.y - 1), heuristic(t.x, t.y - 1, x2, y2), t));
83+
}
84+
if (t.y < 24){
85+
queue.add(new Tuple(t.x, t.y + 1, t.cost + cost(t.x, t.y, t.x, t.y + 1), heuristic(t.x, t.y + 1, x2, y2), t));
86+
}
87+
}
88+
}
89+
return path;
90+
}
91+
}

src/main/java/com/oopproject/gui/MainWindow.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,27 @@
55
import javafx.event.EventHandler;
66
import javafx.scene.Scene;
77
import javafx.stage.Stage;
8+
89
import java.io.IOException;
910
import javafx.fxml.FXMLLoader;
1011
import javafx.stage.Screen;
1112
import javafx.stage.WindowEvent;
1213

1314
/**
1415
* Main window of the application.
15-
* @author Michal
16-
*
1716
*/
1817
public class MainWindow extends Application {
1918
/**
2019
* Entry point starting the application.
21-
* @param stage the stage to be shown
20+
* @param stage
2221
* @throws IOException
2322
*/
2423
@Override
2524
public void start(Stage stage) throws IOException {
2625
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("MainWindow.fxml"));
27-
double windowSize = (double)((int)Screen.getPrimary().getBounds().getHeight()*0.9);
28-
Scene scene = new Scene(fxmlLoader.load(), windowSize*1.1, windowSize);
29-
stage.setTitle("Fallout 0");
30-
stage.getIcons().add(new javafx.scene.image.Image("file:src/main/resources/images/icon.png"));
26+
double window_size = (double)((int)Screen.getPrimary().getBounds().getHeight()) * 0.8;
27+
Scene scene = new Scene(fxmlLoader.load(), window_size*1.1, window_size);
28+
stage.setTitle("OOP Project");
3129
stage.setScene(scene);
3230
stage.setResizable(false);
3331
stage.show();
@@ -37,7 +35,7 @@ public void handle(WindowEvent e) {
3735
// get the controller
3836
MainWindowController controller = fxmlLoader.getController();
3937
// stop the simulation
40-
//controller.terminateSimulation();
38+
controller.terminateSimulation();
4139
Platform.exit();
4240
System.exit(0);
4341
}
Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
package com.oopproject.gui;
22

3-
import com.oopproject.world.animals.Animal;
4-
import com.oopproject.world.animals.Prey;
53
import javafx.fxml.FXML;
64
import javafx.fxml.Initializable;
75
import javafx.scene.canvas.Canvas;
86
import javafx.scene.control.TextField;
97
import javafx.scene.control.Button;
108
import javafx.scene.control.Label;
119
import javafx.scene.control.TitledPane;
12-
import javafx.scene.image.Image;
10+
import javafx.scene.layout.VBox;
1311
import javafx.stage.Screen;
1412
import com.oopproject.world.World;
15-
import javafx.util.Pair;
13+
1614
import java.net.URL;
1715
import java.util.ResourceBundle;
1816

1917
/**
20-
* The main window controller. Managed by The Institute, so watch out for any synth attacks.
18+
* The main window controller.
2119
*/
2220
public class MainWindowController implements Initializable {
2321
private Mapper animalMapper;
24-
private World welcomeToTheJungle;
22+
private World welcometothejungle;
2523
@FXML
2624
private TextField animalTextField;
25+
26+
@FXML
27+
private VBox mainVBox;
28+
2729
@FXML
2830
private Canvas mapCanvas;
31+
2932
@FXML
30-
private Button startButton;
33+
private Button preyButton;
34+
3135
@FXML
32-
private Label objectLabel;
36+
private Button predatorButton;
37+
3338
@FXML
34-
private Canvas objectCanvas;
39+
private Button startButton;
40+
3541
@FXML
36-
private Canvas bombsCanvas;
42+
private Label propertiesViewLabel;
43+
3744
@FXML
3845
private TitledPane mapTitledPane;
39-
private Pair<Integer, Integer> selection;
40-
private Animal selectedAnimal;
41-
private Image nukaImage;
42-
private Image bombImage;
4346

4447
/**
4548
* Initializes the controller class.
@@ -48,94 +51,45 @@ public class MainWindowController implements Initializable {
4851
*/
4952
@Override
5053
public void initialize(URL url, ResourceBundle resourceBundle) {
51-
double height = (double)((int) Screen.getPrimary().getBounds().getHeight()*0.9- 315);
54+
double height = (double)((int) Screen.getPrimary().getBounds().getHeight() * 0.8 - 220);
5255
mapTitledPane.setPrefHeight(height);
5356
mapTitledPane.setMaxHeight(height);
54-
nukaImage = new Image("file:src/main/resources/images/nuka.png");
55-
//draw the image on the canvas, keeping the aspect ratio of the image
56-
objectCanvas.getGraphicsContext2D().drawImage(nukaImage, 0, 0, objectCanvas.getWidth(), objectCanvas.getHeight());
57-
bombImage = new Image("file:src/main/resources/images/bombs.png");
58-
bombsCanvas.getGraphicsContext2D().drawImage(bombImage, 0, 0, bombsCanvas.getWidth(), bombsCanvas.getHeight());
5957
animalMapper = new Mapper(mapCanvas, height);
60-
welcomeToTheJungle = new World(animalMapper);
61-
getClick();
62-
// start the music
63-
}
64-
65-
/**
66-
* Gets the postion of the click on the map and displays the animal in the given box.
67-
*/
68-
public void getClick(){
69-
mapCanvas.setOnMouseClicked(mouseEvent -> {
70-
selection = new Pair<>((int) (mouseEvent.getX() / animalMapper.getxStep()), (int) (mouseEvent.getY() / animalMapper.getyStep()));
71-
objectLabel.setText(welcomeToTheJungle.getObjectInfo(selection.getKey(), selection.getValue()));
72-
for (int i = 0; i < welcomeToTheJungle.getAnimals().size(); i++) {
73-
if (welcomeToTheJungle.getAnimals().get(i).getX() == selection.getKey() && welcomeToTheJungle.getAnimals().get(i).getY() == selection.getValue()) {
74-
selectedAnimal = welcomeToTheJungle.getAnimals().get(i);
75-
break;
76-
}
77-
}
78-
});
58+
welcometothejungle = new World(animalMapper);
7959
}
8060

81-
/**
82-
* Summon Conrad Kellogg. He will handle the rest.
83-
*/
84-
public void killAnimal(){
85-
selectedAnimal.setHealth(0);
86-
}
87-
/**
88-
* Reroute the animal to previously selected position.
89-
*/
90-
public void rerouteAnimal(){
91-
if (selectedAnimal instanceof Prey) {
92-
((Prey) selectedAnimal).reroute(selection.getKey(), selection.getValue());
93-
}
94-
}
9561
/**
9662
* Start/stop button handler.
9763
*/
9864
public void start(){
9965
if (startButton.getText().equals("START")){
10066
startButton.setText("STOP");
101-
welcomeToTheJungle.run();
67+
welcometothejungle.run();
10268
}
10369
else{
10470
startButton.setText("START");
105-
welcomeToTheJungle.stop();
71+
welcometothejungle.stop();
10672
}
10773
}
10874

10975
/**
11076
* Add prey button handler.
11177
*/
11278
public void addPrey(){
113-
if (animalTextField.getText().equals("")){
114-
//glow border red
115-
animalTextField.setStyle("-fx-border-color: red ; -fx-border-width: 2px ;");
116-
}
117-
else{
118-
welcomeToTheJungle.addPrey(animalTextField.getText());
119-
animalTextField.setText("");
120-
//return to normal
121-
animalTextField.setStyle("-fx-border-color: transparent ; -fx-border-width: 0px ;");
122-
}
123-
79+
welcometothejungle.addPrey(animalTextField.getText());
12480
}
12581

12682
/**
12783
* Add predator button handler.
12884
*/
12985
public void addPredator(){
130-
if (animalTextField.getText().equals("")){
131-
//glow border red
132-
animalTextField.setStyle("-fx-border-color: red ; -fx-border-width: 2px ;");
133-
}
134-
else{
135-
welcomeToTheJungle.addPredator(animalTextField.getText());
136-
animalTextField.setText("");
137-
//return to normal
138-
animalTextField.setStyle("-fx-border-color: transparent ; -fx-border-width: 0px ;");
139-
}
86+
welcometothejungle.addPredator(animalTextField.getText());
87+
}
88+
89+
/**
90+
* Terminate the simulation.
91+
*/
92+
public void terminateSimulation(){
93+
welcometothejungle.terminate();
14094
}
14195
}

0 commit comments

Comments
 (0)