From c68803a846cdd81e4343372e7bb931d77efa1a21 Mon Sep 17 00:00:00 2001 From: fdai7012 Date: Wed, 17 Jan 2024 17:05:28 +0100 Subject: [PATCH] Implemented visualization update to VisualizerPlainText --- .../java/pacmanGame/VisualizerPlainText.java | 27 ++++++++++++-- .../pacmanTests/VisualizerPlainTextTest.java | 36 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/test/java/pacmanTests/VisualizerPlainTextTest.java diff --git a/src/main/java/pacmanGame/VisualizerPlainText.java b/src/main/java/pacmanGame/VisualizerPlainText.java index 893f5e9..17ceb9f 100644 --- a/src/main/java/pacmanGame/VisualizerPlainText.java +++ b/src/main/java/pacmanGame/VisualizerPlainText.java @@ -7,6 +7,12 @@ public class VisualizerPlainText implements Visualizer { private String output; public final GameManager gameManager; + public HashMap sprites = new HashMap(){{ + this.put("empty", " "); + this.put("dot", ". "); + this.put("wall", "[]"); + }}; + public VisualizerPlainText(GameManager gameManager) { this.gameManager = gameManager; } @@ -15,9 +21,26 @@ public class VisualizerPlainText implements Visualizer { public Object GetOutput() { return output; } - + @Override public void Update() { - // TODO Auto-generated method stub + output = ""; + Map map = gameManager.map; + + for(int y = 0; y < map.size.y; y++) { + for(int x = 0; x < map.size.x; x++) { + Cell cell = map.GetCell(new Vector2(x,y)); + + if (sprites.containsKey(cell.type)) { + + output += sprites.get(cell.type); + } + else { + System.out.println("unknown type" + cell.type); + } + } + output += "\n"; + } } + } diff --git a/src/test/java/pacmanTests/VisualizerPlainTextTest.java b/src/test/java/pacmanTests/VisualizerPlainTextTest.java new file mode 100644 index 0000000..d948611 --- /dev/null +++ b/src/test/java/pacmanTests/VisualizerPlainTextTest.java @@ -0,0 +1,36 @@ +package pacmanTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import pacmanGame.*; + + +class VisualizerPlainTextTest { + + @Test + void VisualizerPlainText_mapCells_areVisualized() { + // arrange + String[] mapTest = { + "w.w", + "e.e", + "w.w" + }; + + GameManager gameManager = new GameManager(); + gameManager.map = new Map(mapTest); + VisualizerPlainText vpt = new VisualizerPlainText(gameManager); + + String expected = "" + + "[]. []\n" + + " . \n" + + "[]. []\n"; + // act + vpt.Update(); + String result = (String)vpt.GetOutput(); + // assert + assertThat(expected).isEqualTo(result); + } +}