Browse Source

Added default constructor to Map

remotes/origin/menu
fdai7012 11 months ago
committed by Julian
parent
commit
00aff24f39
  1. 71
      src/main/java/pacmanGame/Map.java
  2. 35
      src/test/java/pacmanTests/MapTest.java

71
src/main/java/pacmanGame/Map.java

@ -1,7 +1,78 @@
package pacmanGame; package pacmanGame;
import java.util.HashMap;
public class Map { public class Map {
public final String[] mapClassic = {
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"ww............ww............ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww..........................ww",
"ww.wwww.ww.wwwwwwww.ww.wwww.ww",
"ww.wwww.ww.wwwwwwww.ww.wwww.ww",
"ww......ww....ww....ww......ww",
"wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"eeeeeee.eeeweeeeeeweee.eeeeeee",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"ww............ww............ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww...ww................ww...ww",
"wwww.ww.ww.wwwwwwww.ww.ww.wwww",
"wwww.ww.ww.wwwwwwww.ww.ww.wwww",
"ww......ww....ww....ww......ww",
"ww.wwwwwwwwww.ww.wwwwwwwwww.ww",
"ww.wwwwwwwwww.ww.wwwwwwwwww.ww",
"ww..........................ww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww"
};
public final HashMap<String, String> mapTypes = new HashMap<String,String>(){{
this.put("e", "empty");
this.put(".", "dot");
this.put("w", "wall");
}};
public Cell[][] cells; public Cell[][] cells;
public Vector2 size; public Vector2 size;
public Map() {
GenerateMap(mapClassic);
}
public Map(String[] mapData) {
GenerateMap(mapData);
}
public void GenerateMap(String[] mapData) {
int sizeY = mapData.length;
int sizeX = mapData[0].length();
size = new Vector2(sizeX, sizeY);
cells = new Cell[size.x][size.y];
for(int x = 0; x < size.x; x++) {
for(int y = 0; y < size.y; y++) {
Vector2 cellPos = new Vector2(x,y);
String cellType = mapTypes.get(String.valueOf(mapData[y].charAt(x)));
cells[x][y] = new Cell(cellPos, cellType);
}
}
}
} }

35
src/test/java/pacmanTests/MapTest.java

@ -0,0 +1,35 @@
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 MapTest {
@Test
void Map_differentCellTypes_areInterpretedCorrectly() {
// arrange
String[] mapTest = {
"wew",
"w..",
"ee."
};
String expectedTopLeft = "wall";
String expectedMiddle = "dot";
String expectedBottomRight = "dot";
// act
Map testMap = new Map(mapTest);
String topLeft = testMap.cells[0][0].type;
String middle = testMap.cells[1][1].type;
String bottomRight = testMap.cells[2][2].type;
// assert
assertThat(expectedTopLeft).isEqualTo(topLeft);
assertThat(expectedMiddle).isEqualTo(middle);
assertThat(expectedBottomRight).isEqualTo(bottomRight);
}
}
Loading…
Cancel
Save