From 7380944cfcce14838aa0e0c4431ed339a7c6ae07 Mon Sep 17 00:00:00 2001 From: fdai7910 Date: Thu, 8 Feb 2024 00:13:03 +0100 Subject: [PATCH] added findCellByString method to Map --- src/main/java/pacmanGame/Map.java | 126 ++++++++++++++---------------- 1 file changed, 59 insertions(+), 67 deletions(-) diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 5e119f9..b9d45d4 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -3,98 +3,90 @@ package pacmanGame; import java.util.HashMap; public class Map { - + public final GameManager gameManager; - public static String[] mapClassic = { - "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", - "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", - "ww............ww............ww", - "ww.wwww.wwwww.ww.wwwww.wwww.ww", - "wwpwwww.wwwww.ww.wwwww.wwwwpww", - "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", - "wwp..ww.......s........ww..pww", - "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 mapTypes = new HashMap(){{ - this.put("e", "empty"); - this.put("s", "empty"); - this.put(".", "dot"); - this.put("w", "wall"); - this.put("p", "pill"); - }}; - + public static String[] mapClassic = { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", + "ww............ww............ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww", "wwpwwww.wwwww.ww.wwwww.wwwwpww", + "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", "Teeeeee.eeeweeeeeeweee.eeeeeet", "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", "wwp..ww.......s........ww..pww", "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 mapTypes = new HashMap() { + { + this.put("e", "empty"); + this.put("s", "empty"); + this.put(".", "dot"); + this.put("w", "wall"); + this.put("p", "pill"); + this.put("T", "tpLeft"); + this.put("t", "tpRight"); + } + }; + public Cell[][] cells; public Vector2 size; - - public Vector2 playerSpawn = new Vector2(2,2); + + public Vector2 playerSpawn = new Vector2(2, 2); public char playerSpawnChar = 's'; - + public Map(String[] mapData, GameManager gameManager) { GenerateMap(mapData); this.gameManager = gameManager; } 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); + + 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) { + + if (cellChar == playerSpawnChar) { playerSpawn = cellPos.Clone(); } - } + } } } public Cell GetCell(Vector2 pos) { - if(pos.isWithin(size)) { - - return cells[pos.x][pos.y]; + if (pos.isWithin(size)) { + + return cells[pos.x][pos.y]; + } else { + + return cells[0][0]; } - else { - - return cells[0][0]; + + } + + public Cell findCellByString(String target) { + for (int x = 0; x < size.x; x++) { + for (int y = 0; y < size.y; y++) { + if (cells[x][y].type == target) { + return cells[x][y]; + } + } } - + + return null; } }