diff options
Diffstat (limited to 'src/ch/epfl/xblast/server/debug/GameStatePrinter.java')
-rw-r--r-- | src/ch/epfl/xblast/server/debug/GameStatePrinter.java | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java index ea8b360..575b09a 100644 --- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java +++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java | |||
@@ -2,43 +2,75 @@ package ch.epfl.xblast.server.debug; | |||
2 | 2 | ||
3 | import ch.epfl.xblast.Cell; | 3 | import ch.epfl.xblast.Cell; |
4 | import ch.epfl.xblast.server.Block; | 4 | import ch.epfl.xblast.server.Block; |
5 | import ch.epfl.xblast.server.Board; | ||
6 | import ch.epfl.xblast.server.GameState; | 5 | import ch.epfl.xblast.server.GameState; |
7 | import ch.epfl.xblast.server.Player; | 6 | import ch.epfl.xblast.server.Player; |
8 | 7 | ||
9 | import java.util.List; | 8 | import java.util.List; |
9 | import java.util.Map; | ||
10 | 10 | ||
11 | /** | 11 | /** |
12 | * 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. |
13 | * | 13 | * |
14 | * @author EPFL | 14 | * @author EPFL |
15 | * @author Pacien TRAN-GIRARD (261948) | ||
15 | */ | 16 | */ |
16 | public final class GameStatePrinter { | 17 | public final class GameStatePrinter { |
17 | 18 | ||
19 | private enum ANSIColor { | ||
20 | BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7); | ||
21 | |||
22 | private static final String CSI = "\u001b["; | ||
23 | private static final String END = "m"; | ||
24 | private static final int SET_COMMAND = 4; | ||
25 | |||
26 | private final int code; | ||
27 | |||
28 | ANSIColor(int code) { | ||
29 | this.code = code; | ||
30 | } | ||
31 | |||
32 | public String coloredText(String t) { | ||
33 | return String.format("%s%d%d%s%s%s%s", CSI, SET_COMMAND, this.code, END, t, CSI, END); | ||
34 | } | ||
35 | } | ||
36 | |||
18 | private GameStatePrinter() { | 37 | private GameStatePrinter() { |
19 | } | 38 | } |
20 | 39 | ||
21 | public static void printGameState(GameState s) { | 40 | public static void printGameState(GameState s) { |
22 | List<Player> ps = s.alivePlayers(); | 41 | printStats(s); |
23 | Board board = s.board(); | 42 | printBoard(s); |
43 | } | ||
44 | |||
45 | private static void printStats(GameState s) { | ||
46 | System.out.println(s); | ||
47 | } | ||
48 | |||
49 | private static void printBoard(GameState s) { | ||
50 | Map<Cell, List<Player>> playerMap = s.mapPlayersCells(s.alivePlayers()); | ||
24 | 51 | ||
25 | for (int y = 0; y < Cell.ROWS; ++y) { | 52 | for (int y = 0; y < Cell.ROWS; ++y) { |
26 | xLoop: | 53 | for (int x = 0; x < Cell.COLUMNS; ++x) |
27 | for (int x = 0; x < Cell.COLUMNS; ++x) { | 54 | System.out.print(GameStatePrinter.stringForCellContent(s, new Cell(x, y), playerMap)); |
28 | Cell c = new Cell(x, y); | 55 | |
29 | for (Player p : ps) { | ||
30 | if (p.position().containingCell().equals(c)) { | ||
31 | System.out.print(stringForPlayer(p)); | ||
32 | continue xLoop; | ||
33 | } | ||
34 | } | ||
35 | Block b = board.blockAt(c); | ||
36 | System.out.print(stringForBlock(b)); | ||
37 | } | ||
38 | System.out.println(); | 56 | System.out.println(); |
39 | } | 57 | } |
40 | } | 58 | } |
41 | 59 | ||
60 | private static String stringForCellContent(GameState s, Cell c, Map<Cell, List<Player>> pl) { | ||
61 | if (s.bombedCells().containsKey(c)) | ||
62 | return ANSIColor.RED.coloredText("@@"); | ||
63 | |||
64 | Player p = s.resolvePlayerConflict(pl.get(c)); | ||
65 | if (p != null) | ||
66 | return GameStatePrinter.stringForPlayer(p); | ||
67 | |||
68 | if (s.blastedCells().contains(c)) | ||
69 | return ANSIColor.MAGENTA.coloredText("**"); | ||
70 | |||
71 | return GameStatePrinter.stringForBlock(s.board().blockAt(c)); | ||
72 | } | ||
73 | |||
42 | private static String stringForPlayer(Player p) { | 74 | private static String stringForPlayer(Player p) { |
43 | StringBuilder b = new StringBuilder(); | 75 | StringBuilder b = new StringBuilder(); |
44 | b.append(p.id().ordinal() + 1); | 76 | b.append(p.id().ordinal() + 1); |
@@ -56,25 +88,23 @@ public final class GameStatePrinter { | |||
56 | b.append('<'); | 88 | b.append('<'); |
57 | break; | 89 | break; |
58 | } | 90 | } |
59 | return b.toString(); | 91 | return ANSIColor.CYAN.coloredText(b.toString()); |
60 | } | 92 | } |
61 | 93 | ||
62 | private static String stringForBlock(Block b) { | 94 | private static String stringForBlock(Block b) { |
63 | switch (b) { | 95 | switch (b) { |
64 | case FREE: | ||
65 | return " "; | ||
66 | case INDESTRUCTIBLE_WALL: | 96 | case INDESTRUCTIBLE_WALL: |
67 | return "##"; | 97 | return ANSIColor.BLACK.coloredText("##"); |
68 | case DESTRUCTIBLE_WALL: | 98 | case DESTRUCTIBLE_WALL: |
69 | return "??"; | 99 | return ANSIColor.BLACK.coloredText("??"); |
70 | case CRUMBLING_WALL: | 100 | case CRUMBLING_WALL: |
71 | return "¿¿"; | 101 | return ANSIColor.BLACK.coloredText("¿¿"); |
72 | case BONUS_BOMB: | 102 | case BONUS_BOMB: |
73 | return "+b"; | 103 | return ANSIColor.GREEN.coloredText("+b"); |
74 | case BONUS_RANGE: | 104 | case BONUS_RANGE: |
75 | return "+r"; | 105 | return ANSIColor.GREEN.coloredText("+r"); |
76 | default: | 106 | default: |
77 | throw new Error(); | 107 | return " "; |
78 | } | 108 | } |
79 | } | 109 | } |
80 | 110 | ||