aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-04-08 11:35:08 +0200
committerPacien TRAN-GIRARD2016-04-08 11:35:08 +0200
commit492ca4fe5b9b193948274419d6361c2829eb10bf (patch)
tree7ddf3d742253026d5aea4521e34e6c769ad6f508 /src
parent3dd23622d97b9fdd75c22554250a793a95b08a55 (diff)
downloadxblast-492ca4fe5b9b193948274419d6361c2829eb10bf.tar.gz
Make player collisions great again
Diffstat (limited to 'src')
-rw-r--r--src/ch/epfl/xblast/server/GameState.java48
-rw-r--r--src/ch/epfl/xblast/server/debug/GameStatePrinter.java29
2 files changed, 47 insertions, 30 deletions
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java
index c51afe7..4ed1af5 100644
--- a/src/ch/epfl/xblast/server/GameState.java
+++ b/src/ch/epfl/xblast/server/GameState.java
@@ -473,13 +473,29 @@ public final class GameState {
473 * @return the highest priority player 473 * @return the highest priority player
474 */ 474 */
475 private PlayerID resolveConflict(List<PlayerID> claimants) { 475 private PlayerID resolveConflict(List<PlayerID> claimants) {
476 if (claimants == null || claimants.isEmpty()) throw new IllegalArgumentException(); 476 if (claimants == null || claimants.isEmpty()) return null;
477 477
478 return this.currentPlayerPriorityOrder() 478 return this.currentPlayerPriorityOrder().stream()
479 .stream()
480 .filter(claimants::contains) 479 .filter(claimants::contains)
481 .findFirst() 480 .findFirst().get();
482 .get(); 481 }
482
483 /**
484 * Resolves a conflict according to the current priority order.
485 *
486 * @param claimants the list of claimants
487 * @return the highest priority player
488 */
489 public Player resolvePlayerConflict(List<Player> claimants) {
490 if (claimants == null || claimants.isEmpty()) return null;
491
492 Map<PlayerID, Player> claimantsMap = claimants.stream()
493 .collect(Collectors.toMap(Player::id, Function.identity()));
494
495 List<PlayerID> claimantsIDs = claimants.stream()
496 .map(Player::id).collect(Collectors.toList());
497
498 return claimantsMap.get(this.resolveConflict(claimantsIDs));
483 } 499 }
484 500
485 /** 501 /**
@@ -488,11 +504,11 @@ public final class GameState {
488 * @param players a list of players to map 504 * @param players a list of players to map
489 * @return the location->player mapping 505 * @return the location->player mapping
490 */ 506 */
491 private Map<Cell, List<PlayerID>> mapPlayersCells(List<Player> players) { 507 public Map<Cell, List<Player>> mapPlayersCells(List<Player> players) {
492 return players.stream() 508 return players.stream()
493 .collect( 509 .collect(
494 Collectors.groupingBy(p -> p.position().containingCell(), 510 Collectors.groupingBy(p -> p.position().containingCell(),
495 Collectors.mapping(Player::id, Collectors.toList()))); 511 Collectors.mapping(Function.identity(), Collectors.toList())));
496 } 512 }
497 513
498 /** 514 /**
@@ -502,11 +518,11 @@ public final class GameState {
502 * @param players a list of players to map 518 * @param players a list of players to map
503 * @return the location->top-priority player mapping 519 * @return the location->top-priority player mapping
504 */ 520 */
505 private Map<Cell, PlayerID> mapTopPriorityPlayerCells(List<Player> players) { 521 private Map<Cell, Player> mapTopPriorityPlayerCells(List<Player> players) {
506 return this.mapPlayersCells(players).entrySet().stream() 522 return this.mapPlayersCells(players).entrySet().stream()
507 .collect(Collectors.toMap( 523 .collect(Collectors.toMap(
508 Map.Entry::getKey, 524 Map.Entry::getKey,
509 e -> resolveConflict(e.getValue()))); 525 e -> resolvePlayerConflict(e.getValue())));
510 } 526 }
511 527
512 /** 528 /**
@@ -519,7 +535,9 @@ public final class GameState {
519 this.alivePlayers().stream() 535 this.alivePlayers().stream()
520 .filter(p -> p.position().isCentral()) 536 .filter(p -> p.position().isCentral())
521 .filter(p -> this.board.blockAt(p.position().containingCell()).isBonus()) 537 .filter(p -> this.board.blockAt(p.position().containingCell()).isBonus())
522 .collect(Collectors.toList())); 538 .collect(Collectors.toList()))
539 .entrySet().stream()
540 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().id()));
523 } 541 }
524 542
525 /** 543 /**
@@ -542,13 +560,13 @@ public final class GameState {
542 * @return the conflict-free set of bomb drop events 560 * @return the conflict-free set of bomb drop events
543 */ 561 */
544 private Set<PlayerID> discardConflictingBombDropEvents(Set<PlayerID> bombDropEvents) { 562 private Set<PlayerID> discardConflictingBombDropEvents(Set<PlayerID> bombDropEvents) {
545 Map<Cell, PlayerID> bombDropMap = this.mapTopPriorityPlayerCells( 563 return this.mapTopPriorityPlayerCells(
546 this.alivePlayers().stream() 564 this.alivePlayers().stream()
547 .filter(p -> bombDropEvents.contains(p.id())) 565 .filter(p -> bombDropEvents.contains(p.id()))
548 .collect(Collectors.toList()) 566 .collect(Collectors.toList()))
549 ); 567 .values().stream()
550 568 .map(Player::id)
551 return bombDropMap.isEmpty() ? EnumSet.noneOf(PlayerID.class) : EnumSet.copyOf(bombDropMap.values()); 569 .collect(Collectors.toSet());
552 } 570 }
553 571
554 @Override 572 @Override
diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
index d81c6b9..575b09a 100644
--- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
+++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
@@ -1,13 +1,12 @@
1package ch.epfl.xblast.server.debug; 1package ch.epfl.xblast.server.debug;
2 2
3import ch.epfl.xblast.Cell; 3import ch.epfl.xblast.Cell;
4import ch.epfl.xblast.server.*; 4import ch.epfl.xblast.server.Block;
5import ch.epfl.xblast.server.GameState;
6import ch.epfl.xblast.server.Player;
5 7
6import java.util.List; 8import java.util.List;
7import java.util.Map; 9import java.util.Map;
8import java.util.Set;
9import java.util.function.Function;
10import java.util.stream.Collectors;
11 10
12/** 11/**
13 * Game state printer utility class that outputs the board to the terminal. 12 * Game state printer utility class that outputs the board to the terminal.
@@ -40,36 +39,36 @@ public final class GameStatePrinter {
40 39
41 public static void printGameState(GameState s) { 40 public static void printGameState(GameState s) {
42 printStats(s); 41 printStats(s);
43 printBoard(s.board(), s.alivePlayers(), s.bombedCells(), s.blastedCells()); 42 printBoard(s);
44 } 43 }
45 44
46 private static void printStats(GameState s) { 45 private static void printStats(GameState s) {
47 System.out.println(s); 46 System.out.println(s);
48 } 47 }
49 48
50 private static void printBoard(Board b, List<Player> ps, Map<Cell, Bomb> bc, Set<Cell> blastedCells) { 49 private static void printBoard(GameState s) {
51 Map<Cell, Player> playerMap = ps.stream() 50 Map<Cell, List<Player>> playerMap = s.mapPlayersCells(s.alivePlayers());
52 .collect(Collectors.toMap(p -> p.position().containingCell(), Function.identity()));
53 51
54 for (int y = 0; y < Cell.ROWS; ++y) { 52 for (int y = 0; y < Cell.ROWS; ++y) {
55 for (int x = 0; x < Cell.COLUMNS; ++x) 53 for (int x = 0; x < Cell.COLUMNS; ++x)
56 System.out.print(GameStatePrinter.stringForCellContent(new Cell(x, y), b, bc, playerMap, blastedCells)); 54 System.out.print(GameStatePrinter.stringForCellContent(s, new Cell(x, y), playerMap));
57 55
58 System.out.println(); 56 System.out.println();
59 } 57 }
60 } 58 }
61 59
62 private static String stringForCellContent(Cell c, Board b, Map<Cell, Bomb> bc, Map<Cell, Player> p, Set<Cell> blastedCells) { 60 private static String stringForCellContent(GameState s, Cell c, Map<Cell, List<Player>> pl) {
63 if (bc.containsKey(c)) 61 if (s.bombedCells().containsKey(c))
64 return ANSIColor.RED.coloredText("@@"); 62 return ANSIColor.RED.coloredText("@@");
65 63
66 if (p.containsKey(c)) 64 Player p = s.resolvePlayerConflict(pl.get(c));
67 return GameStatePrinter.stringForPlayer(p.get(c)); 65 if (p != null)
66 return GameStatePrinter.stringForPlayer(p);
68 67
69 if (blastedCells.contains(c)) 68 if (s.blastedCells().contains(c))
70 return ANSIColor.MAGENTA.coloredText("**"); 69 return ANSIColor.MAGENTA.coloredText("**");
71 70
72 return GameStatePrinter.stringForBlock(b.blockAt(c)); 71 return GameStatePrinter.stringForBlock(s.board().blockAt(c));
73 } 72 }
74 73
75 private static String stringForPlayer(Player p) { 74 private static String stringForPlayer(Player p) {