Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Globe #2

Merged
merged 3 commits into from
Dec 28, 2023
Merged
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/main/java/agh/ics/oop/model/movement/MapDirection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public enum MapDirection {

private final String value; // we can change it later if necessary
private final Vector2d unitVector;

public Vector2d getUnitVector() {
return unitVector;
}

private static final List<MapDirection> valuesList = List.of(values());

MapDirection(String value, Vector2d unitVector) {
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/agh/ics/oop/model/worldMaps/Globe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package agh.ics.oop.model.worldMaps;

import agh.ics.oop.model.movement.Vector2d;

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

public class Globe {
private final int width;
private final int height;
private static final Vector2d lowerLeftBoundary = new Vector2d(1, 1);
private final Vector2d upperRightBoundary;;
private int plantCount;
private final int energyPerPlant;
private final int plantsPerDay;
private int numberOfAnimals = 0;
private final int animalsStartingEnergy;
private final int minimalEnergyToReproduce;
private final int energyUsedToReproduce;
private final int minNumberOfMutations;
private final int maxNumberOfMutations;
private final int genomeLength;

private Map<Vector2d, List<Animal>> animals = new HashMap<Vector2d, ArrayList<Animal>>();
private Map<Vector2d, Plant> plants = new HashMap<Vector2d, Plant>();

private void checkIfNotNegative(int value) throws IllegalArgumentException{
if (value < 0) {
throw new IllegalArgumentException();
};
}

public Globe(int width, int height, int energyPerPlant, int plantsPerDay, int numberOfStartingAnimals,
int animalsStartingEnergy,int minEnergyToReproduce, int energyUsedToReproduce,
int minNumberOfMutations, int maxNumberOfMutations, int genomeLength) throws IllegalArgumentException {

if (width < 1) throw new IllegalArgumentException("Map's width must be positive");
this.width = width;

if (height < 1) throw new IllegalArgumentException("Map's height must be positive");
this.height = height;

upperRightBoundary = new Vector2d(width, height);


checkIfNotNegative(energyPerPlant);
this.energyPerPlant = energyPerPlant;

checkIfNotNegative(plantsPerDay);
this.plantsPerDay = plantsPerDay;

checkIfNotNegative(numberOfStartingAnimals);
Random random = new Random();
for (int i = 0; i < numberOfStartingAnimals; i++) {
place(new Animal(new Vector2d(random.nextInt(width) + 1, random.nextInt(height) + 1)));
}

checkIfNotNegative(animalsStartingEnergy);
this.animalsStartingEnergy = animalsStartingEnergy;

checkIfNotNegative(minEnergyToReproduce);
this.minimalEnergyToReproduce = minEnergyToReproduce;
checkIfNotNegative(energyUsedToReproduce);
this.energyUsedToReproduce = energyUsedToReproduce;


checkIfNotNegative(minNumberOfMutations);
this.minNumberOfMutations = minNumberOfMutations;
if (maxNumberOfMutations < minNumberOfMutations) {
throw new IllegalArgumentException("Maximum number of mutations must be greater than minimal");
}
this.maxNumberOfMutations = maxNumberOfMutations;
if (genomeLength < 1) throw new IllegalArgumentException("Genome length must be positive");
this.genomeLength = genomeLength;
}

public boolean canMoveTo(Vector2d position) {
return position.precedes(upperRightBoundary) && position.follows(lowerLeftBoundary);
}

public void place(Animal animal) {
Vector2d position = animal.getPosition();
numberOfAnimals++;

List<Animal> animalsAtThisPosition = animals.remove(position);
animalsAtThisPosition.add(animal);

animals.put(animalsAtThisPosition);
}
}
36 changes: 36 additions & 0 deletions src/test/java/agh/ics/oop/model/movement/MapDirectionTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package agh.ics.oop.model.movement;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;

public class MapDirectionTests {
@Test
public void getUnitVectorTests() {
Assertions.assertEquals(new Vector2d(1, 1), MapDirection.NE.getUnitVector());
Assertions.assertEquals(new Vector2d(1, 0), MapDirection.E.getUnitVector());
Assertions.assertEquals(new Vector2d(1, -1), MapDirection.SE.getUnitVector());
Assertions.assertEquals(new Vector2d(-1, 0), MapDirection.W.getUnitVector());
Assertions.assertEquals(new Vector2d(-1, 1), MapDirection.NW.getUnitVector());
}

@Test
public void toStringTests() {
Assertions.assertEquals("N", MapDirection.N.toString());
Assertions.assertEquals("E", MapDirection.E.toString());
Assertions.assertEquals("SE", MapDirection.SE.toString());
Assertions.assertEquals("SW", MapDirection.SW.toString());
Assertions.assertEquals("W", MapDirection.W.toString());
}

@Test
public void turnRightTests() {
Assertions.assertEquals(MapDirection.NE, MapDirection.N.turnRight(1));
Assertions.assertEquals(MapDirection.S, MapDirection.SE.turnRight(1));
Assertions.assertEquals(MapDirection.N, MapDirection.NW.turnRight(1));
Assertions.assertEquals(MapDirection.SW, MapDirection.E.turnRight(3));
Assertions.assertEquals(MapDirection.N, MapDirection.W.turnRight(2));
Assertions.assertEquals(MapDirection.S, MapDirection.SW.turnRight(7));
}
}
133 changes: 133 additions & 0 deletions src/test/java/agh/ics/oop/model/movement/Vector2dTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package agh.ics.oop.model.movement;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class Vector2dTests { // sometimes I declare the same vector few times; imo it is not a big deal
@Test
public void testToString() {
Assertions.assertEquals("(8,0)", (new Vector2d(8, 0).toString()));
Assertions.assertEquals("(1,3)", (new Vector2d(1, 3).toString()));
Assertions.assertEquals("(100,-5)", (new Vector2d(100, -5).toString()));
Assertions.assertEquals("(9394,9394)", (new Vector2d(9394, 9394).toString()));
Assertions.assertEquals("(-2147483648,2147483647)", (new Vector2d(Integer.MIN_VALUE, Integer.MAX_VALUE).toString()));
}

@Test
public void testPrecedes() {
Vector2d vector1 = new Vector2d(1, 3);
Vector2d vector2 = new Vector2d(10, -6);
Vector2d vector3 = new Vector2d(50, 50);
Vector2d vector4 = new Vector2d(Integer.MIN_VALUE, Integer.MIN_VALUE);

Assertions.assertFalse(vector1.precedes(vector2));
Assertions.assertTrue(vector1.precedes(vector3));
Assertions.assertFalse(vector1.precedes(vector4));

Assertions.assertFalse(vector2.precedes(vector1));
Assertions.assertTrue(vector2.precedes(vector2));

Assertions.assertTrue(vector4.precedes(vector1));
Assertions.assertTrue(vector4.precedes(vector2));
Assertions.assertTrue(vector4.precedes(vector3));
}

@Test
public void testFollows() {
Vector2d vector1 = new Vector2d(10, 4);
Vector2d vector2 = new Vector2d(0, 8);
Vector2d vector3 = new Vector2d(-9, -3);
Vector2d vector4 = new Vector2d(Integer.MAX_VALUE, Integer.MAX_VALUE);

Assertions.assertFalse(vector1.follows(vector2));
Assertions.assertTrue(vector1.follows(vector3));
Assertions.assertFalse(vector1.follows(vector4));

Assertions.assertFalse(vector2.follows(vector1));
Assertions.assertTrue(vector2.follows(vector2));

Assertions.assertTrue(vector4.follows(vector1));
Assertions.assertTrue(vector4.follows(vector2));
Assertions.assertTrue(vector4.follows(vector3));
}

@Test
public void testAdd() {
Vector2d vector1 = new Vector2d(3, -2);
Vector2d vector2 = new Vector2d(40, 2);
Vector2d vector3 = new Vector2d(0, 0);

Assertions.assertEquals(new Vector2d(43, 0), vector1.add(vector2));
Assertions.assertEquals(new Vector2d(43, 0), vector2.add(vector1));

Assertions.assertEquals(new Vector2d(80, 4), vector2.add(vector2));

Assertions.assertEquals(vector1, vector1.add(vector3));
Assertions.assertEquals(vector3, vector3.add(vector3));
}

@Test
public void testSubtract() {
Vector2d vector1 = new Vector2d(1, 7);
Vector2d vector2 = new Vector2d(1, -8);
Vector2d vector3 = new Vector2d(0, 0);

Assertions.assertEquals(new Vector2d(0, 15), vector1.subtract(vector2));
Assertions.assertEquals(new Vector2d(0, -15), vector2.subtract(vector1));

Assertions.assertEquals(vector1, vector1.subtract(vector3));
Assertions.assertEquals(new Vector2d(-1, 8), vector3.subtract(vector2));

Assertions.assertEquals(vector3, vector2.subtract(vector2));
Assertions.assertEquals(vector3, vector3.subtract(vector3));
}

@Test
public void testUpperRight() {
Vector2d vector1 = new Vector2d(100, 5);
Vector2d vector2 = new Vector2d(-9, -9);
Vector2d vector3 = new Vector2d(60, Integer.MAX_VALUE);
Vector2d vector4 = new Vector2d(Integer.MIN_VALUE, Integer.MIN_VALUE);

Assertions.assertEquals(vector1, vector1.upperRight(vector2));
Assertions.assertEquals(new Vector2d(100, Integer.MAX_VALUE), vector1.upperRight(vector3));

Assertions.assertEquals(vector1, vector1.upperRight(vector4));
Assertions.assertEquals(vector2, vector2.upperRight(vector4));
Assertions.assertEquals(vector3, vector3.upperRight(vector4));

Assertions.assertEquals(vector3, vector2.upperRight(vector3));
Assertions.assertEquals(vector3, vector3.upperRight(vector2));
}

@Test
public void testLowerLeft() {
Vector2d vector1 = new Vector2d(102, 50);
Vector2d vector2 = new Vector2d(-1, 3);
Vector2d vector3 = new Vector2d(-500, Integer.MAX_VALUE);
Vector2d vector4 = new Vector2d(Integer.MAX_VALUE, Integer.MAX_VALUE);

Assertions.assertEquals(vector2, vector1.lowerLeft(vector2));
Assertions.assertEquals(new Vector2d(-500, 50), vector1.lowerLeft(vector3));

Assertions.assertEquals(vector1, vector1.lowerLeft(vector4));
Assertions.assertEquals(vector2, vector2.lowerLeft(vector4));
Assertions.assertEquals(vector3, vector3.lowerLeft(vector4));

Assertions.assertEquals(new Vector2d(-500, 3), vector2.lowerLeft(vector3));
Assertions.assertEquals(new Vector2d(-500, 3), vector3.lowerLeft(vector2));
}

@Test
public void testOpposite() {
Vector2d vector1 = new Vector2d(2, 5);
Vector2d vector2 = new Vector2d(-4, 9);
Vector2d vector3 = new Vector2d(0, 0);

Assertions.assertEquals(new Vector2d(-2, -5), vector1.opposite());
Assertions.assertEquals(new Vector2d(4, -9), vector2.opposite());

Assertions.assertEquals(vector2, vector2.opposite().opposite());
Assertions.assertEquals(vector3, vector3.opposite());
}
}
63 changes: 63 additions & 0 deletions src/test/java/agh/ics/oop/model/worldMaps/GlobeTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package agh.ics.oop.model.worldMaps;

import agh.ics.oop.model.movement.Vector2d;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class GlobeTests {
@Test
public void testGlobe() {
Globe globe1 = new Globe(20, 30, 2, 5, 18, 1,
3, 2, 0, 4, 2);
Assertions.assertInstanceOf(Globe.class, globe1);

Globe globe2 = new Globe(1000, 1, 0, 0, Integer.MAX_VALUE, 0,
1000, 0, 0, 0, Integer.MAX_VALUE);
Assertions.assertInstanceOf(Globe.class, globe2);


Assertions.assertThrows(IllegalArgumentException.class, () -> new Globe(-2, 10, 0,
0, Integer.MAX_VALUE, 0, 1000, 0,
0, 0, Integer.MAX_VALUE));

Assertions.assertThrows(IllegalArgumentException.class, () -> new Globe(400, 0, 0,
0, 8, 0, 1000, 0,
0, 0, 9));

Assertions.assertThrows(IllegalArgumentException.class, () -> new Globe(45, 100, -6,
0, 13, 0, 1000, 0,
0, 0, 11));

Assertions.assertThrows(IllegalArgumentException.class, () -> new Globe(2, 120, 20,
200000, Integer.MIN_VALUE, 20, 10, 70,
70, 70, 1));

Assertions.assertThrows(IllegalArgumentException.class, () -> new Globe(25, 20, 9,
0, 1, Integer.MAX_VALUE, 9, 3,
3, 2, 10));

Assertions.assertThrows(IllegalArgumentException.class, () -> new Globe(25, 20, 9,
0, 1, Integer.MAX_VALUE, 9, 3,
3, 200, 0));
}

@Test
public void testCanMoveTo() {
Globe globe = new Globe(36, 200, 22, 51, 8, 9,
93, 0, 0, 0, 2);

Assertions.assertTrue(globe.canMoveTo(new Vector2d(10, 19)));
Assertions.assertTrue(globe.canMoveTo(new Vector2d(15, 15)));
Assertions.assertTrue(globe.canMoveTo(new Vector2d(1, 1)));
Assertions.assertTrue(globe.canMoveTo(new Vector2d(1, 200)));
Assertions.assertTrue(globe.canMoveTo(new Vector2d(36, 200)));
Assertions.assertTrue(globe.canMoveTo(new Vector2d(36, 1)));

Assertions.assertFalse(globe.canMoveTo(new Vector2d(0 ,0)));
Assertions.assertFalse(globe.canMoveTo(new Vector2d(10 ,-1)));
Assertions.assertFalse(globe.canMoveTo(new Vector2d(10 ,Integer.MAX_VALUE)));
Assertions.assertFalse(globe.canMoveTo(new Vector2d(10 ,Integer.MIN_VALUE)));
Assertions.assertFalse(globe.canMoveTo(new Vector2d(Integer.MAX_VALUE ,Integer.MIN_VALUE)));
Assertions.assertFalse(globe.canMoveTo(new Vector2d(201 ,36)));
}
}