diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index b85d99c..c198021 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -40,7 +40,6 @@ public class GameManager { public void gameOver() { System.out.println("Das Spiel ist vorbei! Pech!!"); } - public void Update() { if(!isPaused) { visualizer.Update(); diff --git a/src/main/java/pacmanGame/Ghost.java b/src/main/java/pacmanGame/Ghost.java index dc8a7d9..26e9845 100644 --- a/src/main/java/pacmanGame/Ghost.java +++ b/src/main/java/pacmanGame/Ghost.java @@ -1,12 +1,22 @@ package pacmanGame; public class Ghost { - public Vector2 position; - public final GameManager gameManager; - - public Ghost(GameManager gameManager) { - this.gameManager = gameManager; - this.position = new Vector2(-1, -1); - } - + public Vector2 position; + public final GameManager gameManager; + public final int ghostNumber; + + + public Ghost(GameManager gameManager, int ghostNumber) { + this.gameManager = gameManager; + this.ghostNumber = ghostNumber; + this.position = new Vector2(-1, -1); + } + + public void setPosition(Vector2 newPosition) { + this.position = newPosition; + } + + public void move(Vector2 direction) { + this.position = this.position.Add(direction); + } } diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 40ab6aa..21051b1 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -21,8 +21,8 @@ public class Map { "wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", - "eeeeeee.eeeweeeeeeweee.eeeeeee", - "wwwwwww.wweweeeeeeweww.wwwwwww", + "eeeeeee.eeew804629weee.eeeeeee", + "wwwwwww.wwewe1573eweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww", @@ -40,13 +40,28 @@ public class Map { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww" }; + + public Vector2[] ghostSpawns; + public String ghostSpawnChars = "0123456789"; + public final HashMap mapTypes = new HashMap(){{ this.put("e", "empty"); this.put("s", "empty"); this.put(".", "dot"); this.put("w", "wall"); + this.put("0", "empty"); + this.put("1", "empty"); + this.put("2", "empty"); + this.put("3", "empty"); + this.put("4", "empty"); + this.put("5", "empty"); + this.put("6", "empty"); + this.put("7", "empty"); + this.put("8", "empty"); + this.put("9", "empty"); }}; + public Cell[][] cells; public Vector2 size; @@ -60,29 +75,28 @@ public class Map { } 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); + int sizeY = mapData.length; + int sizeX = mapData[0].length(); - char cellChar = mapData[size.y - 1 - y].charAt(x); - - String cellType = mapTypes.get(String.valueOf(cellChar)); - - cells[x][y] = new Cell(cellPos, cellType, this); - - if(cellChar == playerSpawnChar) { - playerSpawn = cellPos.Clone(); - } - } - } + 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); + + char cellChar = mapData[size.y - 1 - y].charAt(x); + + String cellType = mapTypes.get(String.valueOf(cellChar)); + + cells[x][y] = new Cell(cellPos, cellType, this); + + if(cellChar == playerSpawnChar) { + playerSpawn = cellPos.Clone(); + } + } + } } public Cell GetCell(Vector2 pos) { @@ -96,4 +110,125 @@ public class Map { } } + private void initializeGhostSpawns() { + int count = 0; + for (int y = 0; y < mapClassic.length; y++) { + for (int x = 0; x < mapClassic[y].length(); x++) { + char cellChar = mapClassic[y].charAt(x); + if (ghostSpawnChars.indexOf(cellChar) != -1) { + count++; + } + } + } + ghostSpawns = new Vector2[count]; + count = 0; + for (int y = 0; y < mapClassic.length; y++) { + for (int x = 0; x < mapClassic[y].length(); x++) { + char cellChar = mapClassic[y].charAt(x); + if (ghostSpawnChars.indexOf(cellChar) != -1) { + ghostSpawns[count++] = new Vector2(x, mapClassic.length - 1 - y); + } + } + } + } + + public String FindPath(Vector2 from, Vector2 to) + { + char[][] tempMap = new char[size.x][size.y]; + + for(int x = 0; x < size.x; x++) { + for(int y = 0; y < size.y; y++) { + Vector2 pos = new Vector2(x,y); + Cell cell = GetCell(pos); + if(cell.type.equals("wall")) { + tempMap[x][y] = 'w'; + } + else { + tempMap[x][y] = 'e'; + } + } + } + + tempMap[to.x][to.y] = 't'; + + String targetChars = "tudlr"; + + boolean done = false; + + while(!done) { + int changes = 0; + + for(int x = 0; x < size.x; x++) { + for(int y = 0; y < size.y; y++) { + + if(tempMap[x][y] == 'e') { + + Vector2 pos = new Vector2(x,y); + char newChar = 'x'; + + if(targetChars.indexOf(tempMap[x + 1][y]) >= 0) { + newChar = 'r'; + } + else if(targetChars.indexOf(tempMap[x - 1][y]) >= 0) { + newChar = 'l'; + } + else if(targetChars.indexOf(tempMap[x][y + 1]) >= 0) { + newChar = 'u'; + } + else if(targetChars.indexOf(tempMap[x][y - 1]) >= 0) { + newChar = 'd'; + } + + if(newChar != 'x') { + tempMap[x][y] = newChar; + changes++; + + if(pos.equals(from)) { + done = true; + } + } + } + } + } + + if(changes == 0) { + done = true; + } + } + + if(tempMap[from.x][from.y] != 'e') { + boolean reading = true; + String path = ""; + Vector2 currentReadPos = from.Clone(); + + while(reading) { + char currentChar = tempMap[currentReadPos.x][currentReadPos.y]; + + if(currentReadPos.equals(to)) { + reading = false; + } + else { + path += currentChar; + + if(currentChar == 'l') { + currentReadPos = currentReadPos.Add(new Vector2(-1, 0)); + } + else if(currentChar == 'r') { + currentReadPos = currentReadPos.Add(new Vector2(1, 0)); + } + else if(currentChar == 'u') { + currentReadPos = currentReadPos.Add(new Vector2(0, 1)); + } + else if(currentChar == 'd') { + currentReadPos = currentReadPos.Add(new Vector2(0, -1)); + } + } + } + return path; + } + else { + //no path was found + return ""; + } + } } diff --git a/src/main/java/pacmanGame/Player.java b/src/main/java/pacmanGame/Player.java index b729254..0780614 100644 --- a/src/main/java/pacmanGame/Player.java +++ b/src/main/java/pacmanGame/Player.java @@ -4,6 +4,8 @@ public class Player { public Vector2 position; public Vector2 direction; public GameManager gameManager; + public Vector2 oldDirection; + public Vector2 futureDirection; public Player(GameManager gameManager) { this.gameManager = gameManager; @@ -14,23 +16,28 @@ public class Player { public void Spawn() { position = gameManager.map.playerSpawn; direction = new Vector2(0, -1); + oldDirection = direction; } public void Move() { Vector2 newPosition = position.Add(direction); - boolean newPosIsWall = gameManager.map.GetCell(newPosition).type.equals("wall"); if(!newPosIsWall) { position = newPosition; } + else if (oldDirection != direction) { + direction = oldDirection; + Move(); + } gameManager.updatePlayerCell(); } public void processInput(char inputchar) { - + oldDirection = direction; + if(inputchar == 'w') { - direction = new Vector2(0,1); + direction = new Vector2(0,1); } else if(inputchar == 's') { diff --git a/src/test/java/pacmanTests/MapTest.java b/src/test/java/pacmanTests/MapTest.java index f94d0bd..723fa63 100644 --- a/src/test/java/pacmanTests/MapTest.java +++ b/src/test/java/pacmanTests/MapTest.java @@ -21,14 +21,13 @@ class MapTest { String expectedTopLeft = "wall"; String expectedMiddle = "dot"; String expectedBottomRight = "dot"; - // act GameManager gameManager = new GameManager(); + // act gameManager.map = new Map(mapTest, gameManager); - Map testMap = gameManager.map; - - String topLeft = testMap.cells[0][2].type; - String middle = testMap.cells[1][1].type; - String bottomRight = testMap.cells[2][0].type; + + String topLeft = gameManager.map.cells[0][2].type; + String middle = gameManager.map.cells[1][1].type; + String bottomRight = gameManager.map.cells[2][0].type; // assert assertThat(expectedTopLeft).isEqualTo(topLeft); assertThat(expectedMiddle).isEqualTo(middle); @@ -45,7 +44,7 @@ class MapTest { }; GameManager gameManager = new GameManager(); gameManager.map = new Map(mapTest, gameManager); - Map testMap = gameManager.map; + Map testMap = gameManager.map; Cell expectedTopLeft = testMap.cells[0][2]; Cell expectedMiddle = testMap.cells[1][1]; Cell expectedBottomRight = testMap.cells[2][0]; @@ -58,4 +57,58 @@ class MapTest { assertThat(expectedMiddle).isEqualTo(middle); assertThat(expectedBottomRight).isEqualTo(bottomRight); } + + @Test + void Map_getPath_returnCorrectPathSimple() { + // arrange + String[] mapTest = { + "wwwww", + "w...w", + "w.w.w", + "w.w.w", + "wwwww" + }; + GameManager gameManager = new GameManager(); + gameManager.map = new Map(mapTest, gameManager); + + Vector2 from = new Vector2(1, 1); + Vector2 to = new Vector2(3, 1); + + String expectedPath = "uurrdd"; + + // act + + String path = gameManager.map.FindPath(from, to); + + // assert + assertThat(path).isEqualTo(expectedPath); + } + + @Test + void Map_getPath_returnCorrectPathComplex() { + // arrange + String[] mapTest = { + "wwwwwwwww", + "w.....w.w", + "w.www.w.w", + "w.w...w.w", + "w.w.www.w", + "w.w.....w", + "wwwwwwwww" + }; + GameManager gameManager = new GameManager(); + gameManager.map = new Map(mapTest, gameManager); + + Vector2 from = new Vector2(4, 3); + Vector2 to = new Vector2(1, 1); + + String expectedPath = "ruulllldddd"; + + // act + + String path = gameManager.map.FindPath(from, to); + + // assert + assertThat(path).isEqualTo(expectedPath); + } } diff --git a/team.md b/team.md new file mode 100644 index 0000000..6346c38 --- /dev/null +++ b/team.md @@ -0,0 +1,5 @@ +- fdai2751, fdai2751 +- fdai7012, fdai7012 +- fdai7753, fdai7753 +- fdai7910, fdai7910 +- Julian, fdai7012 \ No newline at end of file