From 4d2044374e00404c4007964a35c6453bf38596db Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 Feb 2024 15:58:18 +0100 Subject: [PATCH 1/4] Added FindPath Function to Map --- src/main/java/pacmanGame/Map.java | 100 ++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index b018895..c610ea3 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -128,4 +128,104 @@ public class Map { } } } + + 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 ""; + } + } } From 4d86a65233726797a45bfc8978cbf99cde49989b Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 Feb 2024 15:58:39 +0100 Subject: [PATCH 2/4] Added tests for FindPath function --- src/test/java/pacmanTests/MapTest.java | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/test/java/pacmanTests/MapTest.java b/src/test/java/pacmanTests/MapTest.java index 7fc442d..723fa63 100644 --- a/src/test/java/pacmanTests/MapTest.java +++ b/src/test/java/pacmanTests/MapTest.java @@ -57,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); + } } From 5d46dd98513e3a1e258a7af9deadcf5395febd86 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 Feb 2024 16:01:48 +0100 Subject: [PATCH 3/4] Added teams.md --- team.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 team.md diff --git a/team.md b/team.md new file mode 100644 index 0000000..ea57d50 --- /dev/null +++ b/team.md @@ -0,0 +1,4 @@ +- fdai2751, fdai2751 +- fdai7012, fdai7012 +- fdai7753, fdai7753 +- fdai7910, fdai7910 \ No newline at end of file From a34dcbd689cc212fc780621ff48ad9fb8bb8b4d9 Mon Sep 17 00:00:00 2001 From: fdai7012 Date: Wed, 7 Feb 2024 16:04:54 +0100 Subject: [PATCH 4/4] edited team.md --- team.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/team.md b/team.md index ea57d50..6346c38 100644 --- a/team.md +++ b/team.md @@ -1,4 +1,5 @@ - fdai2751, fdai2751 - fdai7012, fdai7012 - fdai7753, fdai7753 -- fdai7910, fdai7910 \ No newline at end of file +- fdai7910, fdai7910 +- Julian, fdai7012 \ No newline at end of file