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
228 changes: 228 additions & 0 deletions oolab/map_0.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
Move information:
Animal was placed at (1,1)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |*| | | | | | | | |
1: | |^| | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,1) to (1,2)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |^| | | | | | | | |
1: | | | | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,2) to (1,1)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |*| | | | | | | | |
1: | |^| | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal changed direction from Północ to Wschód

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |*| | | | | | | | |
1: | |>| | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal changed direction from Wschód to Północ

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |*| | | | | | | | |
1: | |^| | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,1) to (1,2)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |^| | | | | | | | |
1: | | | | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,2) to (1,3)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | |^|*| | | | | | | |
2: | |*| | | | | | | | |
1: | | | | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal changed direction from Północ to Wschód

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | |>|*| | | | | | | |
2: | |*| | | | | | | | |
1: | | | | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal changed direction from Wschód to Południe

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | |v|*| | | | | | | |
2: | |*| | | | | | | | |
1: | | | | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,3) to (1,2)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |v| | | | | | | | |
1: | | | | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,2) to (1,1)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |*| | | | | | | | |
1: | |v| | | | | | | | |
0: |*| | | | | | | | |*|
-1: ---------------------

-----------------
Move information:
Animal moved from (1,1) to (1,0)

Current map state:

y\x 0 1 2 3 4 5 6 7 8 9
9: ---------------------
8: | | | | | | | | | |*|
7: | | | |*| | | |*| | |
6: | | | | |*| | | | | |
5: | | | | | | | |*| | |
4: | | | |*| | | | | | |
3: | | |*| | | | | | | |
2: | |*| | | | | | | | |
1: | | | | | | | | | | |
0: |*|v| | | | | | | |*|
-1: ---------------------

-----------------
32 changes: 25 additions & 7 deletions oolab/src/main/java/agh/ics/oop/OptionsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import agh.ics.oop.model.MoveDirection;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;


public class OptionsParser {
Expand All @@ -29,19 +29,37 @@ public static List<MoveDirection> parseOptions(String[] options) {
// };
// current_index++;
// }
List<MoveDirection> moves = new ArrayList<>();
for (String option : options) {
Stream<String> optionsStream = Stream.of(options);

var result = optionsStream.map(option -> {
if (!(option.equals("f") || option.equals("b") || option.equals("l") || option.equals("r"))) {
throw new IllegalArgumentException(option + " is not legal move specification");
}
moves.add(switch (option) {
return switch (option) {
case "f" -> MoveDirection.FORWARD;
case "b" -> MoveDirection.BACKWARD;
case "l" -> MoveDirection.LEFT;
case "r" -> MoveDirection.RIGHT;
default -> null;
});
}
return moves;
}
;
}).toList();

return result;

// List<MoveDirection> moves = new ArrayList<>();
// for (String option : options) {
// if (!(option.equals("f") || option.equals("b") || option.equals("l") || option.equals("r"))) {
// throw new IllegalArgumentException(option + " is not legal move specification");
// }
// moves.add(switch (option) {
// case "f" -> MoveDirection.FORWARD;
// case "b" -> MoveDirection.BACKWARD;
// case "l" -> MoveDirection.LEFT;
// case "r" -> MoveDirection.RIGHT;
// default -> null;
// });
// }
// return moves;
}
}
7 changes: 7 additions & 0 deletions oolab/src/main/java/agh/ics/oop/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import agh.ics.oop.model.RectangularMap;
import agh.ics.oop.model.Vector2d;
import agh.ics.oop.model.util.ConsoleMapDisplay;
import agh.ics.oop.model.util.FileMapDisplay;

import java.time.LocalDateTime;
import java.util.List;

public class World {
Expand All @@ -17,6 +19,11 @@ public static void main(String[] args) {
var rectangularMap = new RectangularMap(10, 10, 1);
var observer = new ConsoleMapDisplay();
grassField.addObserver(observer);
grassField.addObserver((worldMap, message) -> {
System.out.println(String.format("%s %s", LocalDateTime.now(), message));
});
FileMapDisplay fileMapDisplay = new FileMapDisplay();
grassField.addObserver(fileMapDisplay);
rectangularMap.addObserver(observer);
var simulation = new Simulation(positions, moves, grassField);
var simulation2 = new Simulation(positions, moves, rectangularMap);
Expand Down
16 changes: 10 additions & 6 deletions oolab/src/main/java/agh/ics/oop/model/AbstractWorldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import agh.ics.oop.model.util.IncorrectPositionException;
import agh.ics.oop.model.util.MapVisualizer;

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

public abstract class AbstractWorldMap implements WorldMap {
protected final MapVisualizer visualizer;
Expand Down Expand Up @@ -69,13 +67,13 @@ public void place(Animal animal) throws IncorrectPositionException {
}

@Override
public WorldElement objectAt(Vector2d position) {
return animals.get(position);
public Optional<WorldElement> objectAt(Vector2d position) {
return Optional.ofNullable(animals.get(position));
}

@Override
public boolean isOccupied(Vector2d position) {
return objectAt(position) != null;
return objectAt(position).isPresent();
}

@Override
Expand Down Expand Up @@ -105,4 +103,10 @@ public String toString() {
public int getId() {
return mapId;
}

@Override
public List<Animal> getOrderedAnimals() {
return animals.values().stream().sorted(Comparator.comparing((Animal animal) -> animal.getPosition().getX()
).thenComparing((Animal animal) -> animal.getPosition().getY())).toList();
}
}
10 changes: 10 additions & 0 deletions oolab/src/main/java/agh/ics/oop/model/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ public int hashCode() {
public Vector2d getPosition() {
return localizationOnMap;
}

@Override
public String getResourceString() {
return switch (facingDirection) {
case NORTH -> "up.png";
case EAST -> "right.png";
case SOUTH -> "down.png";
case WEST -> "left.png";
};
}
}
5 changes: 5 additions & 0 deletions oolab/src/main/java/agh/ics/oop/model/Grass.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public Vector2d getPosition() {
return position;
}

@Override
public String getResourceString() {
return "grass.png";
}

@Override
public String toString() {
return "*";
Expand Down
Loading