aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-04-06 23:51:09 +0200
committerPacien TRAN-GIRARD2016-04-06 23:51:09 +0200
commit7bf9f6e6195fa5c021871bd025cf8f4582a61fec (patch)
tree12a297d7ea48cf20621b406b736e610c18e8ee46 /src
parenta33c9df30d209e8ba24b51507ffdeab17008dbe8 (diff)
downloadxblast-7bf9f6e6195fa5c021871bd025cf8f4582a61fec.tar.gz
Complete GameStatePrinter with bombs and blasts
Diffstat (limited to 'src')
-rw-r--r--src/ch/epfl/xblast/server/debug/GameStatePrinter.java49
1 files changed, 27 insertions, 22 deletions
diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
index a846421..d81c6b9 100644
--- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
+++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
@@ -1,12 +1,13 @@
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.Block; 4import ch.epfl.xblast.server.*;
5import ch.epfl.xblast.server.Board;
6import ch.epfl.xblast.server.GameState;
7import ch.epfl.xblast.server.Player;
8 5
9import java.util.List; 6import java.util.List;
7import java.util.Map;
8import java.util.Set;
9import java.util.function.Function;
10import java.util.stream.Collectors;
10 11
11/** 12/**
12 * Game state printer utility class that outputs the board to the terminal. 13 * Game state printer utility class that outputs the board to the terminal.
@@ -17,7 +18,6 @@ import java.util.List;
17public final class GameStatePrinter { 18public final class GameStatePrinter {
18 19
19 private enum ANSIColor { 20 private enum ANSIColor {
20
21 BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7); 21 BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7);
22 22
23 private static final String CSI = "\u001b["; 23 private static final String CSI = "\u001b[";
@@ -40,31 +40,38 @@ public final class GameStatePrinter {
40 40
41 public static void printGameState(GameState s) { 41 public static void printGameState(GameState s) {
42 printStats(s); 42 printStats(s);
43 printBoard(s.board(), s.alivePlayers()); 43 printBoard(s.board(), s.alivePlayers(), s.bombedCells(), s.blastedCells());
44 } 44 }
45 45
46 private static void printStats(GameState s) { 46 private static void printStats(GameState s) {
47 System.out.println(s); 47 System.out.println(s);
48 } 48 }
49 49
50 private static void printBoard(Board b, List<Player> ps) { 50 private static void printBoard(Board b, List<Player> ps, Map<Cell, Bomb> bc, Set<Cell> blastedCells) {
51 Map<Cell, Player> playerMap = ps.stream()
52 .collect(Collectors.toMap(p -> p.position().containingCell(), Function.identity()));
53
51 for (int y = 0; y < Cell.ROWS; ++y) { 54 for (int y = 0; y < Cell.ROWS; ++y) {
52 xLoop: 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 Cell c = new Cell(x, y); 57
55 for (Player p : ps) {
56 if (p.position().containingCell().equals(c)) {
57 System.out.print(stringForPlayer(p));
58 continue xLoop;
59 }
60 }
61 Block block = b.blockAt(c);
62 System.out.print(stringForBlock(block));
63 }
64 System.out.println(); 58 System.out.println();
65 } 59 }
66 } 60 }
67 61
62 private static String stringForCellContent(Cell c, Board b, Map<Cell, Bomb> bc, Map<Cell, Player> p, Set<Cell> blastedCells) {
63 if (bc.containsKey(c))
64 return ANSIColor.RED.coloredText("@@");
65
66 if (p.containsKey(c))
67 return GameStatePrinter.stringForPlayer(p.get(c));
68
69 if (blastedCells.contains(c))
70 return ANSIColor.MAGENTA.coloredText("**");
71
72 return GameStatePrinter.stringForBlock(b.blockAt(c));
73 }
74
68 private static String stringForPlayer(Player p) { 75 private static String stringForPlayer(Player p) {
69 StringBuilder b = new StringBuilder(); 76 StringBuilder b = new StringBuilder();
70 b.append(p.id().ordinal() + 1); 77 b.append(p.id().ordinal() + 1);
@@ -87,8 +94,6 @@ public final class GameStatePrinter {
87 94
88 private static String stringForBlock(Block b) { 95 private static String stringForBlock(Block b) {
89 switch (b) { 96 switch (b) {
90 case FREE:
91 return " ";
92 case INDESTRUCTIBLE_WALL: 97 case INDESTRUCTIBLE_WALL:
93 return ANSIColor.BLACK.coloredText("##"); 98 return ANSIColor.BLACK.coloredText("##");
94 case DESTRUCTIBLE_WALL: 99 case DESTRUCTIBLE_WALL:
@@ -100,7 +105,7 @@ public final class GameStatePrinter {
100 case BONUS_RANGE: 105 case BONUS_RANGE:
101 return ANSIColor.GREEN.coloredText("+r"); 106 return ANSIColor.GREEN.coloredText("+r");
102 default: 107 default:
103 throw new Error(); 108 return " ";
104 } 109 }
105 } 110 }
106 111