From c1efe11b684fa3cc66c4960ad150b1a5e5404a6f Mon Sep 17 00:00:00 2001 From: fdai7753 Date: Sun, 4 Feb 2024 19:40:27 +0100 Subject: [PATCH 1/4] Added ghost spawns to Map --- src/main/java/pacmanGame/Map.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 8e9371f..5768e63 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -19,8 +19,8 @@ public class Map { "wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", - "eeeeeee.eeeweeeeeeweee.eeeeeee", - "wwwwwww.wweweeeeeeweww.wwwwwww", + "eeeeeee.eeewe1ee3eweee.eeeeeee", + "wwwwwww.wwewe2ee4eweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww", @@ -38,12 +38,18 @@ public class Map { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww" }; - + + public final int ghostSpawnCount = 4; + public final HashMap mapTypes = new HashMap(){{ this.put("e", "empty"); this.put("s", "empty"); this.put(".", "dot"); this.put("w", "wall"); + + for(int i = 0; i < 10; i++) { + this.put(i+"", "empty"); + } }}; public Cell[][] cells; From 6b78ab00f89a56296fdf233799c7755dfda91a0c Mon Sep 17 00:00:00 2001 From: fdai7753 Date: Sun, 4 Feb 2024 21:00:19 +0100 Subject: [PATCH 2/4] Added ghost number system in Map.java and GameManager.java --- src/main/java/pacmanGame/GameManager.java | 19 ++++++++--------- src/main/java/pacmanGame/Ghost.java | 25 +++++++++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 8a154d5..739359c 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -12,16 +12,15 @@ public class GameManager { } public void setupGame(){ - map = new Map(); - visualizer = new VisualizerPlainText(this); - player = new Player(this); - player.Spawn(); - ghosts = new Ghost[4]; - for(int i = 0; i < ghosts.length; i++) { - ghosts[i] = new Ghost(this); - } - - } + map = new Map(); + visualizer = new VisualizerPlainText(this); + player = new Player(this); + player.Spawn(); + ghosts = new Ghost[4]; + for (int i = 0; i < ghosts.length; i++) { + ghosts[i] = new Ghost(this, i); + } + } public void Update() { visualizer.Update(); if(time%5 == 0) { diff --git a/src/main/java/pacmanGame/Ghost.java b/src/main/java/pacmanGame/Ghost.java index dc8a7d9..9e7f6b3 100644 --- a/src/main/java/pacmanGame/Ghost.java +++ b/src/main/java/pacmanGame/Ghost.java @@ -1,12 +1,21 @@ 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); + } } From 9b2da8a4d6f0e2f13101e44c5ba376b7efdfc31c Mon Sep 17 00:00:00 2001 From: fdai7753 Date: Sun, 4 Feb 2024 21:02:07 +0100 Subject: [PATCH 3/4] Added move directions to ghosts --- src/main/java/pacmanGame/Map.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 5768e63..8468537 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -19,8 +19,8 @@ public class Map { "wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", - "eeeeeee.eeewe1ee3eweee.eeeeeee", - "wwwwwww.wwewe2ee4eweww.wwwwwww", + "eeeeeee.eeewe0ee2eweee.eeeeeee", + "wwwwwww.wwewe1ee3eweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww", @@ -39,8 +39,10 @@ public class Map { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww" }; - public final int ghostSpawnCount = 4; - + public Vector2[] ghostSpawns; + public String ghostSpawnChars = "0123456789"; + + public final HashMap mapTypes = new HashMap(){{ this.put("e", "empty"); this.put("s", "empty"); From c25a45f196d81f0df8a5409b12c608f369d69107 Mon Sep 17 00:00:00 2001 From: fdai7753 Date: Mon, 5 Feb 2024 21:31:39 +0100 Subject: [PATCH 4/4] Fixed ghosts branch crashes --- src/main/java/pacmanGame/GameManager.java | 2 +- src/main/java/pacmanGame/Ghost.java | 1 + src/main/java/pacmanGame/Map.java | 34 ++++++++++++++++--- src/test/java/pacmanTests/MapTest.java | 14 +++++--- .../pacmanTests/VisualizerPlainTextTest.java | 4 +-- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 739359c..d7c22d3 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -12,7 +12,7 @@ public class GameManager { } public void setupGame(){ - map = new Map(); + map = new Map(this); visualizer = new VisualizerPlainText(this); player = new Player(this); player.Spawn(); diff --git a/src/main/java/pacmanGame/Ghost.java b/src/main/java/pacmanGame/Ghost.java index 9e7f6b3..26e9845 100644 --- a/src/main/java/pacmanGame/Ghost.java +++ b/src/main/java/pacmanGame/Ghost.java @@ -4,6 +4,7 @@ public class Ghost { public Vector2 position; public final GameManager gameManager; public final int ghostNumber; + public Ghost(GameManager gameManager, int ghostNumber) { this.gameManager = gameManager; diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 8468537..2379e89 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -41,7 +41,7 @@ public class Map { public Vector2[] ghostSpawns; public String ghostSpawnChars = "0123456789"; - + public final HashMap mapTypes = new HashMap(){{ this.put("e", "empty"); @@ -53,6 +53,7 @@ public class Map { this.put(i+"", "empty"); } }}; + public GameManager gameManager; public Cell[][] cells; public Vector2 size; @@ -60,12 +61,14 @@ public class Map { public Vector2 playerSpawn = new Vector2(2,2); public char playerSpawnChar = 's'; - public Map() { + public Map(GameManager gameManager) { + initializeGhostSpawns(); GenerateMap(mapClassic); + this.gameManager = gameManager; } - - public Map(String[] mapData) { + public Map(String[] mapData,GameManager gameManager) { GenerateMap(mapData); + this.gameManager = gameManager; } public void GenerateMap(String[] mapData) { @@ -87,6 +90,8 @@ public class Map { cells[x][y] = new Cell(cellPos, cellType); + //for(int i = 0; i < gameManager.ghosts.length; i++) + if(cellChar == playerSpawnChar) { playerSpawn = cellPos.Clone(); } @@ -105,4 +110,25 @@ 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); + } + } + } + } } diff --git a/src/test/java/pacmanTests/MapTest.java b/src/test/java/pacmanTests/MapTest.java index cc94256..2f91479 100644 --- a/src/test/java/pacmanTests/MapTest.java +++ b/src/test/java/pacmanTests/MapTest.java @@ -21,12 +21,13 @@ class MapTest { String expectedTopLeft = "wall"; String expectedMiddle = "dot"; String expectedBottomRight = "dot"; + GameManager gameManager = new GameManager(); // act - Map testMap = new Map(mapTest); + gameManager.map = new Map(mapTest, gameManager); - 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); @@ -41,7 +42,10 @@ class MapTest { "w..", "ee." }; - Map testMap = new Map(mapTest); + GameManager gameManager = new GameManager(); + // act + gameManager.map = new Map(mapTest, gameManager); + Map testMap = gameManager.map; Cell expectedTopLeft = testMap.cells[0][2]; Cell expectedMiddle = testMap.cells[1][1]; Cell expectedBottomRight = testMap.cells[2][0]; diff --git a/src/test/java/pacmanTests/VisualizerPlainTextTest.java b/src/test/java/pacmanTests/VisualizerPlainTextTest.java index 92276e2..e73225c 100644 --- a/src/test/java/pacmanTests/VisualizerPlainTextTest.java +++ b/src/test/java/pacmanTests/VisualizerPlainTextTest.java @@ -20,7 +20,7 @@ class VisualizerPlainTextTest { }; GameManager gameManager = new GameManager(); - gameManager.map = new Map(mapTest); + gameManager.map = new Map(mapTest, gameManager); VisualizerPlainText vpt = new VisualizerPlainText(gameManager); String expected = "" @@ -44,7 +44,7 @@ class VisualizerPlainTextTest { }; GameManager gameManager = new GameManager(); - gameManager.map = new Map(mapTest); + gameManager.map = new Map(mapTest, gameManager); VisualizerPlainText vpt = new VisualizerPlainText(gameManager); gameManager.ghosts[0].position = new Vector2(1, 1);