From 1db4b8177f0ab000291d584f5595c10af93636e6 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 15:32:10 +0100 Subject: Add unit 5 method stubs and documentation --- src/ch/epfl/xblast/Cell.java | 10 +++ src/ch/epfl/xblast/SubCell.java | 10 +++ src/ch/epfl/xblast/server/GameState.java | 93 ++++++++++++++++++++++++-- test/ch/epfl/xblast/namecheck/NameCheck05.java | 31 +++++++++ 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 test/ch/epfl/xblast/namecheck/NameCheck05.java diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java index d31a889..098c19c 100644 --- a/src/ch/epfl/xblast/Cell.java +++ b/src/ch/epfl/xblast/Cell.java @@ -174,6 +174,16 @@ public final class Cell { return (((Cell) that).x == this.x && ((Cell) that).y == this.y); } + /** + * Returns the hash code for this Cell, given by its row-major index. + * + * @return the hash code + */ + @Override + public int hashCode() { + return 0; // TODO + } + /** * Returns a String representation of the coordinates of the Cell. * diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java index a95e65e..3b7fdb1 100644 --- a/src/ch/epfl/xblast/SubCell.java +++ b/src/ch/epfl/xblast/SubCell.java @@ -136,6 +136,16 @@ public final class SubCell { return (((SubCell) that).x == this.x && ((SubCell) that).y == this.y); } + /** + * Returns the hash code of this SubCell, given by its row-major index. + * + * @return the hash code + */ + @Override + public int hashCode() { + return 0; // TODO + } + /** * Returns a String representation of the coordinates of the SubCell. * diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index ef792b6..a415301 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -3,12 +3,10 @@ package ch.epfl.xblast.server; import ch.epfl.cs108.Sq; import ch.epfl.xblast.ArgumentChecker; import ch.epfl.xblast.Cell; +import ch.epfl.xblast.Direction; import ch.epfl.xblast.PlayerID; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -143,4 +141,91 @@ public final class GameState { .collect(Collectors.toList()); } + /** + * Returns a mapping of Cells and their Bomb. + * + * @return the map of bombs + */ + public Map bombedCells() { + return null; // TODO + } + + /** + * Returns the set of cells which contains at least one explosion particle. + * + * @return the set of blasted cells + */ + public Set blastedCells() { + return null; // TODO + } + + /** + * Computes and returns the game state for the next tick, given the current state and the given events. + * + * @param speedChangeEvents the speed change events + * @param bombDropEvents the bomb drop events + * @return the next game state + */ + public GameState next(Map> speedChangeEvents, Set bombDropEvents) { + return null; // TODO + } + + /** + * Computes and returns the next board state of the given board according to the given events. + * + * @param board0 the previous board + * @param consumedBonuses the set of consumed bonuses + * @param blastedCells1 the set of newly blasted cells + * @return the next board + */ + private static Board nextBoard(Board board0, Set consumedBonuses, Set blastedCells1) { + return null; // TODO + } + + /** + * Computes and returns the next player list given the current one and the given events and states. + * + * @param players0 the previous player list + * @param playerBonuses the map of player bonuses + * @param bombedCells1 the set of newly bombed cells + * @param board1 the newly updated board + * @param blastedCells1 the set of newly blasted cells + * @param speedChangeEvents the speed change events + * @return the next player list + */ + private static List nextPlayers( + List players0, + Map playerBonuses, + Set bombedCells1, + Board board1, + Set blastedCells1, + Map> speedChangeEvents) { + return null; // TODO + } + + /** + * Computes and returns the next state of the given explosion list. + * + * @param explosions0 the previous explosion state + * @return the next explosion state + */ + private static List>> nextExplosions(List>> explosions0) { + return null; // TODO + } + + /** + * Computes and returns the list of newly dropped bombs by players according to the given player states and events. + * + * @param players0 the previous player states + * @param bombDropEvents the bomb drop events + * @param bombs0 the previous bomb state + * @return the next bomb states + */ + private static List newlyDroppedBombs( + List players0, + Set bombDropEvents, + List bombs0) { + return null; // TODO + } + } diff --git a/test/ch/epfl/xblast/namecheck/NameCheck05.java b/test/ch/epfl/xblast/namecheck/NameCheck05.java new file mode 100644 index 0000000..e8c30f0 --- /dev/null +++ b/test/ch/epfl/xblast/namecheck/NameCheck05.java @@ -0,0 +1,31 @@ +package ch.epfl.xblast.namecheck; + +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.Direction; +import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.server.Bomb; +import ch.epfl.xblast.server.GameState; + +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Classe abstraite utilisant tous les éléments de l'étape 5, pour essayer de + * garantir que ceux-ci ont le bon nom et les bons types. Attention, ceci n'est + * pas un test unitaire, et n'a pas pour but d'être exécuté! + * + * @author EPFL + */ +abstract class NameCheck05 { + + void checkGameState(GameState s, + Map> speedChangeEvents, + Set bombDropEvents) { + Map b = s.bombedCells(); + Set l = s.blastedCells(); + s = s.next(speedChangeEvents, bombDropEvents); + System.out.println("b:" + b + ", l: " + l); + } + +} -- cgit v1.2.3 From 05dc6bb3d944a3edb90f62dfbe8b85b044839ee7 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 15:36:32 +0100 Subject: Implement Cell and SubCell hashCode() methods --- src/ch/epfl/xblast/Cell.java | 2 +- src/ch/epfl/xblast/SubCell.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java index 098c19c..a8a5a08 100644 --- a/src/ch/epfl/xblast/Cell.java +++ b/src/ch/epfl/xblast/Cell.java @@ -181,7 +181,7 @@ public final class Cell { */ @Override public int hashCode() { - return 0; // TODO + return this.rowMajorIndex(); } /** diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java index 3b7fdb1..8a25997 100644 --- a/src/ch/epfl/xblast/SubCell.java +++ b/src/ch/epfl/xblast/SubCell.java @@ -143,7 +143,7 @@ public final class SubCell { */ @Override public int hashCode() { - return 0; // TODO + return this.rowMajorIndex(); } /** @@ -156,4 +156,13 @@ public final class SubCell { return String.format("(%d,%d)", this.x, this.y); } + /** + * Returns the index of the SubCell (major ordered). + * + * @return the index of the SubCell + */ + private int rowMajorIndex() { + return this.y * SUB_COLUMNS + this.x; + } + } -- cgit v1.2.3 From c6adb037f5a3d4edd1b8038113b37e35f2161555 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 15:54:23 +0100 Subject: Return value disambiguation --- src/ch/epfl/xblast/server/GameState.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index a415301..fd4b917 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -219,7 +219,7 @@ public final class GameState { * @param players0 the previous player states * @param bombDropEvents the bomb drop events * @param bombs0 the previous bomb state - * @return the next bomb states + * @return the newly dropped bombs */ private static List newlyDroppedBombs( List players0, -- cgit v1.2.3 From fab4253bba72b035fe36124d66d10799b224b6cf Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 20:02:13 +0100 Subject: Reorder methods and rename parameters according to instructions --- src/ch/epfl/xblast/server/GameState.java | 130 +++++++++++++++---------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index fd4b917..2c6f372 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -28,22 +28,80 @@ public final class GameState { /** * Compute the next state of a blast. * - * @param blasts existing particles - * @param board the game's board - * @param explosions active explosions + * @param blasts0 existing particles + * @param board0 the game's board + * @param explosions0 active explosions * @return the position of the explosion's particles for the next state. */ - private static List> nextBlasts(List> blasts, Board board, List>> explosions) { + private static List> nextBlasts(List> blasts0, Board board0, List>> explosions0) { return Stream.concat( - blasts.stream() + blasts0.stream() .filter(blastSeq -> !blastSeq.tail().isEmpty()) - .filter(blastSeq -> board.blockAt(blastSeq.head()).isFree()) + .filter(blastSeq -> board0.blockAt(blastSeq.head()).isFree()) .map(Sq::tail), - explosions.stream() + explosions0.stream() .map(Sq::head) ).collect(Collectors.toList()); } + /** + * Computes and returns the next board state of the given board according to the given events. + * + * @param board0 the previous board + * @param consumedBonuses the set of consumed bonuses + * @param blastedCells1 the set of newly blasted cells + * @return the next board + */ + private static Board nextBoard(Board board0, Set consumedBonuses, Set blastedCells1) { + return null; // TODO + } + + /** + * Computes and returns the next player list given the current one and the given events and states. + * + * @param players0 the previous player list + * @param playerBonuses the map of player bonuses + * @param bombedCells1 the set of newly bombed cells + * @param board1 the newly updated board + * @param blastedCells1 the set of newly blasted cells + * @param speedChangeEvents the speed change events + * @return the next player list + */ + private static List nextPlayers( + List players0, + Map playerBonuses, + Set bombedCells1, + Board board1, + Set blastedCells1, + Map> speedChangeEvents) { + return null; // TODO + } + + /** + * Computes and returns the next state of the given explosion list. + * + * @param explosions0 the previous explosion state + * @return the next explosion state + */ + private static List>> nextExplosions(List>> explosions0) { + return null; // TODO + } + + /** + * Computes and returns the list of newly dropped bombs by players according to the given player states and events. + * + * @param players0 the previous player states + * @param bombDropEvents the bomb drop events + * @param bombs0 the previous bomb state + * @return the newly dropped bombs + */ + private static List newlyDroppedBombs( + List players0, + Set bombDropEvents, + List bombs0) { + return null; // TODO + } + /** * Instantiates a new GameState. * @@ -170,62 +228,4 @@ public final class GameState { return null; // TODO } - /** - * Computes and returns the next board state of the given board according to the given events. - * - * @param board0 the previous board - * @param consumedBonuses the set of consumed bonuses - * @param blastedCells1 the set of newly blasted cells - * @return the next board - */ - private static Board nextBoard(Board board0, Set consumedBonuses, Set blastedCells1) { - return null; // TODO - } - - /** - * Computes and returns the next player list given the current one and the given events and states. - * - * @param players0 the previous player list - * @param playerBonuses the map of player bonuses - * @param bombedCells1 the set of newly bombed cells - * @param board1 the newly updated board - * @param blastedCells1 the set of newly blasted cells - * @param speedChangeEvents the speed change events - * @return the next player list - */ - private static List nextPlayers( - List players0, - Map playerBonuses, - Set bombedCells1, - Board board1, - Set blastedCells1, - Map> speedChangeEvents) { - return null; // TODO - } - - /** - * Computes and returns the next state of the given explosion list. - * - * @param explosions0 the previous explosion state - * @return the next explosion state - */ - private static List>> nextExplosions(List>> explosions0) { - return null; // TODO - } - - /** - * Computes and returns the list of newly dropped bombs by players according to the given player states and events. - * - * @param players0 the previous player states - * @param bombDropEvents the bomb drop events - * @param bombs0 the previous bomb state - * @return the newly dropped bombs - */ - private static List newlyDroppedBombs( - List players0, - Set bombDropEvents, - List bombs0) { - return null; // TODO - } - } -- cgit v1.2.3 From 3c47558d60a05f3f46674da610a7a550a559b4be Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 20:13:21 +0100 Subject: Implement bombedCells() and blastedCells() mapping functions --- src/ch/epfl/xblast/server/GameState.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 2c6f372..cbe29b5 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -7,6 +7,7 @@ import ch.epfl.xblast.Direction; import ch.epfl.xblast.PlayerID; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -205,7 +206,9 @@ public final class GameState { * @return the map of bombs */ public Map bombedCells() { - return null; // TODO + return this.bombs + .stream() + .collect(Collectors.toMap(Bomb::position, Function.identity())); } /** @@ -214,7 +217,10 @@ public final class GameState { * @return the set of blasted cells */ public Set blastedCells() { - return null; // TODO + return this.blasts + .stream() + .map(Sq::head) + .collect(Collectors.toSet()); } /** -- cgit v1.2.3 From 47de23b97f65b9c97e7bce4bb2f4a496e5190bea Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 20:36:43 +0100 Subject: Make filters and mappers static for easier future state computation --- src/ch/epfl/xblast/server/GameState.java | 51 ++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index cbe29b5..d750732 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -27,7 +27,42 @@ public final class GameState { private final List> blasts; /** - * Compute the next state of a blast. + * Filters the given list of players and returns the lively ones. + * + * @param players a list of players + * @return the list of alive players + */ + private static List alivePlayers(List players) { + return players.stream() + .filter(Player::isAlive) + .collect(Collectors.toList()); + } + + /** + * Maps the given bombs to their position. + * + * @param bombs a list of bombs + * @return a map of positions and their bombs + */ + private static Map bombedCells(List bombs) { + return bombs.stream() + .collect(Collectors.toMap(Bomb::position, Function.identity())); + } + + /** + * Returns a set of cells that contains at least one blast in the given blasts sequences. + * + * @param blasts the list of blast sequences + * @return the set of blasted cells + */ + private static Set blastedCells(List> blasts) { + return blasts.stream() + .map(Sq::head) + .collect(Collectors.toSet()); + } + + /** + * Computes the next state of a blast. * * @param blasts0 existing particles * @param board0 the game's board @@ -194,10 +229,7 @@ public final class GameState { * @return a list of the alive players */ public List alivePlayers() { - return this.players - .stream() - .filter(Player::isAlive) - .collect(Collectors.toList()); + return GameState.alivePlayers(this.players); } /** @@ -206,9 +238,7 @@ public final class GameState { * @return the map of bombs */ public Map bombedCells() { - return this.bombs - .stream() - .collect(Collectors.toMap(Bomb::position, Function.identity())); + return GameState.bombedCells(this.bombs); } /** @@ -217,10 +247,7 @@ public final class GameState { * @return the set of blasted cells */ public Set blastedCells() { - return this.blasts - .stream() - .map(Sq::head) - .collect(Collectors.toSet()); + return GameState.blastedCells(this.blasts); } /** -- cgit v1.2.3 From 1e6848b2a40b331cad4fea9e89e29df621402222 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 22:40:17 +0100 Subject: Reorder static and object members --- src/ch/epfl/xblast/server/GameState.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index d750732..e15f0bf 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -19,13 +19,6 @@ import java.util.stream.Stream; */ public final class GameState { - private final int ticks; - private final Board board; - private final List players; - private final List bombs; - private final List>> explosions; - private final List> blasts; - /** * Filters the given list of players and returns the lively ones. * @@ -138,6 +131,13 @@ public final class GameState { return null; // TODO } + private final int ticks; + private final Board board; + private final List players; + private final List bombs; + private final List>> explosions; + private final List> blasts; + /** * Instantiates a new GameState. * -- cgit v1.2.3 From aa317f43ad2c714cecbae5202bcfc2c2ab68172e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 22:56:27 +0100 Subject: Implement conflict resolution protocol --- src/ch/epfl/xblast/server/GameState.java | 43 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index e15f0bf..6e7f8c4 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -1,10 +1,7 @@ package ch.epfl.xblast.server; import ch.epfl.cs108.Sq; -import ch.epfl.xblast.ArgumentChecker; -import ch.epfl.xblast.Cell; -import ch.epfl.xblast.Direction; -import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.*; import java.util.*; import java.util.function.Function; @@ -19,6 +16,20 @@ import java.util.stream.Stream; */ public final class GameState { + /** + * The list of player priority order permutations. + */ + private static final List> PLAYER_PRIORITY_ORDERS = GameState.buildPlayerPriorityOrderList(); + + /** + * Builds and returns the player priority order permutations. + * + * @return the list of player priority orders + */ + private static List> buildPlayerPriorityOrderList() { + return Lists.permutations(Arrays.asList(PlayerID.values())); + } + /** * Filters the given list of players and returns the lively ones. * @@ -261,4 +272,28 @@ public final class GameState { return null; // TODO } + /** + * Returns the current player priority order permutation. + * + * @return the player priority order + */ + private List currentPlayerPriorityOrder() { + int priorityIndex = this.ticks % GameState.PLAYER_PRIORITY_ORDERS.size(); + return GameState.PLAYER_PRIORITY_ORDERS.get(priorityIndex); + } + + /** + * Resolves a conflict according to the current priority order. + * + * @param claimants the list of claimants + * @return the highest priority player + */ + private PlayerID resolveConflict(List claimants) { + return this.currentPlayerPriorityOrder() + .stream() + .filter(claimants::contains) + .findFirst() + .get(); + } + } -- cgit v1.2.3 From 6f3a8a0946946e28213967a98b5f080e3cb2a5e1 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 16 Mar 2016 06:55:13 +0100 Subject: Add null and empty list check --- src/ch/epfl/xblast/server/GameState.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 6e7f8c4..0512510 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -289,6 +289,8 @@ public final class GameState { * @return the highest priority player */ private PlayerID resolveConflict(List claimants) { + if (claimants == null || claimants.isEmpty()) throw new IllegalArgumentException(); + return this.currentPlayerPriorityOrder() .stream() .filter(claimants::contains) -- cgit v1.2.3 From 383079bb6082944391f1841fca38edd2e5af54fe Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 16 Mar 2016 07:09:02 +0100 Subject: Add random bonus generator --- src/ch/epfl/xblast/server/GameState.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 0512510..5386311 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -21,6 +21,22 @@ public final class GameState { */ private static final List> PLAYER_PRIORITY_ORDERS = GameState.buildPlayerPriorityOrderList(); + /** + * The list of bonuses to choose randomly from. + */ + private static final Block[] RANDOM_BONUSES = new Block[]{Block.BONUS_BOMB, Block.BONUS_RANGE, Block.FREE}; + + /** + * The seed used for random value generation. + * Constant between executions to make tests reproducible. + */ + private static final int RANDOM_SEED = 2016; + + /** + * Pseudo-random source for randomized behaviours. + */ + private static final Random RANDOM_SOURCE = new Random(GameState.RANDOM_SEED); + /** * Builds and returns the player priority order permutations. * @@ -30,6 +46,16 @@ public final class GameState { return Lists.permutations(Arrays.asList(PlayerID.values())); } + /** + * Returns a randomly chosen bonus Block with an equal probability distribution. + * + * @return a random bonus block + */ + private static Block randomBonus() { + int randomIndex = RANDOM_SOURCE.nextInt(GameState.RANDOM_BONUSES.length); + return GameState.RANDOM_BONUSES[randomIndex]; + } + /** * Filters the given list of players and returns the lively ones. * -- cgit v1.2.3 From 388ea0b54eb6e0d1ec1efe7146f92e3720df86eb Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 16 Mar 2016 17:36:02 +0100 Subject: Implement global game state evolution method --- src/ch/epfl/xblast/server/GameState.java | 112 ++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 5386311..522a367 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -295,7 +295,36 @@ public final class GameState { * @return the next game state */ public GameState next(Map> speedChangeEvents, Set bombDropEvents) { - return null; // TODO + // 1. blasts evolution + List> blasts1 = GameState.nextBlasts(this.blasts, this.board, this.explosions); + + // 2. board evolution + Set blastedCells1 = GameState.blastedCells(blasts1); + Map consumedBonuses = this.consumedBonuses(); + Board board1 = GameState.nextBoard(this.board, consumedBonuses.keySet(), blastedCells1); + + // 3. existing explosions evolution + List>> explosions1 = GameState.nextExplosions(this.explosions); + + // 4.1. existing bombs evolution + Set blastedCells0 = this.blastedCells(); + List explodingBombs = this.filterExplodingBombs(this.bombs, blastedCells0); + List bombs = this.bombs.stream().filter(b -> !explodingBombs.contains(b)).collect(Collectors.toList()); + + // 4.2. subsequent explosions addition + explodingBombs.forEach(b -> explosions1.addAll(b.explosion())); + + // 4.3. newly dropped bombs addition + Set topPriorityBombDropEvents = discardConflictingBombDropEvents(bombDropEvents); + bombs.addAll(GameState.newlyDroppedBombs(this.players, topPriorityBombDropEvents, this.bombs)); + + // 5. players evolution + Map playerBonuses = mapPlayersToBonuses(consumedBonuses); + Set bombedCells1 = GameState.bombedCells(bombs).keySet(); + List players1 = GameState.nextPlayers( + this.players, playerBonuses, bombedCells1, board1, blastedCells1, speedChangeEvents); + + return new GameState(this.ticks, board1, players1, bombs, explosions1, blasts1); } /** @@ -324,4 +353,85 @@ public final class GameState { .get(); } + /** + * Returns a mapping of players from their location. + * + * @param players a list of players to map + * @return the location->player mapping + */ + private Map> mapPlayersCells(List players) { + return players.stream() + .collect( + Collectors.groupingBy(p -> p.position().containingCell(), + Collectors.mapping(Player::id, Collectors.toList()))); + } + + /** + * Returns a mapping of top-priority player from their location. + * Conflicts are resolved using the current turn's players priority order. + * + * @param players a list of players to map + * @return the location->top-priority player mapping + */ + private Map mapTopPriorityPlayerCells(List players) { + return this.mapPlayersCells(players).entrySet().stream() + .collect(Collectors.toMap( + Map.Entry::getKey, + e -> resolveConflict(e.getValue()))); + } + + /** + * Returns a mapping of location of consumed bonuses and their top-priority claimant. + * + * @return the bonus-location->player mapping + */ + private Map consumedBonuses() { + return this.mapTopPriorityPlayerCells( + this.alivePlayers().stream() + .filter(p -> p.position().isCentral()) + .filter(p -> this.board.blockAt(p.position().containingCell()).isBonus()) + .collect(Collectors.toList())); + } + + /** + * Returns a mapping of players and their newly acquired bonus. + * + * @param consumedBonuses a mapping of bonus location and their claimants + * @return the player->bonus mapping + */ + private Map mapPlayersToBonuses(Map consumedBonuses) { + return consumedBonuses.entrySet().stream() + .collect(Collectors.toMap( + Map.Entry::getValue, + e -> this.board.blockAt(e.getKey()).associatedBonus())); + } + + /** + * Returns a conflict-free set a player bomb dropping events. + * + * @param bombDropEvents the bomb drop events to filter + * @return the conflict-free set of bomb drop events + */ + private Set discardConflictingBombDropEvents(Set bombDropEvents) { + return EnumSet.copyOf( + this.mapTopPriorityPlayerCells( + this.alivePlayers().stream() + .filter(bombDropEvents::contains) + .collect(Collectors.toList()) + ).values()); + } + + /** + * Returns a list of exploding bombs (reached by a blast or consumed fuse). + * + * @param bombs the list of bombs + * @param blastedCells the set of blasted cells + * @return the list of exploding bombs + */ + private List filterExplodingBombs(List bombs, Set blastedCells) { + return bombs.stream() + .filter(b -> blastedCells.contains(b.position()) || b.fuseLength() == 0) + .collect(Collectors.toList()); + } + } -- cgit v1.2.3 From 6b144c1c438a5d184d5ccb7b6df3acf2f7161d74 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Thu, 24 Mar 2016 09:21:28 +0100 Subject: Implement next*() for the fifth week --- src/ch/epfl/xblast/server/Board.java | 8 ++++++ src/ch/epfl/xblast/server/GameState.java | 49 +++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java index 18ee532..f2a3c89 100644 --- a/src/ch/epfl/xblast/server/Board.java +++ b/src/ch/epfl/xblast/server/Board.java @@ -142,4 +142,12 @@ public final class Board { return this.blocksAt(c).head(); } + /** + * Return the blocks of the Board. + * + * @return a list of the Sequences of blocks of the board. + */ + public List> getBlocks() { + return blocks; + } } diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 522a367..d34c2a4 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -119,7 +119,29 @@ public final class GameState { * @return the next board */ private static Board nextBoard(Board board0, Set consumedBonuses, Set blastedCells1) { - return null; // TODO + List> blocks0 = board0.getBlocks(); + List> blocks1 = new ArrayList<>(); + + int i = 0; + for (Sq blockSq : blocks0) { + int cellId = blocks0.get(i).hashCode(); + Block block = blockSq.head(); + if (((HashSet) consumedBonuses).contains(cellId) && block.isBonus()) { + blocks1.add(Sq.constant(Block.FREE)); + } else if (((HashSet) blastedCells1).contains(cellId) && (block == Block.DESTRUCTIBLE_WALL || block.isBonus())) { + if (block == Block.DESTRUCTIBLE_WALL) { + Block bonus = randomBonus(); + blocks1.add(Sq.repeat(Ticks.WALL_CRUMBLING_TICKS, Block.CRUMBLING_WALL).concat(Sq.constant(bonus))); + } else { + blocks1.add(Sq.repeat(Ticks.BONUS_DISAPPEARING_TICKS, block).concat(Sq.constant(Block.FREE))); + } + } else { + blocks1.add(blockSq.tail()); + } + i++; + } + + return new Board(blocks1); } /** @@ -140,7 +162,8 @@ public final class GameState { Board board1, Set blastedCells1, Map> speedChangeEvents) { - return null; // TODO + //ToDo + return players0; } /** @@ -150,7 +173,11 @@ public final class GameState { * @return the next explosion state */ private static List>> nextExplosions(List>> explosions0) { - return null; // TODO + List>> explosions1 = new ArrayList<>(); + for (Sq> explosion : explosions0) { + explosions1.add(explosion.tail()); + } + return explosions1; } /** @@ -165,7 +192,21 @@ public final class GameState { List players0, Set bombDropEvents, List bombs0) { - return null; // TODO + List bombs1 = new ArrayList<>(); + Set containingBombCells = new HashSet<>(); + + for (Bomb bomb : bombs0) { + containingBombCells.add(bomb.position()); + } + + for (Player player : players0) { + if (bombDropEvents.contains(player.id()) && player.isAlive() && !containingBombCells.contains(player.position().containingCell())) { + Bomb newBomb = new Bomb(player.id(), player.position().containingCell(), Ticks.BOMB_FUSE_TICKS, player.bombRange()); + containingBombCells.add(newBomb.position()); + bombs1.add(newBomb); + } + } + return bombs1; } private final int ticks; -- cgit v1.2.3 From 3c9bb6d8366673a1d88e36bcb575be5a06448f73 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Thu, 24 Mar 2016 10:39:20 +0100 Subject: Redefine equals with rowMajorIndex in Cell and SubCell. Check explosion's tail in nextExplosion (GameState). --- src/ch/epfl/xblast/Cell.java | 2 +- src/ch/epfl/xblast/SubCell.java | 2 +- src/ch/epfl/xblast/server/GameState.java | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java index a8a5a08..06dfdbb 100644 --- a/src/ch/epfl/xblast/Cell.java +++ b/src/ch/epfl/xblast/Cell.java @@ -171,7 +171,7 @@ public final class Cell { if (that == null) return false; if (that == this) return true; if (!(that instanceof Cell)) return false; - return (((Cell) that).x == this.x && ((Cell) that).y == this.y); + return (((Cell) that).rowMajorIndex() == this.rowMajorIndex()); } /** diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java index 8a25997..27d5bcc 100644 --- a/src/ch/epfl/xblast/SubCell.java +++ b/src/ch/epfl/xblast/SubCell.java @@ -133,7 +133,7 @@ public final class SubCell { if (that == null) return false; if (that == this) return true; if (!(that instanceof SubCell)) return false; - return (((SubCell) that).x == this.x && ((SubCell) that).y == this.y); + return (((SubCell) that).rowMajorIndex() == this.rowMajorIndex()); } /** diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index d34c2a4..1abe17a 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -175,7 +175,9 @@ public final class GameState { private static List>> nextExplosions(List>> explosions0) { List>> explosions1 = new ArrayList<>(); for (Sq> explosion : explosions0) { - explosions1.add(explosion.tail()); + if (!explosion.tail().isEmpty()) { + explosions1.add(explosion.tail()); + } } return explosions1; } -- cgit v1.2.3