From b0356d4ff6694ccc7f8fae1766b73b0e99c78ed9 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Tue, 8 Mar 2016 23:16:12 +0100 Subject: Import NameCheck04 and GaneStatePrinter (given files for the week 4) --- .../epfl/xblast/server/debug/GameStatePrinter.java | 57 +++++++++++++++ test/ch/epfl/xblast/namecheck/NameCheck04.java | 81 ++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/ch/epfl/xblast/server/debug/GameStatePrinter.java create mode 100644 test/ch/epfl/xblast/namecheck/NameCheck04.java diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java new file mode 100644 index 0000000..240446b --- /dev/null +++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java @@ -0,0 +1,57 @@ +package ch.epfl.xblast.server.debug; + +import java.util.List; + +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.server.Block; +import ch.epfl.xblast.server.Board; +import ch.epfl.xblast.server.GameState; +import ch.epfl.xblast.server.Player; + +public final class GameStatePrinter { + private GameStatePrinter() {} + + public static void printGameState(GameState s) { + List ps = s.alivePlayers(); + Board board = s.board(); + + for (int y = 0; y < Cell.ROWS; ++y) { + xLoop: for (int x = 0; x < Cell.COLUMNS; ++x) { + Cell c = new Cell(x, y); + for (Player p: ps) { + if (p.position().containingCell().equals(c)) { + System.out.print(stringForPlayer(p)); + continue xLoop; + } + } + Block b = board.blockAt(c); + System.out.print(stringForBlock(b)); + } + System.out.println(); + } + } + + private static String stringForPlayer(Player p) { + StringBuilder b = new StringBuilder(); + b.append(p.id().ordinal() + 1); + switch (p.direction()) { + case N: b.append('^'); break; + case E: b.append('>'); break; + case S: b.append('v'); break; + case W: b.append('<'); break; + } + return b.toString(); + } + + private static String stringForBlock(Block b) { + switch (b) { + case FREE: return " "; + case INDESTRUCTIBLE_WALL: return "##"; + case DESTRUCTIBLE_WALL: return "??"; + case CRUMBLING_WALL: return "¿¿"; + case BONUS_BOMB: return "+b"; + case BONUS_RANGE: return "+r"; + default: throw new Error(); + } + } +} diff --git a/test/ch/epfl/xblast/namecheck/NameCheck04.java b/test/ch/epfl/xblast/namecheck/NameCheck04.java new file mode 100644 index 0000000..b6adc8c --- /dev/null +++ b/test/ch/epfl/xblast/namecheck/NameCheck04.java @@ -0,0 +1,81 @@ +package ch.epfl.xblast.namecheck; + +import java.util.List; +import java.util.Optional; + +import ch.epfl.cs108.Sq; +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.Lists; +import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.Time; +import ch.epfl.xblast.server.Block; +import ch.epfl.xblast.server.Board; +import ch.epfl.xblast.server.Bomb; +import ch.epfl.xblast.server.Bonus; +import ch.epfl.xblast.server.GameState; +import ch.epfl.xblast.server.Player; +import ch.epfl.xblast.server.Ticks; + +/** + * Classe abstraite utilisant tous les éléments de l'étape 4, 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é! + */ + +abstract class NameCheck04 { + void checkLists() { + List l1 = null; + List> p1 = Lists.permutations(l1); + List>> p2 = Lists.permutations(p1); + System.out.println(p2); + } + + void checkBonus(boolean x) { + Bonus b = x ? Bonus.INC_BOMB : Bonus.INC_RANGE; + Player p = null; + p = b.applyTo(p); + } + + void checkBlock(Block b) { + b = b.isBonus() ? Block.BONUS_BOMB : Block.BONUS_RANGE; + Bonus s = b.associatedBonus(); + System.out.println(s); + } + + void checkTime() { + Optional o; + o = Optional.of(Time.S_PER_MIN); + o = Optional.of(Time.MS_PER_S); + o = Optional.of(Time.US_PER_S); + o = Optional.of(Time.NS_PER_S); + System.out.println(o); + } + + void checkTicks() { + Optional o; + o = Optional.of(Ticks.TICKS_PER_SECOND); + o = Optional.of(Ticks.TICK_NANOSECOND_DURATION); + o = Optional.of(Ticks.TOTAL_TICKS); + System.out.println(o); + } + + void checkGameState() { + int ts = 0; + Board b = null; + List ps = null; + List bs = null; + List>> es = null; + List> xs = null; + GameState s = new GameState(ts, b, ps, bs, es, xs); + s = new GameState(b, ps); + ts = s.ticks(); + if (s.isGameOver()) { + Optional t = Optional.of(s.remainingTime()); + System.out.println(t.get()); + } + Optional w = s.winner(); + b = s.board(); + ps = s.isGameOver() ? s.alivePlayers() : s.players(); + System.out.println(w); + } +} -- cgit v1.2.3 From b73b2050d345e0ee47fd38952047bdf9d36365d3 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Wed, 9 Mar 2016 17:27:13 +0100 Subject: Structure of the fourth week assignment --- src/ch/epfl/xblast/Lists.java | 9 ++++++ src/ch/epfl/xblast/Time.java | 8 ++++++ src/ch/epfl/xblast/server/Block.java | 45 ++++++++++++++++++++++++++++- src/ch/epfl/xblast/server/Bonus.java | 18 ++++++++++++ src/ch/epfl/xblast/server/GameState.java | 49 ++++++++++++++++++++++++++++++++ src/ch/epfl/xblast/server/Ticks.java | 7 +++++ 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/ch/epfl/xblast/Time.java create mode 100644 src/ch/epfl/xblast/server/Bonus.java create mode 100644 src/ch/epfl/xblast/server/GameState.java diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index 51e76b0..f541b37 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -44,4 +44,13 @@ public final class Lists { .collect(Collectors.toList()); } + /** + * Returns all the permutations of the elements of the given list + * + * @param l given list + * @return a list of all the permutations of the list + */ + public static List> permutations(List l) { + return null; + } } diff --git a/src/ch/epfl/xblast/Time.java b/src/ch/epfl/xblast/Time.java new file mode 100644 index 0000000..fa18f86 --- /dev/null +++ b/src/ch/epfl/xblast/Time.java @@ -0,0 +1,8 @@ +package ch.epfl.xblast; + +public interface Time { + int S_PER_MIN = 60; + int MS_PER_S = 1000; + int US_PER_S = 1000*MS_PER_S; + int NS_PER_S = 1000*US_PER_S; +} diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index 409f68e..4903e2b 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -26,7 +26,36 @@ public enum Block { /** * Crumbling Wall. */ - CRUMBLING_WALL; + CRUMBLING_WALL, + + /** + * + */ + BONUS_BOMB(Bonus.INC_BOMB), + + /** + * + */ + BONUS_RANGE(Bonus.INC_RANGE); + + /** + * Corresponding bonus, or null + */ + private Bonus maybeAssociatedBonus; + + /** + * Main builder, used by the bonus blocks + */ + private Block(Bonus maybeAssociatedBonus) { + this.maybeAssociatedBonus = maybeAssociatedBonus; + } + + /** + * Default builder, used by the ither blocks + */ + private Block() { + this.maybeAssociatedBonus = null; + } /** * Returns T(this block is free). @@ -55,4 +84,18 @@ public enum Block { return this == INDESTRUCTIBLE_WALL || this == DESTRUCTIBLE_WALL || this == CRUMBLING_WALL; } + /** + * + */ + public boolean isBonus() { + return false; + } + + /** + * + */ + public Bonus associatedBonus() { + return null; + } + } diff --git a/src/ch/epfl/xblast/server/Bonus.java b/src/ch/epfl/xblast/server/Bonus.java new file mode 100644 index 0000000..43be24f --- /dev/null +++ b/src/ch/epfl/xblast/server/Bonus.java @@ -0,0 +1,18 @@ +package ch.epfl.xblast.server; + +/** + * @author TimothéE FLOURE (257420) + */ +public enum Bonus { + INC_BOMB { + @Override + public Player applyTo(Player player) { return null; } + }, + + INC_RANGE { + @Override + public Player applyTo(Player player) { return null; } + }; + + abstract public Player applyTo(Player player); +} diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java new file mode 100644 index 0000000..625767a --- /dev/null +++ b/src/ch/epfl/xblast/server/GameState.java @@ -0,0 +1,49 @@ +package ch.epfl.xblast.server; + +import java.util.List; +import java.util.Optional; +import ch.epfl.xblast.*; +import ch.epfl.cs108.Sq; + +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; + + public GameState(int ticks, Board board, List players, List bombs, List>> explosions, List> blasts) { + } + + public GameState(Board board, List players) { + } + + public int ticks() { + return 0; + } + + public boolean isGameOver() { + return false; + } + + public double remainingTime() { + return 0; + } + + public Optional winner() { + return null; + } + + public Board board() { + return null; + } + + public List players() { + return null; + } + + public List alivePlayers() { + return null; + } +} diff --git a/src/ch/epfl/xblast/server/Ticks.java b/src/ch/epfl/xblast/server/Ticks.java index aa08a23..fe3f40c 100644 --- a/src/ch/epfl/xblast/server/Ticks.java +++ b/src/ch/epfl/xblast/server/Ticks.java @@ -1,5 +1,7 @@ package ch.epfl.xblast.server; +import ch.epfl.xblast.Time; + /** * The Ticks interface defines durations in ticks of time-sensitive aspects of the game. * @@ -38,4 +40,9 @@ public interface Ticks { */ int BONUS_DISAPPEARING_TICKS = EXPLOSION_TICKS; + int TICKS_PER_SECOND = 20; + + int TICK_NANOSECOND_DURATION = TICKS_PER_SECOND / Time.NS_PER_S; + + int TOTAL_TICKS = 2 * Time.S_PER_MIN * TICKS_PER_SECOND; } -- cgit v1.2.3 From a7fc716ae4123d21254d041797fe114fc44a88e3 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Sat, 12 Mar 2016 14:49:54 +0100 Subject: Fill the Time & Ticks interfaces + the Block & Bonus enums as requested in the 4th part --- src/ch/epfl/xblast/Time.java | 15 +++++++++++++++ src/ch/epfl/xblast/server/Block.java | 20 +++++++++++++++----- src/ch/epfl/xblast/server/Bonus.java | 12 ++++++++++++ src/ch/epfl/xblast/server/Ticks.java | 16 +++++++++++++++- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/ch/epfl/xblast/Time.java b/src/ch/epfl/xblast/Time.java index fa18f86..d39c3e3 100644 --- a/src/ch/epfl/xblast/Time.java +++ b/src/ch/epfl/xblast/Time.java @@ -1,8 +1,23 @@ package ch.epfl.xblast; public interface Time { + /** + * Number of seconds per minute. + */ int S_PER_MIN = 60; + + /** + * Number of milliseconds per second. + */ int MS_PER_S = 1000; + + /** + * Number of microseconds per second. + */ int US_PER_S = 1000*MS_PER_S; + + /** + * Number of nanoseconds per second. + */ int NS_PER_S = 1000*US_PER_S; } diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index 4903e2b..fff3819 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -1,5 +1,7 @@ package ch.epfl.xblast.server; +import java.util.NoSuchElementException; + /** * A Block. * @@ -29,12 +31,12 @@ public enum Block { CRUMBLING_WALL, /** - * + * Bonus increasing the maximun number of bombs. */ BONUS_BOMB(Bonus.INC_BOMB), /** - * + * Nomus increasing the range of the bombs. */ BONUS_RANGE(Bonus.INC_RANGE); @@ -51,7 +53,7 @@ public enum Block { } /** - * Default builder, used by the ither blocks + * Default builder, used by the non-bonus blocks */ private Block() { this.maybeAssociatedBonus = null; @@ -85,17 +87,25 @@ public enum Block { } /** + * Returns T(this block is a bonus) * + * @return T(this block is a bonus) */ public boolean isBonus() { - return false; + return this == BONUS_BOMB || this == BONUS_RANGE; } /** + * Return the bonus associated with the block. * + * @throws NoSuchElementException if there is no such bonus. + * @return the bonus associated with the block. */ public Bonus associatedBonus() { - return null; + if (this.maybeAssociatedBonus == null) { + throw new NoSuchElementException(); + } + return this.maybeAssociatedBonus; } } diff --git a/src/ch/epfl/xblast/server/Bonus.java b/src/ch/epfl/xblast/server/Bonus.java index 43be24f..81ae7dd 100644 --- a/src/ch/epfl/xblast/server/Bonus.java +++ b/src/ch/epfl/xblast/server/Bonus.java @@ -4,15 +4,27 @@ package ch.epfl.xblast.server; * @author TimothéE FLOURE (257420) */ public enum Bonus { + + /** + * Increase the maximum number of bombs used simultaneously. + */ INC_BOMB { @Override public Player applyTo(Player player) { return null; } }, + /** + * Increase the range of the bombs. + */ INC_RANGE { @Override public Player applyTo(Player player) { return null; } }; + /** + * Apply the bonus to the given player. + * + * @param player receiving the bonus + */ abstract public Player applyTo(Player player); } diff --git a/src/ch/epfl/xblast/server/Ticks.java b/src/ch/epfl/xblast/server/Ticks.java index fe3f40c..d27d20c 100644 --- a/src/ch/epfl/xblast/server/Ticks.java +++ b/src/ch/epfl/xblast/server/Ticks.java @@ -40,9 +40,23 @@ public interface Ticks { */ int BONUS_DISAPPEARING_TICKS = EXPLOSION_TICKS; + /** + * Number of ticks per second. + */ int TICKS_PER_SECOND = 20; + /** + * Dureation of a tick (in nanoseconds). + */ int TICK_NANOSECOND_DURATION = TICKS_PER_SECOND / Time.NS_PER_S; - int TOTAL_TICKS = 2 * Time.S_PER_MIN * TICKS_PER_SECOND; + /** + * Duration of a game (in seconds). + */ + int GAME_DURATION = 2; + + /** + * Total number of ticks during a game. + */ + int TOTAL_TICKS = GAME_DURATION * TICKS_PER_SECOND; } -- cgit v1.2.3 From 7b228fed7b480cc5825bb168567a556269f785cb Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 14 Mar 2016 13:32:49 +0100 Subject: Fill the GameState class as requested in week 04 --- src/ch/epfl/xblast/server/GameState.java | 108 +++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 7 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 625767a..685bc54 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -2,10 +2,18 @@ package ch.epfl.xblast.server; import java.util.List; import java.util.Optional; +import java.util.Objects; +import java.util.ArrayList; +import java.util.Optional; import ch.epfl.xblast.*; import ch.epfl.cs108.Sq; +/** + * @author Timothée FLOURE (257420) + */ + public final class GameState { + private final int ticks; private final Board board; private final List players; @@ -13,37 +21,123 @@ public final class GameState { private final List>> explosions; private final List> blasts; + /** + * Instanciates a new GameState. + * + * @param ticks thie tick corresponding to the state + * @param board the board + * @param players list of the players + * @param bombs list of the bombs + * @param explosions list of the explosions + * @param blasts + * @throws IllegalArguementException if ticks is negative or players does not contains 4 players. + * @throws NullPointerException if any element except ticks is null. + */ public GameState(int ticks, Board board, List players, List bombs, List>> explosions, List> blasts) { + this.ticks = ArgumentChecker.requireNonNegative(ticks); + this.board = Objects.requireNonNull(board); + if (players.size() == 4) { + this.players = players; + } else { + throw new IllegalArgumentException(); + } + this.bombs = Objects.requireNonNull(bombs); + this.explosions = Objects.requireNonNull(explosions); + this.blasts = Objects.requireNonNull(blasts); } + /** + * Instanciates a new (clean) GameState. + * + * @param board the board + * @param players list of the players + */ public GameState(Board board, List players) { + this(0, + board, + players, + new ArrayList(), + new ArrayList>>(), + new ArrayList>() + ); + } + /** + * @return the tick related to the state. + */ public int ticks() { - return 0; + return ticks; } + /** + * Returns T(the game is over) + * + * @return true if all the players are dead or if the game reachead the time limit + */ public boolean isGameOver() { - return false; + int alivePlayerCount = 0; + for (Player player : players) { + if (player.isAlive()) {alivePlayerCount += 1;} + } + if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) { + return true; + } else { + return false; + } } + /** + * Return the remaining time. + * + * @return the ramaining game time (in seconds) + */ public double remainingTime() { - return 0; + return (double) (Ticks.TOTAL_TICKS - this.ticks) / Ticks.TICKS_PER_SECOND; } + /** + * Returns the winner of the game. + * + * @return the ID of the player who winned the game. + */ public Optional winner() { - return null; + if (players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { + PlayerID winnerId = players.get(0).id(); + return Optional.of(winnerId); + } else { + return Optional.empty(); + } } + /** + * @return the game board + */ public Board board() { - return null; + return this.board; } + /** + * @return the players + */ public List players() { - return null; + return this.players; } + /** + * Returns the alive players. + * + * @return a list of the alive players + */ public List alivePlayers() { - return null; + List alivePlayers = new ArrayList<>(); + + for (Player player : players) { + if (player.isAlive()) { + alivePlayers.add(player); + } + } + + return alivePlayers; } } -- cgit v1.2.3 From 06296e66cd53bc68198c3bfeace17f85707cc2f4 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 14 Mar 2016 14:08:15 +0100 Subject: Add the nextBlasts method to the GameState class --- src/ch/epfl/xblast/server/GameState.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 685bc54..3bd1961 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -21,11 +21,32 @@ public final class GameState { private final List>> explosions; private final List> blasts; + /** + * Compute the next state of a blast. + * + * @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> blasts0, Board board0, List>> explosions0) { + List> blasts1 = new ArrayList<>(); + for (Sq blastSeq : blasts0) { + if (!blastSeq.tail().isEmpty() && board0.blockAt(blastSeq.head()).isFree()) { + blasts1.add(blastSeq.tail()); + } + } + for (Sq> explosion0 : explosions0) { + blasts1.add(explosion0.head()); + } + return blasts1; + } + /** * Instanciates a new GameState. * * @param ticks thie tick corresponding to the state - * @param board the board + * @param board the game's board * @param players list of the players * @param bombs list of the bombs * @param explosions list of the explosions -- cgit v1.2.3 From 741cb9da501b8c6e9e5fa006e5c2c9df0b4616f8 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 11:51:31 +0100 Subject: Add List permutation function and its test --- src/ch/epfl/xblast/Lists.java | 35 ++++++++++++++++++++++++++++++----- test/ch/epfl/xblast/ListsTest.java | 15 +++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index f541b37..4ea45c2 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -1,8 +1,6 @@ package ch.epfl.xblast; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -44,13 +42,40 @@ public final class Lists { .collect(Collectors.toList()); } + /** + * Returns a copy of the given list with element e inserted at index i. + * + * @param l the list + * @param i the insertion index + * @param e the element to insert + * @param the type of the list's elements + * @return a copy of the list with the element inserted + */ + public static List inserted(List l, int i, T e) { + List r = new LinkedList<>(l); + r.add(i, e); + return r; + } + /** * Returns all the permutations of the elements of the given list * - * @param l given list + * @param l given list + * @param the type of the list's elements * @return a list of all the permutations of the list + * @throws IllegalArgumentException if the given list is null */ public static List> permutations(List l) { - return null; + if (l == null) throw new IllegalArgumentException(); + if (l.size() <= 1) return Collections.singletonList(l); + if (l.size() == 2) return Arrays.asList(l, Lists.reversed(l)); + + List> p = new LinkedList<>(); + for (List sp : Lists.permutations(l.subList(1, l.size()))) + for (int i = 0; i <= sp.size(); ++i) + p.add(Lists.inserted(sp, i, l.get(0))); + + return p; } + } diff --git a/test/ch/epfl/xblast/ListsTest.java b/test/ch/epfl/xblast/ListsTest.java index b898399..b18b05e 100644 --- a/test/ch/epfl/xblast/ListsTest.java +++ b/test/ch/epfl/xblast/ListsTest.java @@ -4,6 +4,7 @@ import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -48,4 +49,18 @@ public class ListsTest { assertEquals(mirrored.get(i), mirrored.get(mirrored.size() - 1 - i)); } + @Test + public void isListPermuted() { + List sampleList = Arrays.asList(1, 2, 3); + List> expected = new LinkedList<>(); + expected.add(Arrays.asList(1, 2, 3)); + expected.add(Arrays.asList(2, 1, 3)); + expected.add(Arrays.asList(2, 3, 1)); + expected.add(Arrays.asList(1, 3, 2)); + expected.add(Arrays.asList(3, 1, 2)); + expected.add(Arrays.asList(3, 2, 1)); + + assertEquals(expected, Lists.permutations(sampleList)); + } + } -- cgit v1.2.3 From 59d35ae34c4513f54decf056e944190d7e9926f0 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 13:44:43 +0100 Subject: Reformat provided classes --- .../epfl/xblast/server/debug/GameStatePrinter.java | 56 +++++++++++++++------- test/ch/epfl/xblast/namecheck/NameCheck04.java | 19 ++++---- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java index 240446b..ea8b360 100644 --- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java +++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java @@ -1,24 +1,32 @@ package ch.epfl.xblast.server.debug; -import java.util.List; - import ch.epfl.xblast.Cell; import ch.epfl.xblast.server.Block; import ch.epfl.xblast.server.Board; import ch.epfl.xblast.server.GameState; import ch.epfl.xblast.server.Player; +import java.util.List; + +/** + * Game state printer utility class that outputs the board to the terminal. + * + * @author EPFL + */ public final class GameStatePrinter { - private GameStatePrinter() {} + + private GameStatePrinter() { + } public static void printGameState(GameState s) { List ps = s.alivePlayers(); Board board = s.board(); for (int y = 0; y < Cell.ROWS; ++y) { - xLoop: for (int x = 0; x < Cell.COLUMNS; ++x) { + xLoop: + for (int x = 0; x < Cell.COLUMNS; ++x) { Cell c = new Cell(x, y); - for (Player p: ps) { + for (Player p : ps) { if (p.position().containingCell().equals(c)) { System.out.print(stringForPlayer(p)); continue xLoop; @@ -35,23 +43,39 @@ public final class GameStatePrinter { StringBuilder b = new StringBuilder(); b.append(p.id().ordinal() + 1); switch (p.direction()) { - case N: b.append('^'); break; - case E: b.append('>'); break; - case S: b.append('v'); break; - case W: b.append('<'); break; + case N: + b.append('^'); + break; + case E: + b.append('>'); + break; + case S: + b.append('v'); + break; + case W: + b.append('<'); + break; } return b.toString(); } private static String stringForBlock(Block b) { switch (b) { - case FREE: return " "; - case INDESTRUCTIBLE_WALL: return "##"; - case DESTRUCTIBLE_WALL: return "??"; - case CRUMBLING_WALL: return "¿¿"; - case BONUS_BOMB: return "+b"; - case BONUS_RANGE: return "+r"; - default: throw new Error(); + case FREE: + return " "; + case INDESTRUCTIBLE_WALL: + return "##"; + case DESTRUCTIBLE_WALL: + return "??"; + case CRUMBLING_WALL: + return "¿¿"; + case BONUS_BOMB: + return "+b"; + case BONUS_RANGE: + return "+r"; + default: + throw new Error(); } } + } diff --git a/test/ch/epfl/xblast/namecheck/NameCheck04.java b/test/ch/epfl/xblast/namecheck/NameCheck04.java index b6adc8c..dc6cfb2 100644 --- a/test/ch/epfl/xblast/namecheck/NameCheck04.java +++ b/test/ch/epfl/xblast/namecheck/NameCheck04.java @@ -1,28 +1,24 @@ package ch.epfl.xblast.namecheck; -import java.util.List; -import java.util.Optional; - import ch.epfl.cs108.Sq; import ch.epfl.xblast.Cell; import ch.epfl.xblast.Lists; import ch.epfl.xblast.PlayerID; import ch.epfl.xblast.Time; -import ch.epfl.xblast.server.Block; -import ch.epfl.xblast.server.Board; -import ch.epfl.xblast.server.Bomb; -import ch.epfl.xblast.server.Bonus; -import ch.epfl.xblast.server.GameState; -import ch.epfl.xblast.server.Player; -import ch.epfl.xblast.server.Ticks; +import ch.epfl.xblast.server.*; + +import java.util.List; +import java.util.Optional; /** * Classe abstraite utilisant tous les éléments de l'étape 4, 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 NameCheck04 { + void checkLists() { List l1 = null; List> p1 = Lists.permutations(l1); @@ -78,4 +74,5 @@ abstract class NameCheck04 { ps = s.isGameOver() ? s.alivePlayers() : s.players(); System.out.println(w); } + } -- cgit v1.2.3 From 3b8c85a5f483c856b34c7026f48abd4f355b67d7 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 13:57:11 +0100 Subject: Code clean up and reformatting --- src/ch/epfl/xblast/ArgumentChecker.java | 5 +-- src/ch/epfl/xblast/Time.java | 12 +++++- src/ch/epfl/xblast/server/Block.java | 14 +++--- src/ch/epfl/xblast/server/Bonus.java | 24 ++++++++--- src/ch/epfl/xblast/server/GameState.java | 74 ++++++++++++++++++-------------- src/ch/epfl/xblast/server/Ticks.java | 3 +- 6 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java index 311807e..589c197 100644 --- a/src/ch/epfl/xblast/ArgumentChecker.java +++ b/src/ch/epfl/xblast/ArgumentChecker.java @@ -16,11 +16,10 @@ public final class ArgumentChecker { * @throws IllegalArgumentException if the value is inferior to 0 */ public static int requireNonNegative(int value) { - if (value >= 0) { + if (value >= 0) return value; - } else { + else throw new IllegalArgumentException(); - } } } diff --git a/src/ch/epfl/xblast/Time.java b/src/ch/epfl/xblast/Time.java index d39c3e3..7c84257 100644 --- a/src/ch/epfl/xblast/Time.java +++ b/src/ch/epfl/xblast/Time.java @@ -1,6 +1,13 @@ package ch.epfl.xblast; +/** + * Time unit constants. + * + * @author Pacien TRAN-GIRARD (261948) + * @author Timothée FLOURE (257420) + */ public interface Time { + /** * Number of seconds per minute. */ @@ -14,10 +21,11 @@ public interface Time { /** * Number of microseconds per second. */ - int US_PER_S = 1000*MS_PER_S; + int US_PER_S = 1000 * MS_PER_S; /** * Number of nanoseconds per second. */ - int NS_PER_S = 1000*US_PER_S; + int NS_PER_S = 1000 * US_PER_S; + } diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index fff3819..c90a469 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -31,12 +31,12 @@ public enum Block { CRUMBLING_WALL, /** - * Bonus increasing the maximun number of bombs. + * Bonus increasing the maximum number of bombs. */ BONUS_BOMB(Bonus.INC_BOMB), /** - * Nomus increasing the range of the bombs. + * Bonus increasing the range of the bombs. */ BONUS_RANGE(Bonus.INC_RANGE); @@ -87,7 +87,7 @@ public enum Block { } /** - * Returns T(this block is a bonus) + * Returns T(this block is a bonus). * * @return T(this block is a bonus) */ @@ -96,15 +96,13 @@ public enum Block { } /** - * Return the bonus associated with the block. + * Returns the bonus associated with the block. * - * @throws NoSuchElementException if there is no such bonus. * @return the bonus associated with the block. + * @throws NoSuchElementException if there is no such bonus. */ public Bonus associatedBonus() { - if (this.maybeAssociatedBonus == null) { - throw new NoSuchElementException(); - } + if (this.maybeAssociatedBonus == null) throw new NoSuchElementException(); return this.maybeAssociatedBonus; } diff --git a/src/ch/epfl/xblast/server/Bonus.java b/src/ch/epfl/xblast/server/Bonus.java index 81ae7dd..09a2248 100644 --- a/src/ch/epfl/xblast/server/Bonus.java +++ b/src/ch/epfl/xblast/server/Bonus.java @@ -1,24 +1,33 @@ package ch.epfl.xblast.server; /** - * @author TimothéE FLOURE (257420) + * Bonuses. + * + * @author Pacien TRAN-GIRARD (261948) + * @author Timothée FLOURE (257420) */ public enum Bonus { /** - * Increase the maximum number of bombs used simultaneously. + * Increases the maximum number of bombs used simultaneously. */ INC_BOMB { - @Override - public Player applyTo(Player player) { return null; } + @Override + public Player applyTo(Player player) { + // TODO + return null; + } }, /** - * Increase the range of the bombs. + * Increases the range of the bombs. */ INC_RANGE { - @Override - public Player applyTo(Player player) { return null; } + @Override + public Player applyTo(Player player) { + // TODO + return null; + } }; /** @@ -27,4 +36,5 @@ public enum Bonus { * @param player receiving the bonus */ abstract public Player applyTo(Player player); + } diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 3bd1961..e7e6ca7 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -1,17 +1,22 @@ package ch.epfl.xblast.server; +import ch.epfl.cs108.Sq; +import ch.epfl.xblast.ArgumentChecker; +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.PlayerID; + +import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.Objects; -import java.util.ArrayList; import java.util.Optional; -import ch.epfl.xblast.*; -import ch.epfl.cs108.Sq; +import java.util.stream.Collectors; /** + * GameState representing the current game state. + * + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ - public final class GameState { private final int ticks; @@ -24,8 +29,8 @@ public final class GameState { /** * Compute the next state of a blast. * - * @param blasts0 existing particles - * @param board0 the game's board + * @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. */ @@ -43,16 +48,16 @@ public final class GameState { } /** - * Instanciates a new GameState. + * Instantiates a new GameState. * - * @param ticks thie tick corresponding to the state - * @param board the game's board - * @param players list of the players - * @param bombs list of the bombs + * @param ticks the tick corresponding to the state + * @param board the game's board + * @param players list of the players + * @param bombs list of the bombs * @param explosions list of the explosions - * @param blasts - * @throws IllegalArguementException if ticks is negative or players does not contains 4 players. - * @throws NullPointerException if any element except ticks is null. + * @param blasts list of particle blasts + * @throws IllegalArgumentException if ticks is negative or players does not contains 4 players. + * @throws NullPointerException if any element except ticks is null. */ public GameState(int ticks, Board board, List players, List bombs, List>> explosions, List> blasts) { this.ticks = ArgumentChecker.requireNonNegative(ticks); @@ -68,19 +73,19 @@ public final class GameState { } /** - * Instanciates a new (clean) GameState. + * Instantiates a new (clean) GameState. * - * @param board the board + * @param board the board * @param players list of the players */ public GameState(Board board, List players) { this(0, - board, - players, - new ArrayList(), - new ArrayList>>(), - new ArrayList>() - ); + board, + players, + new ArrayList(), + new ArrayList>>(), + new ArrayList>() + ); } @@ -88,18 +93,20 @@ public final class GameState { * @return the tick related to the state. */ public int ticks() { - return ticks; + return this.ticks; } /** - * Returns T(the game is over) + * Returns T(the game is over). * - * @return true if all the players are dead or if the game reachead the time limit + * @return true if all the players are dead or if the game reached the time limit */ public boolean isGameOver() { int alivePlayerCount = 0; - for (Player player : players) { - if (player.isAlive()) {alivePlayerCount += 1;} + for (Player player : this.players) { + if (player.isAlive()) { + alivePlayerCount += 1; + } } if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) { return true; @@ -111,7 +118,7 @@ public final class GameState { /** * Return the remaining time. * - * @return the ramaining game time (in seconds) + * @return the remaining game time (in seconds) */ public double remainingTime() { return (double) (Ticks.TOTAL_TICKS - this.ticks) / Ticks.TICKS_PER_SECOND; @@ -120,11 +127,11 @@ public final class GameState { /** * Returns the winner of the game. * - * @return the ID of the player who winned the game. + * @return the ID of the player who won the game. */ public Optional winner() { - if (players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { - PlayerID winnerId = players.get(0).id(); + if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { + PlayerID winnerId = this.players.get(0).id(); return Optional.of(winnerId); } else { return Optional.empty(); @@ -153,7 +160,7 @@ public final class GameState { public List alivePlayers() { List alivePlayers = new ArrayList<>(); - for (Player player : players) { + for (Player player : this.players) { if (player.isAlive()) { alivePlayers.add(player); } @@ -161,4 +168,5 @@ public final class GameState { return alivePlayers; } + } diff --git a/src/ch/epfl/xblast/server/Ticks.java b/src/ch/epfl/xblast/server/Ticks.java index d27d20c..3517491 100644 --- a/src/ch/epfl/xblast/server/Ticks.java +++ b/src/ch/epfl/xblast/server/Ticks.java @@ -46,7 +46,7 @@ public interface Ticks { int TICKS_PER_SECOND = 20; /** - * Dureation of a tick (in nanoseconds). + * Duration of a tick (in nanoseconds). */ int TICK_NANOSECOND_DURATION = TICKS_PER_SECOND / Time.NS_PER_S; @@ -59,4 +59,5 @@ public interface Ticks { * Total number of ticks during a game. */ int TOTAL_TICKS = GAME_DURATION * TICKS_PER_SECOND; + } -- cgit v1.2.3 From 74e7fe38193d28d58f41d23ccd41634b988394ef Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 14:13:35 +0100 Subject: Simplify code --- src/ch/epfl/xblast/server/Block.java | 4 +- src/ch/epfl/xblast/server/GameState.java | 78 +++++++++++--------------------- 2 files changed, 28 insertions(+), 54 deletions(-) diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index c90a469..5a139ff 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -48,14 +48,14 @@ public enum Block { /** * Main builder, used by the bonus blocks */ - private Block(Bonus maybeAssociatedBonus) { + Block(Bonus maybeAssociatedBonus) { this.maybeAssociatedBonus = maybeAssociatedBonus; } /** * Default builder, used by the non-bonus blocks */ - private Block() { + Block() { this.maybeAssociatedBonus = null; } diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index e7e6ca7..7e93812 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * GameState representing the current game state. @@ -29,22 +30,20 @@ public final class GameState { /** * Compute the next state of a blast. * - * @param blasts0 existing particles - * @param board0 the game's board - * @param explosions0 active explosions + * @param blasts existing particles + * @param board the game's board + * @param explosions active explosions * @return the position of the explosion's particles for the next state. */ - private static List> nextBlasts(List> blasts0, Board board0, List>> explosions0) { - List> blasts1 = new ArrayList<>(); - for (Sq blastSeq : blasts0) { - if (!blastSeq.tail().isEmpty() && board0.blockAt(blastSeq.head()).isFree()) { - blasts1.add(blastSeq.tail()); - } - } - for (Sq> explosion0 : explosions0) { - blasts1.add(explosion0.head()); - } - return blasts1; + private static List> nextBlasts(List> blasts, Board board, List>> explosions) { + return Stream.concat( + blasts.stream() + .filter(blastSeq -> !blastSeq.tail().isEmpty()) + .filter(blastSeq -> board.blockAt(blastSeq.head()).isFree()) + .map(Sq::tail), + explosions.stream() + .map(Sq::head) + ).collect(Collectors.toList()); } /** @@ -62,11 +61,10 @@ public final class GameState { public GameState(int ticks, Board board, List players, List bombs, List>> explosions, List> blasts) { this.ticks = ArgumentChecker.requireNonNegative(ticks); this.board = Objects.requireNonNull(board); - if (players.size() == 4) { - this.players = players; - } else { - throw new IllegalArgumentException(); - } + + if (players.size() != 4) throw new IllegalArgumentException(); + this.players = players; + this.bombs = Objects.requireNonNull(bombs); this.explosions = Objects.requireNonNull(explosions); this.blasts = Objects.requireNonNull(blasts); @@ -79,14 +77,7 @@ public final class GameState { * @param players list of the players */ public GameState(Board board, List players) { - this(0, - board, - players, - new ArrayList(), - new ArrayList>>(), - new ArrayList>() - ); - + this(0, board, players, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); } /** @@ -102,17 +93,7 @@ public final class GameState { * @return true if all the players are dead or if the game reached the time limit */ public boolean isGameOver() { - int alivePlayerCount = 0; - for (Player player : this.players) { - if (player.isAlive()) { - alivePlayerCount += 1; - } - } - if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) { - return true; - } else { - return false; - } + return this.alivePlayers().isEmpty() || this.ticks >= Ticks.TOTAL_TICKS; } /** @@ -130,12 +111,10 @@ public final class GameState { * @return the ID of the player who won the game. */ public Optional winner() { - if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { - PlayerID winnerId = this.players.get(0).id(); - return Optional.of(winnerId); - } else { + if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) + return Optional.of(this.players.get(0).id()); + else return Optional.empty(); - } } /** @@ -158,15 +137,10 @@ public final class GameState { * @return a list of the alive players */ public List alivePlayers() { - List alivePlayers = new ArrayList<>(); - - for (Player player : this.players) { - if (player.isAlive()) { - alivePlayers.add(player); - } - } - - return alivePlayers; + return this.players + .stream() + .filter(Player::isAlive) + .collect(Collectors.toList()); } } -- cgit v1.2.3 From a04618577760d1ee306503fd14b86e5ebfc1a8ba Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 14:19:13 +0100 Subject: Fix cast ambiguity --- 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 7e93812..ef792b6 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -102,7 +102,7 @@ public final class GameState { * @return the remaining game time (in seconds) */ public double remainingTime() { - return (double) (Ticks.TOTAL_TICKS - this.ticks) / Ticks.TICKS_PER_SECOND; + return ((double) (Ticks.TOTAL_TICKS - this.ticks)) / Ticks.TICKS_PER_SECOND; } /** -- cgit v1.2.3 From bc0c2723e96b6b0769520eb33bc932fce688d62d Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Tue, 15 Mar 2016 14:34:33 +0100 Subject: Implement the applyTo method for INC_BOMB and INC_RANGE --- src/ch/epfl/xblast/server/Bonus.java | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ch/epfl/xblast/server/Bonus.java b/src/ch/epfl/xblast/server/Bonus.java index 09a2248..f22c9fe 100644 --- a/src/ch/epfl/xblast/server/Bonus.java +++ b/src/ch/epfl/xblast/server/Bonus.java @@ -1,40 +1,53 @@ package ch.epfl.xblast.server; /** - * Bonuses. - * - * @author Pacien TRAN-GIRARD (261948) - * @author Timothée FLOURE (257420) + * @author TimothéE FLOURE (257420) */ public enum Bonus { /** - * Increases the maximum number of bombs used simultaneously. + * Increase the maximum number of bombs used simultaneously. */ INC_BOMB { @Override public Player applyTo(Player player) { - // TODO - return null; + if (player.maxBombs() < BOMBS_LIMIT) { + return player.withMaxBombs(player.maxBombs() + 1); + } else { + return player.withMaxBombs(BOMBS_LIMIT); + } } }, /** - * Increases the range of the bombs. + * Increase the range of the bombs. */ INC_RANGE { @Override public Player applyTo(Player player) { - // TODO - return null; + if (player.bombRange() < RANGE_LIMIT) { + return player.withBombRange(player.bombRange() + 1); + } else { + return player.withBombRange(RANGE_LIMIT); + } } }; + /** + * Maximum number of bombs. + */ + private final int BOMBS_LIMIT = 9; + + /** + * Maximum range of a bomb. + */ + private final int RANGE_LIMIT = 9; + /** * Apply the bonus to the given player. * * @param player receiving the bonus + * @return a new player with the modified values */ abstract public Player applyTo(Player player); - } -- cgit v1.2.3 From 9ab2568b7519615165fa869c1852b0afbb4950e7 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 14:51:34 +0100 Subject: Restore doc fixes and reformatting --- src/ch/epfl/xblast/server/Bonus.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ch/epfl/xblast/server/Bonus.java b/src/ch/epfl/xblast/server/Bonus.java index f22c9fe..80ca8a9 100644 --- a/src/ch/epfl/xblast/server/Bonus.java +++ b/src/ch/epfl/xblast/server/Bonus.java @@ -1,35 +1,36 @@ package ch.epfl.xblast.server; /** - * @author TimothéE FLOURE (257420) + * Bonuses. + * + * @author Pacien TRAN-GIRARD (261948) + * @author Timothée FLOURE (257420) */ public enum Bonus { /** - * Increase the maximum number of bombs used simultaneously. + * Increases the maximum number of bombs used simultaneously. */ INC_BOMB { @Override public Player applyTo(Player player) { - if (player.maxBombs() < BOMBS_LIMIT) { + if (player.maxBombs() < BOMBS_LIMIT) return player.withMaxBombs(player.maxBombs() + 1); - } else { + else return player.withMaxBombs(BOMBS_LIMIT); - } } }, /** - * Increase the range of the bombs. + * Increases the range of the bombs. */ INC_RANGE { @Override public Player applyTo(Player player) { - if (player.bombRange() < RANGE_LIMIT) { + if (player.bombRange() < RANGE_LIMIT) return player.withBombRange(player.bombRange() + 1); - } else { + else return player.withBombRange(RANGE_LIMIT); - } } }; @@ -50,4 +51,5 @@ public enum Bonus { * @return a new player with the modified values */ abstract public Player applyTo(Player player); + } -- cgit v1.2.3