Browse Source

implemented ghosts visualisation to Visualiserplaintext

remotes/origin/menu
fdai2751 12 months ago
parent
commit
b6f4058f16
  1. 17
      src/main/java/pacmanGame/VisualizerPlainText.java
  2. 26
      src/test/java/pacmanTests/VisualizerPlainTextTest.java

17
src/main/java/pacmanGame/VisualizerPlainText.java

@ -13,6 +13,8 @@ public class VisualizerPlainText implements Visualizer {
this.put("wall", "[]"); this.put("wall", "[]");
}}; }};
public final String ghostSprite = "AA";
public VisualizerPlainText(GameManager gameManager) { public VisualizerPlainText(GameManager gameManager) {
this.gameManager = gameManager; this.gameManager = gameManager;
} }
@ -30,8 +32,17 @@ public class VisualizerPlainText implements Visualizer {
for(int y = 0; y < map.size.y; y++) { for(int y = 0; y < map.size.y; y++) {
for(int x = 0; x < map.size.x; x++) { for(int x = 0; x < map.size.x; x++) {
Cell cell = map.GetCell(new Vector2(x,y)); Cell cell = map.GetCell(new Vector2(x,y));
if (sprites.containsKey(cell.type)) {
boolean containsGhosts = false;
for( int i = 0; i < gameManager.ghosts.length; i++) {
if(gameManager.ghosts[i].position.equals(cell.pos)) {
containsGhosts = true;
}
}
if(containsGhosts) {
output += ghostSprite;
}
else if (sprites.containsKey(cell.type)) {
output += sprites.get(cell.type); output += sprites.get(cell.type);
} }
@ -42,5 +53,7 @@ public class VisualizerPlainText implements Visualizer {
output += "\n"; output += "\n";
} }
} }
} }

26
src/test/java/pacmanTests/VisualizerPlainTextTest.java

@ -33,4 +33,30 @@ class VisualizerPlainTextTest {
// assert // assert
assertThat(expected).isEqualTo(result); assertThat(expected).isEqualTo(result);
} }
@Test
void VisualizerPlainText_ghosts_areVisualized() {
// arrange
String[] mapTest = {
"w.w",
"e.e",
"w.w"
};
GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest);
VisualizerPlainText vpt = new VisualizerPlainText(gameManager);
gameManager.ghosts[0].position = new Vector2(1, 1);
String expected = ""
+ "[]. []\n"
+ " AA \n"
+ "[]. []\n";
// act
vpt.Update();
String result = (String)vpt.GetOutput();
// assert
assertThat(expected).isEqualTo(result);
}
} }
Loading…
Cancel
Save