Skip to content

Commit

Permalink
Refactor addTile and connectSides + implement ZonePartitionsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
balthaB committed Mar 10, 2024
1 parent 77cc600 commit 670165e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/ch/epfl/chacun/ZonePartitions.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,22 @@ public void addTile(Tile tile) {
// Create the union between the river and the lake
riverSystems.addSingleton(r, openConnections[r.id()]);
riverSystems.addSingleton(r.lake(), openConnections[r.lake().id()]);
riverSystems.union(r.lake(), r);
riverSystems.union(r, r.lake());
}
// A lake should not be in the side zones
default -> throw new IllegalArgumentException("A lake shouldn't be in the side zones");
}
}
}

/**
* Connects each other the two given tile sides by connecting the corresponding areas.
*
* @param s1 the first tile side
* @param s2 the second tile side
* @throws IllegalArgumentException if the two given tile sides are not of the same kind
*/

public void connectSides(TileSide s1, TileSide s2) {
switch (s1) {
case TileSide.Meadow(Zone.Meadow m1) when s2 instanceof TileSide.Meadow(Zone.Meadow m2) -> {
Expand All @@ -89,10 +97,11 @@ public void connectSides(TileSide s1, TileSide s2) {
case TileSide.Forest(Zone.Forest f1) when s2 instanceof TileSide.Forest(Zone.Forest f2) -> {
forests.union(f1, f2);
}
case TileSide.River(Zone.Meadow m1, Zone.River r1, Zone.Meadow m2) -> {
if (s2 instanceof TileSide.River(Zone.Meadow m3, Zone.River r2, Zone.Meadow m4)) {
rivers.union(r1, r2);
}
case TileSide.River(Zone.Meadow m3, Zone.River r1, Zone.Meadow m4)
when s2 instanceof TileSide.River(Zone.Meadow m5, Zone.River r2, Zone.Meadow m6) -> {
rivers.union(r1, r2);
meadows.union(m3, m5);
meadows.union(m4, m6);
}
default -> throw new IllegalArgumentException("The tile sides are not of the same kind");
}
Expand Down
54 changes: 54 additions & 0 deletions test/ch/epfl/chacun/ZonePartitionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 org.junit.jupiter.api.Assertions.*;

public class ZonePartitionsTest {

@Test
void addTileWorks() {
Zone.Meadow meadow0 = new Zone.Meadow(0, new ArrayList<>(List.of(new Animal(0
, Animal.Kind.AUROCHS))), null);
Zone.Forest forest1 = new Zone.Forest(1, Zone.Forest.Kind.WITH_MENHIR);
Zone.Meadow meadow2 = new Zone.Meadow(2, new ArrayList<>(), null);
Zone.Lake lake8 = new Zone.Lake(8, 1, null);
Zone.River river3 = new Zone.River(3, 0, lake8);

TileSide.Meadow nSide = new TileSide.Meadow(meadow0);
TileSide.Forest eSide = new TileSide.Forest(forest1);
TileSide.Forest sSide = new TileSide.Forest(forest1);
TileSide.River wSide = new TileSide.River(meadow2, river3, meadow0);

Tile tile = new Tile(56, Tile.Kind.START, nSide, eSide, sSide, wSide);

Set<Zone.Meadow> meadows1 = new HashSet<>(Set.of(meadow0));
Set<Zone.Meadow> meadows2 = new HashSet<>(Set.of(meadow2));
Set<Zone.Forest> forests = new HashSet<>(Set.of(forest1));
Set<Zone.River> rivers = new HashSet<>(Set.of(river3));
Set<Zone.Lake> lakes = new HashSet<>(Set.of(lake8));
Set<Zone.Water> waterZones = new HashSet<>(Set.of(river3, lake8));

Area<Zone.Meadow> meadowArea1 = new Area<>(meadows1, new ArrayList<>(), 2);
Area<Zone.Meadow> meadowArea2 = new Area<>(meadows2, new ArrayList<>(), 1);
Area<Zone.Forest> forestArea = new Area<>(forests, new ArrayList<>(), 2);
Area<Zone.River> riverArea = new Area<>(rivers, new ArrayList<>(), 1);
Area<Zone.Water> waterArea = new Area<>(waterZones, new ArrayList<>(), 1);

ZonePartition<Zone.Meadow> meadowZonePartition = new ZonePartition<>(Set.of(meadowArea1, meadowArea2));
ZonePartition<Zone.Forest> forestZonePartition = new ZonePartition<>(Set.of(forestArea));
ZonePartition<Zone.River> riverZonePartition = new ZonePartition<>(Set.of(riverArea));
ZonePartition<Zone.Water> waterZonePartition = new ZonePartition<>(Set.of(waterArea));

ZonePartitions expectedZonePartitions = new ZonePartitions(forestZonePartition, meadowZonePartition,
riverZonePartition, waterZonePartition);
ZonePartitions.Builder testBuilder = new ZonePartitions.Builder(ZonePartitions.EMPTY);
testBuilder.addTile(tile);

assertEquals(expectedZonePartitions, testBuilder.build());
}
}

0 comments on commit 670165e

Please sign in to comment.