aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-03-15 14:13:35 +0100
committerPacien TRAN-GIRARD2016-03-15 14:13:35 +0100
commit74e7fe38193d28d58f41d23ccd41634b988394ef (patch)
tree0ae178a382d52cea8c6abb89338d2c4e4cb77900
parent3b8c85a5f483c856b34c7026f48abd4f355b67d7 (diff)
downloadxblast-74e7fe38193d28d58f41d23ccd41634b988394ef.tar.gz
Simplify code
-rw-r--r--src/ch/epfl/xblast/server/Block.java4
-rw-r--r--src/ch/epfl/xblast/server/GameState.java78
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 {
48 /** 48 /**
49 * Main builder, used by the bonus blocks 49 * Main builder, used by the bonus blocks
50 */ 50 */
51 private Block(Bonus maybeAssociatedBonus) { 51 Block(Bonus maybeAssociatedBonus) {
52 this.maybeAssociatedBonus = maybeAssociatedBonus; 52 this.maybeAssociatedBonus = maybeAssociatedBonus;
53 } 53 }
54 54
55 /** 55 /**
56 * Default builder, used by the non-bonus blocks 56 * Default builder, used by the non-bonus blocks
57 */ 57 */
58 private Block() { 58 Block() {
59 this.maybeAssociatedBonus = null; 59 this.maybeAssociatedBonus = null;
60 } 60 }
61 61
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;
10import java.util.Objects; 10import java.util.Objects;
11import java.util.Optional; 11import java.util.Optional;
12import java.util.stream.Collectors; 12import java.util.stream.Collectors;
13import java.util.stream.Stream;
13 14
14/** 15/**
15 * GameState representing the current game state. 16 * GameState representing the current game state.
@@ -29,22 +30,20 @@ public final class GameState {
29 /** 30 /**
30 * Compute the next state of a blast. 31 * Compute the next state of a blast.
31 * 32 *
32 * @param blasts0 existing particles 33 * @param blasts existing particles
33 * @param board0 the game's board 34 * @param board the game's board
34 * @param explosions0 active explosions 35 * @param explosions active explosions
35 * @return the position of the explosion's particles for the next state. 36 * @return the position of the explosion's particles for the next state.
36 */ 37 */
37 private static List<Sq<Cell>> nextBlasts(List<Sq<Cell>> blasts0, Board board0, List<Sq<Sq<Cell>>> explosions0) { 38 private static List<Sq<Cell>> nextBlasts(List<Sq<Cell>> blasts, Board board, List<Sq<Sq<Cell>>> explosions) {
38 List<Sq<Cell>> blasts1 = new ArrayList<>(); 39 return Stream.concat(
39 for (Sq<Cell> blastSeq : blasts0) { 40 blasts.stream()
40 if (!blastSeq.tail().isEmpty() && board0.blockAt(blastSeq.head()).isFree()) { 41 .filter(blastSeq -> !blastSeq.tail().isEmpty())
41 blasts1.add(blastSeq.tail()); 42 .filter(blastSeq -> board.blockAt(blastSeq.head()).isFree())
42 } 43 .map(Sq::tail),
43 } 44 explosions.stream()
44 for (Sq<Sq<Cell>> explosion0 : explosions0) { 45 .map(Sq::head)
45 blasts1.add(explosion0.head()); 46 ).collect(Collectors.toList());
46 }
47 return blasts1;
48 } 47 }
49 48
50 /** 49 /**
@@ -62,11 +61,10 @@ public final class GameState {
62 public GameState(int ticks, Board board, List<Player> players, List<Bomb> bombs, List<Sq<Sq<Cell>>> explosions, List<Sq<Cell>> blasts) { 61 public GameState(int ticks, Board board, List<Player> players, List<Bomb> bombs, List<Sq<Sq<Cell>>> explosions, List<Sq<Cell>> blasts) {
63 this.ticks = ArgumentChecker.requireNonNegative(ticks); 62 this.ticks = ArgumentChecker.requireNonNegative(ticks);
64 this.board = Objects.requireNonNull(board); 63 this.board = Objects.requireNonNull(board);
65 if (players.size() == 4) { 64
66 this.players = players; 65 if (players.size() != 4) throw new IllegalArgumentException();
67 } else { 66 this.players = players;
68 throw new IllegalArgumentException(); 67
69 }
70 this.bombs = Objects.requireNonNull(bombs); 68 this.bombs = Objects.requireNonNull(bombs);
71 this.explosions = Objects.requireNonNull(explosions); 69 this.explosions = Objects.requireNonNull(explosions);
72 this.blasts = Objects.requireNonNull(blasts); 70 this.blasts = Objects.requireNonNull(blasts);
@@ -79,14 +77,7 @@ public final class GameState {
79 * @param players list of the players 77 * @param players list of the players
80 */ 78 */
81 public GameState(Board board, List<Player> players) { 79 public GameState(Board board, List<Player> players) {
82 this(0, 80 this(0, board, players, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
83 board,
84 players,
85 new ArrayList<Bomb>(),
86 new ArrayList<Sq<Sq<Cell>>>(),
87 new ArrayList<Sq<Cell>>()
88 );
89
90 } 81 }
91 82
92 /** 83 /**
@@ -102,17 +93,7 @@ public final class GameState {
102 * @return true if all the players are dead or if the game reached the time limit 93 * @return true if all the players are dead or if the game reached the time limit
103 */ 94 */
104 public boolean isGameOver() { 95 public boolean isGameOver() {
105 int alivePlayerCount = 0; 96 return this.alivePlayers().isEmpty() || this.ticks >= Ticks.TOTAL_TICKS;
106 for (Player player : this.players) {
107 if (player.isAlive()) {
108 alivePlayerCount += 1;
109 }
110 }
111 if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) {
112 return true;
113 } else {
114 return false;
115 }
116 } 97 }
117 98
118 /** 99 /**
@@ -130,12 +111,10 @@ public final class GameState {
130 * @return the ID of the player who won the game. 111 * @return the ID of the player who won the game.
131 */ 112 */
132 public Optional<PlayerID> winner() { 113 public Optional<PlayerID> winner() {
133 if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { 114 if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS)
134 PlayerID winnerId = this.players.get(0).id(); 115 return Optional.of(this.players.get(0).id());
135 return Optional.of(winnerId); 116 else
136 } else {
137 return Optional.empty(); 117 return Optional.empty();
138 }
139 } 118 }
140 119
141 /** 120 /**
@@ -158,15 +137,10 @@ public final class GameState {
158 * @return a list of the alive players 137 * @return a list of the alive players
159 */ 138 */
160 public List<Player> alivePlayers() { 139 public List<Player> alivePlayers() {
161 List<Player> alivePlayers = new ArrayList<>(); 140 return this.players
162 141 .stream()
163 for (Player player : this.players) { 142 .filter(Player::isAlive)
164 if (player.isAlive()) { 143 .collect(Collectors.toList());
165 alivePlayers.add(player);
166 }
167 }
168
169 return alivePlayers;
170 } 144 }
171 145
172} 146}