From 1c14e80ad66f1e2771858dcb24b2efe7001b11fc Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 8 Mar 2024 14:39:45 +0100 Subject: [PATCH] WIP step 4 - ZonePartitions --- .idea/.gitignore | 2 + src/ch/epfl/chacun/PlacedTile.java | 3 +- src/ch/epfl/chacun/TextMaker.java | 150 +++++++++++++++++++++++++ src/ch/epfl/chacun/ZonePartitions.java | 125 ++++++++++++++++++++- 4 files changed, 273 insertions(+), 7 deletions(-) create mode 100644 src/ch/epfl/chacun/TextMaker.java diff --git a/.idea/.gitignore b/.idea/.gitignore index 13566b8..a9d7db9 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -6,3 +6,5 @@ # Datasource local storage ignored files /dataSources/ /dataSources.local.xml +# GitHub Copilot persisted chat sessions +/copilot/chatSessions diff --git a/src/ch/epfl/chacun/PlacedTile.java b/src/ch/epfl/chacun/PlacedTile.java index 848d66a..d9a461f 100644 --- a/src/ch/epfl/chacun/PlacedTile.java +++ b/src/ch/epfl/chacun/PlacedTile.java @@ -147,8 +147,7 @@ public Set 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())); } diff --git a/src/ch/epfl/chacun/TextMaker.java b/src/ch/epfl/chacun/TextMaker.java new file mode 100644 index 0000000..68b9a48 --- /dev/null +++ b/src/ch/epfl/chacun/TextMaker.java @@ -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 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 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 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 scorers, int points, Map 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 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 scorers, int points, Map 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 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 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(); +} diff --git a/src/ch/epfl/chacun/ZonePartitions.java b/src/ch/epfl/chacun/ZonePartitions.java index f6c0fec..3e48fdf 100644 --- a/src/ch/epfl/chacun/ZonePartitions.java +++ b/src/ch/epfl/chacun/ZonePartitions.java @@ -1,17 +1,40 @@ 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 forests, ZonePartition meadows, - ZonePartition rivers, ZonePartition lakes, - ZonePartition riverSystems) { - public final static ZonePartitions EMPTY = new ZonePartitions(new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>(), new ZonePartition<>()); + ZonePartition rivers, ZonePartition 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 forests; + // The builder of the meadows partition private ZonePartition.Builder meadows; + // The builder of the rivers partition private ZonePartition.Builder rivers; + // The builder of the river systems partition private ZonePartition.Builder 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); @@ -19,6 +42,11 @@ public Builder(ZonePartitions initial) { 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 @@ -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 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 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()); + } + }