Skip to content

Commit

Permalink
WIP step 4 - ZonePartitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Mar 8, 2024
1 parent 22d6fa6 commit 1c14e80
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

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

3 changes: 1 addition & 2 deletions src/ch/epfl/chacun/PlacedTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public Set<Occupant> potentialOccupants() {
if (!(zone instanceof Zone.Lake)) {
potentialOccupants.add(new Occupant(Occupant.Kind.PAWN, zone.id()));
}
// A hut can only be placed on a lake if it is connected to a river
// or on a river if there's no lake
// A hut can only be placed on a lake or on a river if there's no lake
if (zone instanceof Zone.Lake || (zone instanceof Zone.River river && !river.hasLake())) {
potentialOccupants.add(new Occupant(Occupant.Kind.HUT, zone.id()));
}
Expand Down
150 changes: 150 additions & 0 deletions src/ch/epfl/chacun/TextMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package ch.epfl.chacun;

import java.util.Map;
import java.util.Set;


public interface TextMaker {
/**
* Returns the name of the player of the given color.
*
* @param playerColor the color of the player
* @return the name of the player
*/
String playerName(PlayerColor playerColor);

/**
* Returns the textual representation of the given number of points (e.g., "3 points").
*
* @param points the number of points
* @return the textual representation of the number of points
*/
String points(int points);

/**
* Returns the text of a message declaring that a player has closed a forest with a menhir.
*
* @param player the player who closed the forest
* @return the text of the message
*/
String playerClosedForestWithMenhir(PlayerColor player);

/**
* Returns the text of a message declaring that the majority occupants of a newly
* closed forest, consisting of a certain number of tiles and containing a certain number of mushroom groups,
* have won the corresponding points.
*
* @param scorers the majority occupants of the forest
* @param points the points won
* @param mushroomGroupCount the number of mushroom groups that the forest contains
* @param tileCount the number of tiles that make up the forest
* @return the text of the message
*/
String playersScoredForest(Set<PlayerColor> scorers, int points, int mushroomGroupCount, int tileCount);

/**
* Returns the text of a message declaring that the majority occupants of a newly
* closed river, consisting of a certain number of tiles and containing a certain number of fish,
* have won the corresponding points.
*
* @param scorers the majority occupants of the river
* @param points the points won
* @param fishCount the number of fish swimming in the river or adjacent lakes
* @param tileCount the number of tiles that make up the river
* @return the text of the message
*/
String playersScoredRiver(Set<PlayerColor> scorers, int points, int fishCount, int tileCount);

/**
* Returns the text of a message declaring that a player has placed the pit trap in a meadow containing,
* on the 8 neighboring tiles of the pit, certain animals, and won the corresponding points.
*
* @param scorer the player who placed the pit trap
* @param points the points won
* @param animals the animals present in the same meadow as the pit and on the 8 neighboring tiles
* @return the text of the message
*/
String playerScoredHuntingTrap(PlayerColor scorer, int points, Map<Animal.Kind, Integer> animals);

/**
* Returns the text of a message declaring that a player has placed the logboat in a river system
* containing a certain number of lakes, and won the corresponding points.
*
* @param scorer the player who placed the logboat
* @param points the points won
* @param lakeCount the number of lakes accessible to the logboat
* @return the text of the message
*/
String playerScoredLogboat(PlayerColor scorer, int points, int lakeCount);

/**
* Returns the text of a message declaring that the majority occupants of a meadow containing certain
* animals have won the corresponding points.
*
* @param scorers the majority occupants of the meadow
* @param points the points won
* @param animals the animals present in the meadow (excluding those previously cancelled)
* @return the text of the message
*/
String playersScoredMeadow(Set<PlayerColor> scorers, int points, Map<Animal.Kind, Integer> animals);

/**
* Returns the text of a message declaring that the majority occupants of a river system
* containing a certain number of fish have won the corresponding points.
*
* @param scorers the majority occupants of the river system
* @param points the points won
* @param fishCount the number of fish swimming in the river system
* @return the text of the message
*/
String playersScoredRiverSystem(Set<PlayerColor> scorers, int points, int fishCount);

/**
* Returns the text of a message declaring that the majority occupants of a meadow containing the
* large pit trap and, on the 8 neighboring tiles of it, certain animals, have won the
* corresponding points.
*
* @param scorers the majority occupants of the meadow containing the pit trap
* @param points the points won
* @param animals the animals present on the tiles neighboring the pit (excluding those previously cancelled)
* @return the text of the message
*/
String playersScoredPitTrap(Set<PlayerColor> scorers, int points, Map<Animal.Kind, Integer> animals);

/**
* Returns the text of a message declaring that the majority occupants of a river system
* containing the raft have won the corresponding points.
*
* @param scorers the majority occupants of the river system containing the raft
* @param points the points won
* @param lakeCount the number of lakes contained in the river system
* @return the text of the message
*/
String playersScoredRaft(Set<PlayerColor> scorers, int points, int lakeCount);

/**
* Returns the text of a message declaring that one or more players have won the game, with a
* certain number of points.
*
* @param winners the set of players who have won the game
* @param points the points of the winners
* @return the text of the message
*/
String playersWon(Set<PlayerColor> winners, int points);

/**
* Returns a text asking the current player to click on the occupant they wish to place, or on the text
* of the message if they do not wish to place any occupant.
*
* @return the text in question
*/
String clickToOccupy();

/**
* Returns a text asking the current player to click on the pawn they wish to take back, or on the text
* of the message if they do not wish to take back any pawn.
*
* @return the text in question
*/
String clickToUnoccupy();
}
125 changes: 120 additions & 5 deletions src/ch/epfl/chacun/ZonePartitions.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
package ch.epfl.chacun;

/**
* Represents the partition which regroups the four partitions of the different zones.
*
* @param forests the forests partition
* @param meadows the meadows partition
* @param rivers the rivers partition
* @param riverSystems the river systems partition
* @author Maxence Espagnet (sciper: 372808)
* @author Balthazar Baillat (sciper: 373420)
*/
public record ZonePartitions(ZonePartition<Zone.Forest> forests, ZonePartition<Zone.Meadow> meadows,
ZonePartition<Zone.River> rivers, ZonePartition<Zone.Lake> lakes,
ZonePartition<Zone.Water> riverSystems) {
public final static ZonePartitions EMPTY = new ZonePartitions(new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>());
ZonePartition<Zone.River> rivers, ZonePartition<Zone.Water> riverSystems) {

public static final class Builder {
// Represent a group of four empty partitions
public final static ZonePartitions EMPTY = new ZonePartitions(new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>());

/**
* Represents the builder of zone partitions.
*/
public static final class Builder {
// The builder of the forests partition
private ZonePartition.Builder<Zone.Forest> forests;
// The builder of the meadows partition
private ZonePartition.Builder<Zone.Meadow> meadows;
// The builder of the rivers partition
private ZonePartition.Builder<Zone.River> rivers;
// The builder of the river systems partition
private ZonePartition.Builder<Zone.Water> riverSystems;

/**
* Returns a new builder whose four partitions are initially identical to those of the given
* group of four partitions.
*
* @param initial the initial group of four partitions
*/
public Builder(ZonePartitions initial) {
this.forests = new ZonePartition.Builder<>(initial.forests);
this.meadows = new ZonePartition.Builder<>(initial.meadows);
this.rivers = new ZonePartition.Builder<>(initial.rivers);
this.riverSystems = new ZonePartition.Builder<>(initial.riverSystems);
}

/**
* Adds to the partitions the areas corresponding to the zones of the given tile.
*
* @param tile the tile
*/
public void addTile(Tile tile) {
int[] openConnections = new int[10];
// Calculate the number of open connections for each zone
Expand Down Expand Up @@ -48,11 +76,98 @@ public void addTile(Tile tile) {
riverSystems.union(r.lake(), r);
}
// A lake should not be in the side zones
default -> throw new IllegalArgumentException();
default -> throw new IllegalArgumentException("A lake shouldn't be in the side zones");
}
}
}

public void connectSides(TileSide s1, TileSide s2) {
switch (s1) {
case TileSide.Meadow(Zone.Meadow m1) when s2 instanceof TileSide.Meadow(Zone.Meadow m2) -> {
meadows.union(m1, m2);
}
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);
}
}
default -> throw new IllegalArgumentException("The tile sides are not of the same kind");
}
}


/**
* Adds an initial occupant, of the given type and belonging to the given player,
* to the area containing the given zone.
*
* @param player the player
* @param occupantKind the occupant kind
* @param occupiedZone the occupied zone
* @throws IllegalArgumentException if the occupant cannot be placed on the desired zone
*/
public void addInitialOccupant(PlayerColor player, Occupant.Kind occupantKind, Zone occupiedZone) {
if (occupantKind == Occupant.Kind.PAWN) {
switch (occupiedZone) {
case Zone.Forest f -> forests.addInitialOccupant(f, player);
case Zone.Meadow m -> meadows.addInitialOccupant(m, player);
case Zone.River r -> rivers.addInitialOccupant(r, player);
default -> throw new IllegalArgumentException("A pawn cannot be placed on a lake");
}
} else {
switch (occupiedZone) {
case Zone.River river when !river.hasLake() -> rivers.addInitialOccupant(river, player);
case Zone.River river -> riverSystems.addInitialOccupant(river.lake(), player);
default -> throw new IllegalArgumentException("A hut can only be on a lake or a river");
}
}
}

/**
* Removes an occupant (a pawn) belonging to the given player from the area containing the given zone.
*
* @param player the player
* @param occupiedZone the occupied zone
* @throws IllegalArgumentException if the zone is a lake
*/
public void removePawn(PlayerColor player, Zone occupiedZone) {
switch (occupiedZone) {
case Zone.Forest f -> forests.removeOccupant(f, player);
case Zone.Meadow m -> meadows.removeOccupant(m, player);
case Zone.River r -> rivers.removeOccupant(r, player);
default -> throw new IllegalArgumentException("A pawn cannot be removed from a lake");
}
}

/**
* Removes all occupants (pawns playing the role of gatherers) from the given forest.
*
* @param forest the forest to remove all pawns from
*/
public void clearGatherers(Area<Zone.Forest> forest) {
forests.removeAllOccupantsOf(forest);
}

/**
* Removes all occupants (pawns playing the role of fishers) from the given river.
*
* @param river the river to remove all pawns from
*/
public void clearFishers(Area<Zone.River> river) {
rivers.removeAllOccupantsOf(river);
}

/**
* Builds the group of four partitions under construction.
*
* @return the group of four partitions under construction
*/
public ZonePartitions build() {
return new ZonePartitions(forests.build(), meadows.build(), rivers.build(), riverSystems.build());
}

}


Expand Down

0 comments on commit 1c14e80

Please sign in to comment.