diff --git a/.idea/misc.xml b/.idea/misc.xml index 69ace3f..9d2ecf0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/ch/epfl/chacun/Area.java b/src/ch/epfl/chacun/Area.java index ff43219..23b6dbd 100644 --- a/src/ch/epfl/chacun/Area.java +++ b/src/ch/epfl/chacun/Area.java @@ -160,11 +160,8 @@ public Set majorityOccupants() { * @return the new area resulting from the connection */ public Area connectTo(Area newArea) { - Set zones = new HashSet<>(); - zones.addAll(this.zones); - - List occupants = new ArrayList<>(); - occupants.addAll(this.occupants); + Set zones = new HashSet<>(this.zones); + List occupants = new ArrayList<>(this.occupants); // Calculate the new number of open connections // The new area will have 2 less open connections, as each area had at least one open connection int openConnections = this.openConnections - 2; diff --git a/src/ch/epfl/chacun/ZonePartition.java b/src/ch/epfl/chacun/ZonePartition.java index fa5167f..f3bb3ff 100644 --- a/src/ch/epfl/chacun/ZonePartition.java +++ b/src/ch/epfl/chacun/ZonePartition.java @@ -144,9 +144,7 @@ public void removeOccupant(Z zone, PlayerColor color) { * @throws IllegalArgumentException if the area is not part of the partition */ public void removeAllOccupantsOf(Area area) { - if (!areas.contains(area)) { - throw new IllegalArgumentException("The area is not part of the partition."); - } + Preconditions.checkArgument(areas.contains(area)); // Create a new area with no occupants Area unoccupiedArea = area.withoutOccupants(); // Replace the area containing the given zone by the new one in the set of areas @@ -168,7 +166,15 @@ public void union(Z zone1, Z zone2) { Area area2 = findAreaContainingZone(zone2); // If the two zones are not assigned to the same area, connect the two areas // Otherwise connect the area to itself - area1.connectTo(area1.equals(area2) ? area1 : area2); + if(!area1.equals(area2)) { + // Remove the two areas from the set of areas and add the new one + areas.remove(area1); + areas.remove(area2); + } else { + // Remove the area from the set of areas and add the new one + areas.remove(area1); + } + areas.add(area1.connectTo(area2)); } /** diff --git a/test/ch/epfl/chacun/AreaTest.java b/test/ch/epfl/chacun/AreaTest.java index 4c238e2..9e8e996 100644 --- a/test/ch/epfl/chacun/AreaTest.java +++ b/test/ch/epfl/chacun/AreaTest.java @@ -315,4 +315,35 @@ void withoutOccupantWorks() { assertEquals(forestArea2, forestArea1.withoutOccupant(PlayerColor.YELLOW)); } + @Test + void zoneWithSpecialPowerReturnsNullWhenNoSpecialPower() { + Set meadows = new HashSet<>(); + meadows.add(new Meadow(0, new ArrayList<>(), null)); + List occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area meadowArea = new Area<>(meadows, occupants1, 3); + + assertNull(meadowArea.zoneWithSpecialPower(SpecialPower.HUNTING_TRAP)); + } + + @Test + void zoneWithSpecialPowerReturnsNullWhenDifferentSpecialPower() { + Set meadows = new HashSet<>(); + meadows.add(new Meadow(0, new ArrayList<>(), SpecialPower.HUNTING_TRAP)); + List occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area meadowArea = new Area<>(meadows, occupants1, 3); + + assertNull(meadowArea.zoneWithSpecialPower(SpecialPower.LOGBOAT)); + } + + @Test + void zoneWithSpecialPowerWorks() { + Set meadows = new HashSet<>(); + Meadow meadowWithSpecialPower = new Meadow(0, new ArrayList<>(), SpecialPower.HUNTING_TRAP); + meadows.add(meadowWithSpecialPower); + List occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area meadowArea = new Area<>(meadows, occupants1, 3); + + assertEquals(meadowWithSpecialPower, meadowArea.zoneWithSpecialPower(SpecialPower.HUNTING_TRAP)); + } + } diff --git a/test/ch/epfl/chacun/ZonePartitionTest.java b/test/ch/epfl/chacun/ZonePartitionTest.java new file mode 100644 index 0000000..92443ba --- /dev/null +++ b/test/ch/epfl/chacun/ZonePartitionTest.java @@ -0,0 +1,197 @@ +package ch.epfl.chacun; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static ch.epfl.chacun.Zone.Forest; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ZonePartitionTest { + @Test + void areaContainingThrowsOnUnassignedZone() { + Set forestZones = new HashSet<>(); + forestZones.add(new Forest(0, Forest.Kind.PLAIN)); + List occupants = new ArrayList<>(List.of(PlayerColor.RED)); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + + assertThrows(IllegalArgumentException.class + , () -> zonePartition.areaContaining(new Forest(1, Forest.Kind.PLAIN))); + } + + @Test + void areaContainingWorks() { + Set forestZones = new HashSet<>(); + Forest forest = new Forest(0, Forest.Kind.PLAIN); + forestZones.add(forest); + List occupants = new ArrayList<>(List.of(PlayerColor.RED)); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + + assertEquals(forestArea, zonePartition.areaContaining(forest)); + } + + @Test + void addSingletonWorks() { + Set forestZones = new HashSet<>(); + forestZones.add(new Forest(0, Forest.Kind.PLAIN)); + List occupants = new ArrayList<>(List.of(PlayerColor.RED)); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder<>(zonePartition); + + Set newForestZones = new HashSet<>(); + Forest newForest = new Forest(1, Forest.Kind.WITH_MUSHROOMS); + newForestZones.add(newForest); + List newOccupants = new ArrayList<>(); + Area newForestArea = new Area<>(newForestZones, newOccupants, 1); + + builder.addSingleton(newForest, 1); + ZonePartition buildedZonePartition = builder.build(); + + Set expectedAreas = new HashSet<>(); + expectedAreas.add(forestArea); + expectedAreas.add(newForestArea); + + assertEquals(expectedAreas, buildedZonePartition.areas()); + } + + @Test + void addInitialOccupantThrowsOnUnAssignedZone() { + Set forestZones = new HashSet<>(); + forestZones.add(new Forest(0, Forest.Kind.PLAIN)); + List occupants = new ArrayList<>(); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder(zonePartition); + + assertThrows(IllegalArgumentException.class + , () -> builder.addInitialOccupant(new Forest(1, Forest.Kind.WITH_MENHIR), PlayerColor.BLUE)); + } + + @Test + void addInitialOccupantThrowsOnOccupiedArea() { + Set forestZones = new HashSet<>(); + Forest forest = new Forest(0, Forest.Kind.PLAIN); + forestZones.add(forest); + List occupants = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder(zonePartition); + + assertThrows(IllegalArgumentException.class + , () -> builder.addInitialOccupant(forest, PlayerColor.BLUE)); + } + + @Test + void addInitialOccupantWorks() { + Set forestZones = new HashSet<>(); + Forest forest = new Forest(0, Forest.Kind.PLAIN); + forestZones.add(forest); + List occupants = new ArrayList<>(); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder<>(zonePartition); + + List newOccupants = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area newForestArea = new Area<>(forestZones, newOccupants, 3); + + builder.addInitialOccupant(forest, PlayerColor.YELLOW); + Set expectedAreas = new HashSet<>(); + expectedAreas.add(newForestArea); + + assertEquals(expectedAreas, builder.build().areas()); + } + + @Test + void removeOccupantWorks() { + Set forestZones = new HashSet<>(); + Forest forest = new Forest(0, Forest.Kind.PLAIN); + forestZones.add(forest); + List occupants = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder<>(zonePartition); + + List newOccupants = new ArrayList<>(); + Area newForestArea = new Area<>(forestZones, newOccupants, 3); + + builder.removeOccupant(forest, PlayerColor.YELLOW); + Set expectedAreas = new HashSet<>(); + expectedAreas.add(newForestArea); + + assertEquals(expectedAreas, builder.build().areas()); + } + + @Test + void removeAllOccupantsOfThrowsOnUnknownArea() { + Set forestZones = new HashSet<>(); + Forest forest = new Forest(0, Forest.Kind.PLAIN); + forestZones.add(forest); + List occupants = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area forestArea = new Area<>(forestZones, occupants, 3); + + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder<>(zonePartition); + + Forest unknownForest = new Forest(1, Forest.Kind.WITH_MENHIR); + Area unknownArea = new Area<>(Set.of(unknownForest), occupants, 2); + + assertThrows(IllegalArgumentException.class, () -> builder.removeAllOccupantsOf(unknownArea)); + } + + @Test + void removeAllOccupantsOfWorks() { + Set forestZones = new HashSet<>(); + Forest forest = new Forest(0, Forest.Kind.PLAIN); + forestZones.add(forest); + List occupants = new ArrayList<>(List.of(PlayerColor.YELLOW, PlayerColor.GREEN)); + Area forestArea = new Area<>(forestZones, occupants, 3); + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea)); + ZonePartition.Builder builder = new ZonePartition.Builder<>(zonePartition); + + List newOccupants = new ArrayList<>(); + Area newForestArea = new Area<>(forestZones, newOccupants, 3); + + builder.removeAllOccupantsOf(forestArea); + Set expectedAreas = new HashSet<>(); + expectedAreas.add(newForestArea); + + assertEquals(expectedAreas, builder.build().areas()); + } + @Test + void unionWorksWithDifferentAreas() { + Set forestZones1 = new HashSet<>(); + Forest forest1 = new Forest(0, Forest.Kind.PLAIN); + forestZones1.add(forest1); + List occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW)); + Area forestArea1 = new Area<>(forestZones1, occupants1, 3); + + Set forestZones2 = new HashSet<>(); + Forest forest2 = new Forest(1, Forest.Kind.WITH_MENHIR); + forestZones2.add(forest2); + List occupants2 = new ArrayList<>(List.of(PlayerColor.RED)); + Area forestArea2 = new Area<>(forestZones2, occupants2, 3); + + ZonePartition zonePartition = new ZonePartition<>(Set.of(forestArea1, forestArea2)); + ZonePartition.Builder builder = new ZonePartition.Builder<>(zonePartition); + + Set expectedForestZones = new HashSet<>(); + expectedForestZones.add(forest1); + expectedForestZones.add(forest2); + List expectedOccupants = new ArrayList<>(); + expectedOccupants.add(PlayerColor.YELLOW); + expectedOccupants.add(PlayerColor.RED); + Area expectedArea = new Area<>(expectedForestZones, expectedOccupants, 4); + + builder.union(forest1, forest2); + + assertEquals(Set.of(expectedArea), builder.build().areas()); + } + +}