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
9 changes: 5 additions & 4 deletions oolab/src/main/java/agh/ics/oop/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
public class Simulation {
private List<Animal> animals = new ArrayList<Animal>();
private List<MoveDirection> moves;
private WorldMap map;
private WorldMap<Animal, Vector2d> map;
private MapVisualizer visualizer;
public Simulation(List<Vector2d> positions, List<MoveDirection> moves, WorldMap map) {
public Simulation(List<Vector2d> positions, List<MoveDirection> moves, WorldMap<Animal, Vector2d> map) {
this.moves = moves;
this.map = map;
this.visualizer = new MapVisualizer(map);
for(Vector2d position : positions) {
Animal animal = new Animal(position);
if(map.place(animal)) {
if(map.place(animal,position)) {
animals.add(animal);
}
}
Expand All @@ -33,10 +33,11 @@ public void run() {
int i = 0;
int count = animals.size();
Vector2d upperRight = map.getUpperRight();
Vector2d lowerLeft = map.getLowerLeft();
for(MoveDirection move : moves) {
Animal animal = animals.get(i%count);
map.move(animal,move);
System.out.println(visualizer.draw(new Vector2d(0,0),upperRight));
System.out.println(visualizer.draw(lowerLeft,upperRight));
i++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions oolab/src/main/java/agh/ics/oop/model/MoveValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import agh.ics.oop.model.Vector2d;

public interface MoveValidator {
public interface MoveValidator<P> {

/**
* Indicate if any object can move to the given position.
Expand All @@ -11,5 +11,5 @@ public interface MoveValidator {
* The position checked for the movement possibility.
* @return True if the object can move to that position.
*/
boolean canMoveTo(Vector2d position);
boolean canMoveTo(P position);
}
19 changes: 9 additions & 10 deletions oolab/src/main/java/agh/ics/oop/model/RectangularMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;
import java.util.Map;

public class RectangularMap implements WorldMap {
public class RectangularMap implements WorldMap<Animal,Vector2d> {
Map<Vector2d, Animal> animals = new HashMap<>();
private final Vector2d lowerLeft;
private final Vector2d upperRight;
Expand All @@ -28,9 +28,9 @@ public boolean isOccupied(Vector2d targetPosition) {
return (animals.get(targetPosition)!=null);
}
@Override
public boolean place(Animal animal) {
if(!isOccupied(animal.getPosition())) {
animals.put(animal.getPosition(),animal);
public boolean place(Animal animal, Vector2d position) {
if(!isOccupied(position)) {
animals.put(position,animal);
return true;
}
return false;
Expand All @@ -40,12 +40,12 @@ public Animal objectAt(Vector2d targetPosition) {
return (animals.get(targetPosition));
}
@Override
public void move(Animal animal, MoveDirection direction) {
Vector2d oldPosition = animal.getPosition();
animal.move(direction,this);
if(!animal.getPosition().equals(oldPosition)) {
public void move(Animal object, MoveDirection direction) {
Vector2d oldPosition = object.getPosition();
object.move(direction,this);
if(!object.getPosition().equals(oldPosition)) {
animals.remove(oldPosition);
animals.put(animal.getPosition(),animal);
animals.put(object.getPosition(),object);
}
}
@Override
Expand All @@ -58,5 +58,4 @@ public boolean canMoveTo(Vector2d targetPosition) {
public String toString() {
return (visualiser.draw(this.lowerLeft,this.upperRight));
}

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

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

public class TextMap implements WorldMap<String, Integer>{
List<String> map = new ArrayList<String>();
public TextMap() {
}
@Override
public boolean place(String object, Integer position) {
map.add(object);
return true;
}
@Override
public void move(String object,MoveDirection direction) {
Integer position = map.indexOf(object);
String temp = map.get(position);
switch(direction) {
case BACKWARD: {
if(position-1>=0) {
map.set(position, map.get(position-1));
map.set(position-1, temp);
}
break;
}
case FORWARD: {
if(position+1<map.size()) {
map.set(position, map.get(position+1));
map.set(position+1, temp);
}
break;
}
default: {
break;
}
}
}
@Override
public boolean isOccupied(Integer position) {
if(position<0 || position>=map.size()) {
return false;
}
if(map.get(position) == null)
return false;
return true;
}
@Override
public Integer getLowerLeft() {
return(0);
}
@Override
public Integer getUpperRight() {
return(map.size());
}
@Override
public String objectAt(Integer position) {
return(map.get(position));
}
@Override
public boolean canMoveTo(Integer position) {
if(position<0 || position>=map.size()) {
return false;
} else {
return true;
}
}
}
16 changes: 9 additions & 7 deletions oolab/src/main/java/agh/ics/oop/model/WorldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
*
* @author apohllo, idzik
*/
public interface WorldMap extends MoveValidator {
public interface WorldMap<T,P> extends MoveValidator<P> {

/**
* Place a animal on the map.
*
* @param animal The animal to place on the map.
* @param object The animal to place on the map.
* @return True if the animal was placed. The animal cannot be placed if the move is not valid.
*/
boolean place(Animal animal);
boolean place(T object,P position);

/**
* Moves an animal (if it is present on the map) according to specified direction.
* If the move is not possible, this method has no effect.
*/
void move(Animal animal, MoveDirection direction);
void move(T object, MoveDirection direction);

/**
* Return true if given position on the map is occupied. Should not be
Expand All @@ -33,15 +33,17 @@ public interface WorldMap extends MoveValidator {
* @param position Position to check.
* @return True if the position is occupied.
*/
boolean isOccupied(Vector2d position);
boolean isOccupied(P position);

/**
* Return an animal at a given position.
*
* @param position The position of the animal.
* @return animal or null if the position is not occupied.
*/
Animal objectAt(Vector2d position);
T objectAt(P position);

Vector2d getUpperRight();
P getUpperRight();

P getLowerLeft();
}
2 changes: 1 addition & 1 deletion oolab/src/test/java/agh/ics/oop/model/AnimalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class AnimalTest {

class valid implements MoveValidator {
class valid implements MoveValidator<Vector2d> {
public boolean canMoveTo(Vector2d position) {
return true;
}
Expand Down
16 changes: 8 additions & 8 deletions oolab/src/test/java/agh/ics/oop/model/RectangularMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,42 @@ void placeTest() {
Animal animalTested1 = new Animal(new Vector2d(2,1));
Animal animalTested2 = new Animal(new Vector2d(3,7));
Animal animalTested3 = new Animal(new Vector2d(3,7));
assertTrue(map.place(animalTested1));
assertTrue(map.place(animalTested2));
assertFalse(map.place(animalTested3));
assertTrue(map.place(animalTested1,animalTested1.getPosition()));
assertTrue(map.place(animalTested2,animalTested2.getPosition()));
assertFalse(map.place(animalTested3,animalTested3.getPosition()));
}
@Test
void isOccupiedTest() {
Animal animalTested4 = new Animal(new Vector2d(6,9));
map.place(animalTested4);
map.place(animalTested4,animalTested4.getPosition());
assertTrue(map.isOccupied(new Vector2d(6,9)));
assertFalse(map.isOccupied(new Vector2d(6,6)));
}
@Test
void objectAtTest() {
Animal animalTested5 = new Animal(new Vector2d(2,2));
map.place(animalTested5);
map.place(animalTested5,animalTested5.getPosition());
assertEquals(map.objectAt(new Vector2d(2,2)),animalTested5);
assertNotEquals(map.objectAt(new Vector2d(3,3)),animalTested5);
}
@Test
void toStringTest() {
System.out.println(map.toString());
Animal animalTested6 = new Animal(new Vector2d(4,4));
map.place(animalTested6);
map.place(animalTested6,animalTested6.getPosition());
System.out.println(map.toString());
}
@Test
void canMoveToTest() {
Animal animalTested7 = new Animal(new Vector2d(5,5));
map.place(animalTested7);
map.place(animalTested7,animalTested7.getPosition());
assertTrue(map.canMoveTo(new Vector2d(4,5)));
assertFalse(map.canMoveTo(new Vector2d(5,5)));
}
@Test
void moveTest() {
Animal animalTested8 = new Animal(new Vector2d(7,7));
map.place(animalTested8);
map.place(animalTested8,animalTested8.getPosition());
map.move(animalTested8,MoveDirection.BACKWARD);
assertTrue(map.isOccupied(new Vector2d(7,6)));
map.move(animalTested8,MoveDirection.BACKWARD);
Expand Down
63 changes: 63 additions & 0 deletions oolab/src/test/java/agh/ics/oop/model/TextMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package agh.ics.oop.model;

import org.junit.jupiter.api.Test;

public class TextMapTest {
String[] args = {"Wiśnia","Borówka","Jabłko","Ananas","Gruszka","Truskawka","Poziomka"};

@Test
public void placeTest() {
TextMap map = new TextMap();
for(int i=0;i<args.length;i++) {
map.place(args[i],i);
}
for(int i=0;i<args.length;i++) {
assert(map.objectAt(i).equals(args[i]));
}
}
@Test
public void moveTest() {
TextMap map = new TextMap();
for(int i=0;i<args.length;i++) {
map.place(args[i],i);
}
map.move("Borówka",MoveDirection.FORWARD);
assert(map.objectAt(2).equals("Borówka"));
assert(map.objectAt(1).equals("Jabłko"));
map.move("Jabłko",MoveDirection.BACKWARD);
assert(map.objectAt(1).equals("Wiśnia"));
assert(map.objectAt(0).equals("Jabłko"));
map.move("Jabłko",MoveDirection.BACKWARD);
assert(map.objectAt(0).equals("Jabłko"));
map.move("Truskawka",MoveDirection.FORWARD);
assert(map.objectAt(6).equals("Truskawka"));
assert(map.objectAt(5).equals("Poziomka"));
map.move("Truskawka",MoveDirection.FORWARD);
assert(map.objectAt(6).equals("Truskawka"));
}
@Test
public void isOccupiedTest() {
TextMap map = new TextMap();
for(int i=0;i<args.length;i++) {
map.place(args[i],i);
}
assert(map.isOccupied(0));
assert(map.isOccupied(1));
assert(map.isOccupied(2));
assert(map.isOccupied(3));
assert(map.isOccupied(4));
assert(map.isOccupied(5));
assert(map.isOccupied(6));
assert(!map.isOccupied(7));
assert(!map.isOccupied(8));
assert(!map.isOccupied(9));
}
@Test
public void getUpperRightTest() {
TextMap map = new TextMap();
for(int i=0;i<args.length;i++) {
map.place(args[i],i);
}
assert(map.getUpperRight()==7);
}
}