Browse Source

Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7012/pacmayham

remotes/origin/playerRefactoring
fdai2751 11 months ago
parent
commit
031dd459cc
  1. 12
      src/main/java/pacmanGame/GameManager.java
  2. 4
      src/main/java/pacmanGame/Ghost.java
  3. 32
      src/main/java/pacmanGame/Map.java
  4. 9
      src/main/java/pacmanGame/VisualizerPlainText.java
  5. 23
      src/test/java/pacmanTests/GameManagerTest.java
  6. 11
      src/test/java/pacmanTests/GhostTest.java

12
src/main/java/pacmanGame/GameManager.java

@ -9,6 +9,7 @@ public class GameManager {
public int score = 0;
public boolean ghostIsEdible = false;
public boolean isPaused;
public final int ghostCount = 10;
public int livesRemaining;
public int failedAttempts;
@ -19,18 +20,23 @@ public class GameManager {
}
public void setupGame(){
ghosts = new Ghost[ghostCount];
for (int i = 0; i < ghosts.length; i++) {
ghosts[i] = new Ghost(this, i);
}
map = new Map(Map.mapClassic, this);
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, 1);
ghosts[i].spawn();
}
isPaused = false;
}
public void handleGhostCollision() {
livesRemaining--; // Reduziere verbleibende leben um 1
failedAttempts++; // Erhöhe die Anzahl der Fehlversuche
if(livesRemaining <= 0) {
gameOver(); //wenn Keine Leben mehr übrig sind.
@ -76,7 +82,7 @@ public class GameManager {
}
}
public boolean GhostPlayerColisionTest() {
public boolean GhostPlayerCollisionTest() {
for(int i = 0; i < ghosts.length; i++) {

4
src/main/java/pacmanGame/Ghost.java

@ -19,4 +19,8 @@ public class Ghost {
public void move(Vector2 direction) {
this.position = this.position.Add(direction);
}
public void spawn() {
this.position = gameManager.map.ghostSpawns[ghostNumber].Clone();
}
}

32
src/main/java/pacmanGame/Map.java

@ -69,9 +69,10 @@ public class Map {
public Vector2 playerSpawn = new Vector2(2,2);
public char playerSpawnChar = 's';
public Map(String[] mapData, GameManager gameManager) {
GenerateMap(mapData);
this.gameManager = gameManager;
GenerateMap(mapData);
}
public void GenerateMap(String[] mapData) {
@ -82,6 +83,8 @@ public class Map {
cells = new Cell[size.x][size.y];
ghostSpawns = new Vector2[10];
for(int x = 0; x < size.x; x++) {
for(int y = 0; y < size.y; y++) {
Vector2 cellPos = new Vector2(x,y);
@ -92,6 +95,12 @@ public class Map {
cells[x][y] = new Cell(cellPos, cellType, this);
for(int i = 0; i < gameManager.ghosts.length; i++) {
if(cellChar == ghostSpawnChars.charAt(i)) {
ghostSpawns[i] = cellPos.Clone();
}
}
if(cellChar == playerSpawnChar) {
playerSpawn = cellPos.Clone();
}
@ -110,27 +119,6 @@ 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)
{

9
src/main/java/pacmanGame/VisualizerPlainText.java

@ -15,6 +15,15 @@ public class VisualizerPlainText implements Visualizer {
}};
public final String ghostSprite = "AA";
public final String ghostSprite1 = "BB";
public final String ghostSprite2 = "CC";
public final String ghostSprite3 = "DD";
public final String ghostSprite4 = "EE";
public final String ghostSprite5 = "FF";
public final String ghostSprite6 = "GG";
public final String ghostSprite7 = "HH";
public final String ghostSprite8 = "II";
public final String ghostSprite9 = "JJ";
public final String playerSprite = "()";
public VisualizerPlainText(GameManager gameManager) {

23
src/test/java/pacmanTests/GameManagerTest.java

@ -54,14 +54,14 @@ class GameManagerTest {
GameManager gameManager = new GameManager();
gameManager.player.position = gameManager.ghosts[0].position.Clone();
//act
boolean colision = gameManager.GhostPlayerColisionTest();
boolean collision = gameManager.GhostPlayerCollisionTest();
boolean expected = true;
// assert
assertThat(colision).isEqualTo(expected);
assertThat(collision).isEqualTo(expected);
}
@Test
void GameManager_GostPlayerColisionTest_DoesntDetectColosion() {
void GameManager_GostPlayerCollisionTest_DoesntDetectColliosion() {
// arrange
@ -69,11 +69,24 @@ class GameManagerTest {
gameManager.player.position = new Vector2(2,3);
gameManager.ghosts[0].position = new Vector2(4,5);
//act
boolean colision = gameManager.GhostPlayerColisionTest();
boolean collision = gameManager.GhostPlayerCollisionTest();
boolean expected = false;
// assert
assertThat(colision).isEqualTo(expected);
assertThat(collision).isEqualTo(expected);
}
@Test
void GameManager_setupgame_spawnsghosts() {
// arrange
GameManager gameManager = new GameManager();
gameManager.map.ghostSpawns[0] = new Vector2(1, 1);
Vector2 expectedPosition = new Vector2(1, 1);
// act
gameManager.ghosts[0].spawn();
Vector2 ghostPosition = gameManager.ghosts[0].position;
// assert
assertThat(ghostPosition).isEqualTo(expectedPosition);
}
@Test

11
src/test/java/pacmanTests/GhostTest.java

@ -0,0 +1,11 @@
package pacmanTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GhostTest {
}
Loading…
Cancel
Save