Skip to content

Commit

Permalink
Merge pull request #2 from Mw3y/step-3
Browse files Browse the repository at this point in the history
Step 3 fixes
  • Loading branch information
Mw3y authored Mar 7, 2024
2 parents 4d1ce23 + 2f78ca5 commit 3364879
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 10 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

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

7 changes: 2 additions & 5 deletions src/ch/epfl/chacun/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,8 @@ public Set<PlayerColor> majorityOccupants() {
* @return the new area resulting from the connection
*/
public Area<Z> connectTo(Area<Z> newArea) {
Set<Z> zones = new HashSet<>();
zones.addAll(this.zones);

List<PlayerColor> occupants = new ArrayList<>();
occupants.addAll(this.occupants);
Set<Z> zones = new HashSet<>(this.zones);
List<PlayerColor> 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;
Expand Down
14 changes: 10 additions & 4 deletions src/ch/epfl/chacun/ZonePartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Z> 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<Z> unoccupiedArea = area.withoutOccupants();
// Replace the area containing the given zone by the new one in the set of areas
Expand All @@ -168,7 +166,15 @@ public void union(Z zone1, Z zone2) {
Area<Z> 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));
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/ch/epfl/chacun/AreaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,35 @@ void withoutOccupantWorks() {
assertEquals(forestArea2, forestArea1.withoutOccupant(PlayerColor.YELLOW));
}

@Test
void zoneWithSpecialPowerReturnsNullWhenNoSpecialPower() {
Set<Meadow> meadows = new HashSet<>();
meadows.add(new Meadow(0, new ArrayList<>(), null));
List<PlayerColor> occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Meadow> meadowArea = new Area<>(meadows, occupants1, 3);

assertNull(meadowArea.zoneWithSpecialPower(SpecialPower.HUNTING_TRAP));
}

@Test
void zoneWithSpecialPowerReturnsNullWhenDifferentSpecialPower() {
Set<Meadow> meadows = new HashSet<>();
meadows.add(new Meadow(0, new ArrayList<>(), SpecialPower.HUNTING_TRAP));
List<PlayerColor> occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Meadow> meadowArea = new Area<>(meadows, occupants1, 3);

assertNull(meadowArea.zoneWithSpecialPower(SpecialPower.LOGBOAT));
}

@Test
void zoneWithSpecialPowerWorks() {
Set<Meadow> meadows = new HashSet<>();
Meadow meadowWithSpecialPower = new Meadow(0, new ArrayList<>(), SpecialPower.HUNTING_TRAP);
meadows.add(meadowWithSpecialPower);
List<PlayerColor> occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Meadow> meadowArea = new Area<>(meadows, occupants1, 3);

assertEquals(meadowWithSpecialPower, meadowArea.zoneWithSpecialPower(SpecialPower.HUNTING_TRAP));
}

}
197 changes: 197 additions & 0 deletions test/ch/epfl/chacun/ZonePartitionTest.java
Original file line number Diff line number Diff line change
@@ -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<Forest> forestZones = new HashSet<>();
forestZones.add(new Forest(0, Forest.Kind.PLAIN));
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.RED));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));

assertThrows(IllegalArgumentException.class
, () -> zonePartition.areaContaining(new Forest(1, Forest.Kind.PLAIN)));
}

@Test
void areaContainingWorks() {
Set<Forest> forestZones = new HashSet<>();
Forest forest = new Forest(0, Forest.Kind.PLAIN);
forestZones.add(forest);
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.RED));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));

assertEquals(forestArea, zonePartition.areaContaining(forest));
}

@Test
void addSingletonWorks() {
Set<Forest> forestZones = new HashSet<>();
forestZones.add(new Forest(0, Forest.Kind.PLAIN));
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.RED));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));
ZonePartition.Builder<Forest> builder = new ZonePartition.Builder<>(zonePartition);

Set<Forest> newForestZones = new HashSet<>();
Forest newForest = new Forest(1, Forest.Kind.WITH_MUSHROOMS);
newForestZones.add(newForest);
List<PlayerColor> newOccupants = new ArrayList<>();
Area<Forest> newForestArea = new Area<>(newForestZones, newOccupants, 1);

builder.addSingleton(newForest, 1);
ZonePartition<Forest> buildedZonePartition = builder.build();

Set<Area> expectedAreas = new HashSet<>();
expectedAreas.add(forestArea);
expectedAreas.add(newForestArea);

assertEquals(expectedAreas, buildedZonePartition.areas());
}

@Test
void addInitialOccupantThrowsOnUnAssignedZone() {
Set<Forest> forestZones = new HashSet<>();
forestZones.add(new Forest(0, Forest.Kind.PLAIN));
List<PlayerColor> occupants = new ArrayList<>();
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> 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<Forest> forestZones = new HashSet<>();
Forest forest = new Forest(0, Forest.Kind.PLAIN);
forestZones.add(forest);
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> 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<Forest> forestZones = new HashSet<>();
Forest forest = new Forest(0, Forest.Kind.PLAIN);
forestZones.add(forest);
List<PlayerColor> occupants = new ArrayList<>();
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));
ZonePartition.Builder<Forest> builder = new ZonePartition.Builder<>(zonePartition);

List<PlayerColor> newOccupants = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Forest> newForestArea = new Area<>(forestZones, newOccupants, 3);

builder.addInitialOccupant(forest, PlayerColor.YELLOW);
Set<Area> expectedAreas = new HashSet<>();
expectedAreas.add(newForestArea);

assertEquals(expectedAreas, builder.build().areas());
}

@Test
void removeOccupantWorks() {
Set<Forest> forestZones = new HashSet<>();
Forest forest = new Forest(0, Forest.Kind.PLAIN);
forestZones.add(forest);
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));
ZonePartition.Builder<Forest> builder = new ZonePartition.Builder<>(zonePartition);

List<PlayerColor> newOccupants = new ArrayList<>();
Area<Forest> newForestArea = new Area<>(forestZones, newOccupants, 3);

builder.removeOccupant(forest, PlayerColor.YELLOW);
Set<Area> expectedAreas = new HashSet<>();
expectedAreas.add(newForestArea);

assertEquals(expectedAreas, builder.build().areas());
}

@Test
void removeAllOccupantsOfThrowsOnUnknownArea() {
Set<Forest> forestZones = new HashSet<>();
Forest forest = new Forest(0, Forest.Kind.PLAIN);
forestZones.add(forest);
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);

ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));
ZonePartition.Builder<Forest> builder = new ZonePartition.Builder<>(zonePartition);

Forest unknownForest = new Forest(1, Forest.Kind.WITH_MENHIR);
Area<Forest> unknownArea = new Area<>(Set.of(unknownForest), occupants, 2);

assertThrows(IllegalArgumentException.class, () -> builder.removeAllOccupantsOf(unknownArea));
}

@Test
void removeAllOccupantsOfWorks() {
Set<Forest> forestZones = new HashSet<>();
Forest forest = new Forest(0, Forest.Kind.PLAIN);
forestZones.add(forest);
List<PlayerColor> occupants = new ArrayList<>(List.of(PlayerColor.YELLOW, PlayerColor.GREEN));
Area<Forest> forestArea = new Area<>(forestZones, occupants, 3);
ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea));
ZonePartition.Builder<Forest> builder = new ZonePartition.Builder<>(zonePartition);

List<PlayerColor> newOccupants = new ArrayList<>();
Area<Forest> newForestArea = new Area<>(forestZones, newOccupants, 3);

builder.removeAllOccupantsOf(forestArea);
Set<Area> expectedAreas = new HashSet<>();
expectedAreas.add(newForestArea);

assertEquals(expectedAreas, builder.build().areas());
}
@Test
void unionWorksWithDifferentAreas() {
Set<Forest> forestZones1 = new HashSet<>();
Forest forest1 = new Forest(0, Forest.Kind.PLAIN);
forestZones1.add(forest1);
List<PlayerColor> occupants1 = new ArrayList<>(List.of(PlayerColor.YELLOW));
Area<Forest> forestArea1 = new Area<>(forestZones1, occupants1, 3);

Set<Forest> forestZones2 = new HashSet<>();
Forest forest2 = new Forest(1, Forest.Kind.WITH_MENHIR);
forestZones2.add(forest2);
List<PlayerColor> occupants2 = new ArrayList<>(List.of(PlayerColor.RED));
Area<Forest> forestArea2 = new Area<>(forestZones2, occupants2, 3);

ZonePartition<Forest> zonePartition = new ZonePartition<>(Set.of(forestArea1, forestArea2));
ZonePartition.Builder<Forest> builder = new ZonePartition.Builder<>(zonePartition);

Set<Forest> expectedForestZones = new HashSet<>();
expectedForestZones.add(forest1);
expectedForestZones.add(forest2);
List<PlayerColor> expectedOccupants = new ArrayList<>();
expectedOccupants.add(PlayerColor.YELLOW);
expectedOccupants.add(PlayerColor.RED);
Area<Forest> expectedArea = new Area<>(expectedForestZones, expectedOccupants, 4);

builder.union(forest1, forest2);

assertEquals(Set.of(expectedArea), builder.build().areas());
}

}

0 comments on commit 3364879

Please sign in to comment.