From 7bf9f6e6195fa5c021871bd025cf8f4582a61fec Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 6 Apr 2016 23:51:09 +0200 Subject: Complete GameStatePrinter with bombs and blasts --- .../epfl/xblast/server/debug/GameStatePrinter.java | 49 ++++++++++++---------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'src') 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 @@ package ch.epfl.xblast.server.debug; import ch.epfl.xblast.Cell; -import ch.epfl.xblast.server.Block; -import ch.epfl.xblast.server.Board; -import ch.epfl.xblast.server.GameState; -import ch.epfl.xblast.server.Player; +import ch.epfl.xblast.server.*; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; /** * Game state printer utility class that outputs the board to the terminal. @@ -17,7 +18,6 @@ import java.util.List; public final class GameStatePrinter { private enum ANSIColor { - BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7); private static final String CSI = "\u001b["; @@ -40,31 +40,38 @@ public final class GameStatePrinter { public static void printGameState(GameState s) { printStats(s); - printBoard(s.board(), s.alivePlayers()); + printBoard(s.board(), s.alivePlayers(), s.bombedCells(), s.blastedCells()); } private static void printStats(GameState s) { System.out.println(s); } - private static void printBoard(Board b, List ps) { + private static void printBoard(Board b, List ps, Map bc, Set blastedCells) { + Map playerMap = ps.stream() + .collect(Collectors.toMap(p -> p.position().containingCell(), Function.identity())); + for (int y = 0; y < Cell.ROWS; ++y) { - xLoop: - for (int x = 0; x < Cell.COLUMNS; ++x) { - Cell c = new Cell(x, y); - for (Player p : ps) { - if (p.position().containingCell().equals(c)) { - System.out.print(stringForPlayer(p)); - continue xLoop; - } - } - Block block = b.blockAt(c); - System.out.print(stringForBlock(block)); - } + for (int x = 0; x < Cell.COLUMNS; ++x) + System.out.print(GameStatePrinter.stringForCellContent(new Cell(x, y), b, bc, playerMap, blastedCells)); + System.out.println(); } } + private static String stringForCellContent(Cell c, Board b, Map bc, Map p, Set blastedCells) { + if (bc.containsKey(c)) + return ANSIColor.RED.coloredText("@@"); + + if (p.containsKey(c)) + return GameStatePrinter.stringForPlayer(p.get(c)); + + if (blastedCells.contains(c)) + return ANSIColor.MAGENTA.coloredText("**"); + + return GameStatePrinter.stringForBlock(b.blockAt(c)); + } + private static String stringForPlayer(Player p) { StringBuilder b = new StringBuilder(); b.append(p.id().ordinal() + 1); @@ -87,8 +94,6 @@ public final class GameStatePrinter { private static String stringForBlock(Block b) { switch (b) { - case FREE: - return " "; case INDESTRUCTIBLE_WALL: return ANSIColor.BLACK.coloredText("##"); case DESTRUCTIBLE_WALL: @@ -100,7 +105,7 @@ public final class GameStatePrinter { case BONUS_RANGE: return ANSIColor.GREEN.coloredText("+r"); default: - throw new Error(); + return " "; } } -- cgit v1.2.3