From 9c01f0b368e87b2d4ea371515e1846bad7e035c7 Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 19:46:15 +0100 Subject: [PATCH 1/7] added score --- src/main/java/pacmanGame/GameManager.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 8a154d5..d269424 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -6,6 +6,7 @@ public class GameManager { public Map map; public Visualizer visualizer; public Player player; + public int score = 0; public GameManager() { setupGame(); @@ -64,4 +65,6 @@ public class GameManager { } return false; } + + } From d1ccd84e74771474a7cdadb2e23a4950b2896995 Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 19:50:45 +0100 Subject: [PATCH 2/7] added triggerItem function to cell --- src/main/java/pacmanGame/Cell.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/pacmanGame/Cell.java b/src/main/java/pacmanGame/Cell.java index 53105af..f01761e 100644 --- a/src/main/java/pacmanGame/Cell.java +++ b/src/main/java/pacmanGame/Cell.java @@ -8,4 +8,32 @@ public class Cell { this.pos = pos; this.type = type; } + + public void triggerDot() { + //todo: code + } + + public void triggerPill() { + //todo: code + } + + public void triggerFruit() { + //todo: code + } + + public void triggerItem() { + if(type.equals("dot")) { + triggerDot(); + } + else if(type.equals("pill")) { + triggerPill(); + } + else if(type.equals("cherry")) { + triggerFruit(); + } + else { + System.out.println("cell contains no item!"); + } + } + } From 8823cd5128d06af482d1d37c05c3c7ca74f2445f Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 20:06:15 +0100 Subject: [PATCH 3/7] refactoring: removed one constructor in Map --- src/main/java/pacmanGame/GameManager.java | 2 +- src/main/java/pacmanGame/Map.java | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index d269424..2d5ae52 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -13,7 +13,7 @@ public class GameManager { } public void setupGame(){ - map = new Map(); + map = new Map(Map.mapClassic); visualizer = new VisualizerPlainText(this); player = new Player(this); player.Spawn(); diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 8e9371f..7f5e7d4 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -3,7 +3,8 @@ package pacmanGame; import java.util.HashMap; public class Map { - public final String[] mapClassic = { + + public static String[] mapClassic = { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "ww............ww............ww", @@ -52,10 +53,6 @@ public class Map { public Vector2 playerSpawn = new Vector2(2,2); public char playerSpawnChar = 's'; - public Map() { - GenerateMap(mapClassic); - } - public Map(String[] mapData) { GenerateMap(mapData); } From d93047ffac4670ed5ee5a45f1cffce8f300e402f Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 20:19:38 +0100 Subject: [PATCH 4/7] added reference from Map to GameManager --- src/main/java/pacmanGame/GameManager.java | 2 +- src/main/java/pacmanGame/Map.java | 4 +++- src/test/java/pacmanTests/MapTest.java | 10 +++++++--- src/test/java/pacmanTests/VisualizerPlainTextTest.java | 4 ++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 2d5ae52..c33d0ac 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -13,7 +13,7 @@ public class GameManager { } public void setupGame(){ - map = new Map(Map.mapClassic); + map = new Map(Map.mapClassic,this); visualizer = new VisualizerPlainText(this); player = new Player(this); player.Spawn(); diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 7f5e7d4..ce72d36 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -4,6 +4,7 @@ import java.util.HashMap; public class Map { + public final GameManager gameManager; public static String[] mapClassic = { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", @@ -53,8 +54,9 @@ public class Map { public Vector2 playerSpawn = new Vector2(2,2); public char playerSpawnChar = 's'; - public Map(String[] mapData) { + public Map(String[] mapData, GameManager gameManager) { GenerateMap(mapData); + this.gameManager = gameManager; } public void GenerateMap(String[] mapData) { diff --git a/src/test/java/pacmanTests/MapTest.java b/src/test/java/pacmanTests/MapTest.java index cc94256..f94d0bd 100644 --- a/src/test/java/pacmanTests/MapTest.java +++ b/src/test/java/pacmanTests/MapTest.java @@ -22,8 +22,10 @@ class MapTest { String expectedMiddle = "dot"; String expectedBottomRight = "dot"; // act - Map testMap = new Map(mapTest); - + GameManager gameManager = new GameManager(); + 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; @@ -41,7 +43,9 @@ class MapTest { "w..", "ee." }; - Map testMap = new Map(mapTest); + GameManager gameManager = new GameManager(); + 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); From 1e5d36d12c4bf5100211da9ead8f74ab425378cd Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 20:21:57 +0100 Subject: [PATCH 5/7] added reference from Cell to Map --- src/main/java/pacmanGame/Cell.java | 4 +++- src/main/java/pacmanGame/Map.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/pacmanGame/Cell.java b/src/main/java/pacmanGame/Cell.java index f01761e..46c71f2 100644 --- a/src/main/java/pacmanGame/Cell.java +++ b/src/main/java/pacmanGame/Cell.java @@ -3,10 +3,12 @@ package pacmanGame; public class Cell { public Vector2 pos; public String type; + public final Map map; - public Cell(Vector2 pos, String type) { + public Cell(Vector2 pos, String type, Map map) { this.pos = pos; this.type = type; + this.map = map; } public void triggerDot() { diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index ce72d36..40ab6aa 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -76,7 +76,7 @@ public class Map { String cellType = mapTypes.get(String.valueOf(cellChar)); - cells[x][y] = new Cell(cellPos, cellType); + cells[x][y] = new Cell(cellPos, cellType, this); if(cellChar == playerSpawnChar) { playerSpawn = cellPos.Clone(); From 376742e0c99cf9844883590d3b2c7940fa136e59 Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 20:24:30 +0100 Subject: [PATCH 6/7] added functionality to triggerDot function --- src/main/java/pacmanGame/Cell.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/pacmanGame/Cell.java b/src/main/java/pacmanGame/Cell.java index 46c71f2..808cde1 100644 --- a/src/main/java/pacmanGame/Cell.java +++ b/src/main/java/pacmanGame/Cell.java @@ -12,7 +12,8 @@ public class Cell { } public void triggerDot() { - //todo: code + this.type = "empty"; + map.gameManager.score += 10; } public void triggerPill() { From 20d88f3075f74d269b276fedb3884e00d8068e2d Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Sat, 3 Feb 2024 20:37:03 +0100 Subject: [PATCH 7/7] refactoring: formating of GameManager --- src/main/java/pacmanGame/GameManager.java | 18 +++++------------- src/test/java/pacmanTests/GameManagerTest.java | 4 ++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index c33d0ac..818d0ed 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -20,9 +20,9 @@ public class GameManager { ghosts = new Ghost[4]; for(int i = 0; i < ghosts.length; i++) { ghosts[i] = new Ghost(this); - } - + } } + public void Update() { visualizer.Update(); if(time%5 == 0) { @@ -31,23 +31,19 @@ public class GameManager { time++; } - public void ProcessInput(char inputChar) { if(inputChar == 27) { //todo: escape key pauses game } - else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a') { player.processInput(inputChar); } - - else { System.out.println("Unprocessed Input: " + inputChar + " (" + (int)inputChar + ")"); } } - public boolean GostPlayerColisionTest() - { + + public boolean GhostPlayerColisionTest() { for(int i = 0; i < ghosts.length; i++) { @@ -57,14 +53,10 @@ public class GameManager { { return true; } - - } - + } } } return false; } - - } diff --git a/src/test/java/pacmanTests/GameManagerTest.java b/src/test/java/pacmanTests/GameManagerTest.java index 9495c45..175852b 100644 --- a/src/test/java/pacmanTests/GameManagerTest.java +++ b/src/test/java/pacmanTests/GameManagerTest.java @@ -54,7 +54,7 @@ class GameManagerTest { GameManager gameManager = new GameManager(); gameManager.player.position = gameManager.ghosts[0].position.Clone(); //act - boolean colision = gameManager.GostPlayerColisionTest(); + boolean colision = gameManager.GhostPlayerColisionTest(); boolean expected = true; // assert @@ -69,7 +69,7 @@ class GameManagerTest { gameManager.player.position = new Vector2(2,3); gameManager.ghosts[0].position = new Vector2(4,5); //act - boolean colision = gameManager.GostPlayerColisionTest(); + boolean colision = gameManager.GhostPlayerColisionTest(); boolean expected = false; // assert