aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-05 22:10:46 +0200
committerPacien TRAN-GIRARD2016-05-05 22:10:46 +0200
commit2e521cc8863796355666ce1dde69d5e513073954 (patch)
treec03ba201cea798f79a21debebd33b6ab72ec5498
parentbbd1aa31fbec7b46cb2c3a53e43101dbfa40897a (diff)
downloadxblast-2e521cc8863796355666ce1dde69d5e513073954.tar.gz
Modularize player priority management
-rw-r--r--src/ch/epfl/xblast/server/GameState.java57
-rw-r--r--src/ch/epfl/xblast/server/PlayerPriorityManager.java82
-rw-r--r--src/ch/epfl/xblast/server/debug/GameStatePrinter.java3
3 files changed, 85 insertions, 57 deletions
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java
index 7c56153..50b0ff2 100644
--- a/src/ch/epfl/xblast/server/GameState.java
+++ b/src/ch/epfl/xblast/server/GameState.java
@@ -18,11 +18,6 @@ import java.util.stream.Stream;
18public final class GameState { 18public final class GameState {
19 19
20 /** 20 /**
21 * The list of player priority order permutations.
22 */
23 private static final List<List<PlayerID>> PLAYER_PRIORITY_ORDERS = GameState.buildPlayerPriorityOrderList();
24
25 /**
26 * The list of bonuses to choose randomly from. 21 * The list of bonuses to choose randomly from.
27 */ 22 */
28 private static final Block[] RANDOM_BONUSES = new Block[]{Block.BONUS_BOMB, Block.BONUS_RANGE, Block.FREE}; 23 private static final Block[] RANDOM_BONUSES = new Block[]{Block.BONUS_BOMB, Block.BONUS_RANGE, Block.FREE};
@@ -80,15 +75,6 @@ public final class GameState {
80 } 75 }
81 76
82 /** 77 /**
83 * Builds and returns the player priority order permutations.
84 *
85 * @return the list of player priority orders
86 */
87 private static List<List<PlayerID>> buildPlayerPriorityOrderList() {
88 return Lists.permutations(Arrays.asList(PlayerID.values()));
89 }
90
91 /**
92 * Returns a randomly chosen bonus Block with an equal probability distribution. 78 * Returns a randomly chosen bonus Block with an equal probability distribution.
93 * 79 *
94 * @return a random bonus block 80 * @return a random bonus block
@@ -582,47 +568,6 @@ public final class GameState {
582 return new GameState(this.ticks + 1, board1, players1, bombs1, explosions1, blasts1); 568 return new GameState(this.ticks + 1, board1, players1, bombs1, explosions1, blasts1);
583 } 569 }
584 570
585 /**
586 * Returns the current player priority order permutation.
587 *
588 * @return the player priority order
589 */
590 private List<PlayerID> currentPlayerPriorityOrder() {
591 int priorityIndex = this.ticks % GameState.PLAYER_PRIORITY_ORDERS.size();
592 return GameState.PLAYER_PRIORITY_ORDERS.get(priorityIndex);
593 }
594
595 /**
596 * Resolves a conflict according to the current priority order.
597 *
598 * @param claimants the list of claimants
599 * @return the highest priority player
600 */
601 private PlayerID resolveConflict(List<PlayerID> claimants) {
602 if (claimants == null || claimants.isEmpty()) return null;
603
604 return this.currentPlayerPriorityOrder().stream()
605 .filter(claimants::contains)
606 .findFirst().orElse(null);
607 }
608
609 /**
610 * Resolves a conflict according to the current priority order.
611 *
612 * @param claimants the list of claimants
613 * @return the highest priority player
614 */
615 public Player resolvePlayerConflict(List<Player> claimants) {
616 if (claimants == null || claimants.isEmpty()) return null;
617
618 Map<PlayerID, Player> claimantsMap = claimants.stream()
619 .collect(Collectors.toMap(Player::id, Function.identity()));
620
621 List<PlayerID> claimantsIDs = claimants.stream()
622 .map(Player::id).collect(Collectors.toList());
623
624 return claimantsMap.get(this.resolveConflict(claimantsIDs));
625 }
626 571
627 /** 572 /**
628 * Returns a mapping of players from their location. 573 * Returns a mapping of players from their location.
@@ -648,7 +593,7 @@ public final class GameState {
648 return this.mapPlayersCells(players).entrySet().stream() 593 return this.mapPlayersCells(players).entrySet().stream()
649 .collect(Collectors.toMap( 594 .collect(Collectors.toMap(
650 Map.Entry::getKey, 595 Map.Entry::getKey,
651 e -> resolvePlayerConflict(e.getValue()))); 596 e -> PlayerPriorityManager.resolvePlayerConflict(e.getValue(), this.ticks())));
652 } 597 }
653 598
654 /** 599 /**
diff --git a/src/ch/epfl/xblast/server/PlayerPriorityManager.java b/src/ch/epfl/xblast/server/PlayerPriorityManager.java
new file mode 100644
index 0000000..ce6e12a
--- /dev/null
+++ b/src/ch/epfl/xblast/server/PlayerPriorityManager.java
@@ -0,0 +1,82 @@
1package ch.epfl.xblast.server;
2
3import ch.epfl.xblast.Lists;
4import ch.epfl.xblast.PlayerID;
5
6import java.util.Arrays;
7import java.util.List;
8import java.util.Map;
9import java.util.function.Function;
10import java.util.stream.Collectors;
11
12/**
13 * The player priority manager providing a sequential priority conflict resolution method.
14 *
15 * @author Pacien TRAN-GIRARD (261948)
16 */
17public final class PlayerPriorityManager {
18
19 /**
20 * The list of player priority order permutations.
21 */
22 private static final List<List<PlayerID>> PLAYER_PRIORITY_ORDERS = buildPlayerPriorityOrderList();
23
24 private PlayerPriorityManager() {
25 // Static class
26 }
27
28 /**
29 * Builds and returns the player priority order permutations.
30 *
31 * @return the list of player priority orders
32 */
33 private static List<List<PlayerID>> buildPlayerPriorityOrderList() {
34 return Lists.permutations(Arrays.asList(PlayerID.values()));
35 }
36
37 /**
38 * Returns the current player priority order permutation.
39 *
40 * @param ticks the current GameState ticks
41 * @return the player priority order
42 */
43 private static List<PlayerID> getCurrentPlayerPriorityOrder(int ticks) {
44 int priorityIndex = ticks % PLAYER_PRIORITY_ORDERS.size();
45 return PLAYER_PRIORITY_ORDERS.get(priorityIndex);
46 }
47
48 /**
49 * Resolves a conflict according to the current priority order.
50 *
51 * @param claimants the list of claimants
52 * @param ticks the current GameState ticks
53 * @return the highest priority player
54 */
55 private static PlayerID resolveConflict(List<PlayerID> claimants, int ticks) {
56 if (claimants == null || claimants.isEmpty()) return null;
57
58 return getCurrentPlayerPriorityOrder(ticks).stream()
59 .filter(claimants::contains)
60 .findFirst().orElse(null);
61 }
62
63 /**
64 * Resolves a conflict according to the current priority order.
65 *
66 * @param claimants the list of claimants
67 * @param ticks the current GameState ticks
68 * @return the highest priority player
69 */
70 public static Player resolvePlayerConflict(List<Player> claimants, int ticks) {
71 if (claimants == null || claimants.isEmpty()) return null;
72
73 Map<PlayerID, Player> claimantsMap = claimants.stream()
74 .collect(Collectors.toMap(Player::id, Function.identity()));
75
76 List<PlayerID> claimantsIDs = claimants.stream()
77 .map(Player::id).collect(Collectors.toList());
78
79 return claimantsMap.get(resolveConflict(claimantsIDs, ticks));
80 }
81
82}
diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
index c3d9338..bb02843 100644
--- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
+++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
@@ -5,6 +5,7 @@ import ch.epfl.xblast.Direction;
5import ch.epfl.xblast.server.Block; 5import ch.epfl.xblast.server.Block;
6import ch.epfl.xblast.server.GameState; 6import ch.epfl.xblast.server.GameState;
7import ch.epfl.xblast.server.Player; 7import ch.epfl.xblast.server.Player;
8import ch.epfl.xblast.server.PlayerPriorityManager;
8 9
9import java.util.List; 10import java.util.List;
10import java.util.Map; 11import java.util.Map;
@@ -45,7 +46,7 @@ public final class GameStatePrinter {
45 if (s.bombedCells().containsKey(c)) 46 if (s.bombedCells().containsKey(c))
46 return ANSIColor.RED.coloredText("@@"); 47 return ANSIColor.RED.coloredText("@@");
47 48
48 Player p = s.resolvePlayerConflict(pl.get(c)); 49 Player p = PlayerPriorityManager.resolvePlayerConflict(pl.get(c), s.ticks());
49 if (p != null) 50 if (p != null)
50 return GameStatePrinter.stringForPlayer(p); 51 return GameStatePrinter.stringForPlayer(p);
51 52