Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion oolab/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
plugins {
id 'application'
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.13'
// id 'org.openjfx.javafxplugin' version '0.1.0'
}


group = 'org.example'
version = '1.0-SNAPSHOT'

repositories {
mavenCentral()
javafx {
version = "17"
modules = ['javafx.base', 'javafx.controls', 'javafx.fxml', 'javafx.graphics', 'javafx.media', 'javafx.swing', 'javafx.web']
}
}

dependencies {
Expand All @@ -20,7 +27,8 @@ test {
}

application {
getMainClass().set('agh.ics.oop.World')
getMainClass().set('agh.ics.oop.Main')

}

java {
Expand Down
10 changes: 10 additions & 0 deletions oolab/src/main/java/agh/ics/oop/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//package agh.ics.oop;
//
//import javafx.application.Application;
//
//public class Main {
//
// public static void main(String[] args) {
// Application.launch(SimulationApp.class, args);
// }
//}
4 changes: 3 additions & 1 deletion oolab/src/main/java/agh/ics/oop/OptionsParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package agh.ics.oop;

import agh.ics.oop.model.MoveDirection;
import agh.ics.oop.model.exceptions.IllegalMoveSpecificationException;

import java.util.List;
import java.util.ArrayList;

Expand All @@ -14,7 +16,7 @@ public static List<MoveDirection> parse(String[] input) {
case "b" -> moves.add(MoveDirection.BACKWARD);
case "r" -> moves.add(MoveDirection.RIGHT);
case "l" -> moves.add(MoveDirection.LEFT);
default -> throw new IllegalArgumentException(letter + " is not legal move specification");
default -> throw new IllegalMoveSpecificationException(letter);
}
}
return moves;
Expand Down
15 changes: 12 additions & 3 deletions oolab/src/main/java/agh/ics/oop/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Collections;
import java.util.List;

public class Simulation {
public class Simulation implements Runnable {
public static final String ANIMAL_STRING = "Animal";
private final List<Vector2d> positions;
private final List<MoveDirection> moves;
Expand All @@ -17,7 +17,7 @@ public Simulation(List<Vector2d> positions, List<MoveDirection> moves, WorldMap
this.positions = new ArrayList<>(positions); //zeby dało sie usuwać
this.moves = moves;
this.worldMap = worldMap;
fillWorldMap();
// fillWorldMap();
}

private void fillWorldMap() {
Expand All @@ -27,8 +27,11 @@ private void fillWorldMap() {
Animal animal = new Animal(position);
try {
worldMap.place(animal);
Thread.sleep(750);
}catch (IncorrectPositionException e){
indicesToRemove.add(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
indicesToRemove.sort(Collections.reverseOrder());
Expand All @@ -49,13 +52,19 @@ public List<Vector2d> getPositions() {
public void run(){
int animalsIndex;
Vector2d currPos;

fillWorldMap();
for (int i = 0; i < moves.size(); i++) {
animalsIndex = i % positions.size();
currPos = positions.get(animalsIndex); //pozycja zwierzaka, którego bedziemy chcieli przenieść
WorldElement animalAtCurrPos = worldMap.objectAt(currPos);
worldMap.move((Animal) animalAtCurrPos, moves.get(i)); //próba przeniesienia
positions.set(animalsIndex, animalAtCurrPos.getPos()); //zaktualizuj pozycje
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}
61 changes: 61 additions & 0 deletions oolab/src/main/java/agh/ics/oop/SimulationApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package agh.ics.oop;

import agh.ics.oop.model.*;
import agh.ics.oop.model.exceptions.IllegalMoveSpecificationException;
import agh.ics.oop.presenter.SimulationPresenter;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class SimulationApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getClassLoader().getResource("simulation.fxml"));
BorderPane viewRoot = loader.load();
SimulationPresenter presenter = loader.getController();

configureStage(primaryStage, viewRoot);

primaryStage.show();





// java.util.List<MoveDirection> directions = null;
// try {
// directions = List.of(MoveDirection.FORWARD, MoveDirection.BACKWARD, MoveDirection.FORWARD, MoveDirection.LEFT, MoveDirection.FORWARD, MoveDirection.RIGHT);
// }
// catch (IllegalMoveSpecificationException e) {
// e.printStackTrace();
// System.err.println("error: " + e.getMessage());
// return;
// }
// java.util.List<Vector2d> positions = java.util.List.of(new Vector2d(2,2), new Vector2d(3,4));
// List<Simulation> simulations = new ArrayList<>();
// AbstractWorldMap grassF = new GrassField(10);
// grassF.addObserver(new ConsoleMapDisplay());
// SimulationPresenter simPresenter = loader.getController();
// simPresenter.setWorldMap(grassF);
// grassF.addObserver(simPresenter);
// simulations.add(new Simulation(positions,directions,grassF));
//
// SimulationEngine simEngine = new SimulationEngine(simulations);
// simEngine.runSync();
// simEngine.runAsyncInThreadPool();
}
private void configureStage(Stage primaryStage, BorderPane viewRoot) {
var scene = new Scene(viewRoot);
primaryStage.setScene(scene);
primaryStage.setTitle("Simulation app");
primaryStage.minWidthProperty().bind(viewRoot.minWidthProperty());
primaryStage.minHeightProperty().bind(viewRoot.minHeightProperty());
}
}
29 changes: 22 additions & 7 deletions oolab/src/main/java/agh/ics/oop/World.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
package agh.ics.oop;
import agh.ics.oop.model.*;
import agh.ics.oop.model.exceptions.IllegalMoveSpecificationException;
import javafx.application.Application;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class World {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
List<MoveDirection> directions = null;
try {
directions = OptionsParser.parse(args);
}
catch (IllegalArgumentException e) {
catch (IllegalMoveSpecificationException e) {
e.printStackTrace();
System.err.println("error: " + e.getMessage());
return;
}
List<Vector2d> positions = List.of(new Vector2d(2,2), new Vector2d(3,4));
AbstractWorldMap grassField = new GrassField(10);
ConsoleMapDisplay observer1 = new ConsoleMapDisplay();
grassField.addObserver(observer1);
Simulation simulation = new Simulation(positions, directions, grassField);
simulation.run();
List<Simulation> simulations = new ArrayList<>();
for (int i = 0; i < 1000;i++){
AbstractWorldMap grassF = new GrassField(10);
grassF.addObserver(new ConsoleMapDisplay());
AbstractWorldMap rectangularF = new RectangularMap(5,5);
rectangularF.addObserver(new ConsoleMapDisplay());
simulations.add(new Simulation(positions,directions,rectangularF));
simulations.add(new Simulation(positions,directions,grassF));
}
SimulationEngine simEngine = new SimulationEngine(simulations);
// simEngine.runSync();
simEngine.runAsyncInThreadPool();
System.out.println("system zakonczyl dzialanie");
// SimulationApp simulationApp = new SimulationApp();
// Stage stage = new Stage();
// simulationApp.start(stage);
}
public static void run(MoveDirection[] input){
for (MoveDirection arg : input) {
Expand Down
10 changes: 10 additions & 0 deletions oolab/src/main/java/agh/ics/oop/WorldGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package agh.ics.oop;

import javafx.application.Application;

public class WorldGUI {
public static void main(String[] args) {
//to co normalnie bym w World robil teraz powinno byc w WorldGUI
Application.launch(SimulationApp.class, args);
}
}
4 changes: 4 additions & 0 deletions oolab/src/main/java/agh/ics/oop/model/AbstractWorldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static agh.ics.oop.Simulation.ANIMAL_STRING;

public abstract class AbstractWorldMap implements WorldMap{
private final UUID id = UUID.randomUUID();
protected final Map<Vector2d, Animal> animals = new HashMap<>(); //animals jest w obu mapach
public abstract Boundary getCurrentBounds();
public abstract boolean canMoveTo(Vector2d position);
Expand Down Expand Up @@ -90,5 +91,8 @@ public List<WorldElement> getElements(){
return new ArrayList<>(animals.values());
}

public UUID getId(){
return id;
}
}

10 changes: 6 additions & 4 deletions oolab/src/main/java/agh/ics/oop/model/ConsoleMapDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ public class ConsoleMapDisplay implements MapChangeListener{
private int updateCnt = 0;
@Override
public void mapChanged(WorldMap worldMap, String message) {
updateCnt++;
System.out.println(message);
System.out.println(worldMap);
System.out.println("%s %s: %d\n\n".formatted(UPDATE_STRING, COUNT_STRING, updateCnt));
synchronized (System.out) {
updateCnt++;
System.out.println(message);
System.out.println(worldMap);
System.out.println("%s %s: %d ID:%s\n\n".formatted(UPDATE_STRING, COUNT_STRING, updateCnt, worldMap.getId()));
}
}
}
2 changes: 1 addition & 1 deletion oolab/src/main/java/agh/ics/oop/model/RectangularMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean canMoveTo(Vector2d position) {

@Override
public Boundary getCurrentBounds(){
return new Boundary(upperRightBoundary, lowerLeftBoundary);
return new Boundary(lowerLeftBoundary, upperRightBoundary);
}


Expand Down
60 changes: 60 additions & 0 deletions oolab/src/main/java/agh/ics/oop/model/SimulationEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package agh.ics.oop.model;

import agh.ics.oop.Simulation;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SimulationEngine {
public static final String EXECUTOR_TIMEOUT = "Executor did not terminate in time";
private final List<Simulation> simulations;
private final List <Thread> threads = new ArrayList<>();
private final ExecutorService executor = Executors.newFixedThreadPool(4);
public SimulationEngine(List<Simulation> simulations){
this.simulations = simulations;
}

public void runSync(){
for (Simulation sim : simulations){
sim.run();
}
}

public void runAsync(){
for (Simulation sim : simulations){
Thread thread = new Thread(sim);
threads.add(thread);
thread.start();
}
try {
awaitSimulationEnd();
}catch(InterruptedException e) {
e.printStackTrace();
}
}

public void awaitSimulationEnd() throws InterruptedException {
for (Thread thread : threads){
thread.join();
}
executor.shutdown();
if (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.println("%s (%d s)".formatted(EXECUTOR_TIMEOUT, 10));
executor.shutdownNow();
}
}

public void runAsyncInThreadPool(){
for (Runnable simulation : simulations) {
executor.submit(simulation);
}
try {
awaitSimulationEnd();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
6 changes: 6 additions & 0 deletions oolab/src/main/java/agh/ics/oop/model/WorldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import agh.ics.oop.model.exceptions.IncorrectPositionException;

import java.util.List;
import java.util.UUID;

/**
* The interface responsible for interacting with the map of the world.
Expand Down Expand Up @@ -51,4 +52,9 @@ public interface WorldMap extends MoveValidator {
* @return ArrayList of all objects on map
*/
List<WorldElement> getElements();

/**
* @return MapID
*/
UUID getId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package agh.ics.oop.model.exceptions;

public class IllegalMoveSpecificationException extends RuntimeException {
public static final String ILLEGAL_MOVE_SPEC_MESSAGE = "move specification is illegal";
public IllegalMoveSpecificationException(String letter) {
super("%s: %s".formatted(letter, ILLEGAL_MOVE_SPEC_MESSAGE));
}
}
Loading