Browse Source

Merge branch 'player'

remotes/origin/playerRefactoring
fdai7910 11 months ago
parent
commit
432e9918d7
  1. 50
      src/main/java/pacmanGame/Cell.java
  2. 43
      src/main/java/pacmanGame/GameManager.java
  3. 32
      src/main/java/pacmanGame/Map.java
  4. 35
      src/main/java/pacmanGame/Player.java
  5. 11
      src/main/java/pacmanGame/VisualizerPlainText.java
  6. 28
      src/main/java/pacmanGame/VisualizerPlainTextUltra.java
  7. 110
      src/test/java/pacmanTests/CellTest.java
  8. 4
      src/test/java/pacmanTests/GameManagerTest.java
  9. 18
      src/test/java/pacmanTests/MapTest.java
  10. 1
      src/test/java/pacmanTests/VisualizerPlainTextTest.java

50
src/main/java/pacmanGame/Cell.java

@ -19,16 +19,36 @@ public class Cell {
public void triggerPill() { public void triggerPill() {
this.type = "empty"; this.type = "empty";
gameManager.score += 100;
int timeStopPillEffect = gameManager.time + 200;
gameManager.ghostIsEdible = true;
map.gameManager.score += 100;
map.gameManager.timeStopPillEffect = map.gameManager.time + 200;
map.gameManager.ghostIsEdible = true;
} }
public void triggerFruit() {
public void triggerCherry() {
this.type = "empty"; this.type = "empty";
map.gameManager.score += 505; map.gameManager.score += 505;
} }
public void triggerStrawberry() {
this.type = "empty";
map.gameManager.score += 405;
}
public void triggerOrange() {
this.type = "empty";
map.gameManager.score += 305;
}
public void triggerApple() {
this.type = "empty";
map.gameManager.score += 205;
}
public void triggerCrystal() {
this.type = "empty";
map.gameManager.score += 506;
map.gameManager.moveSpeed = 3;
}
public void triggerItem() { public void triggerItem() {
if(type.equals("dot")) { if(type.equals("dot")) {
triggerDot(); triggerDot();
@ -37,10 +57,30 @@ public class Cell {
triggerPill(); triggerPill();
} }
else if(type.equals("cherry")) { else if(type.equals("cherry")) {
triggerFruit();
triggerCherry();
}
else if(type.equals("strawberry")) {
triggerStrawberry();
}
else if(type.equals("orange")) {
triggerOrange();
}
else if(type.equals("apple")) {
triggerApple();
}
else if(type.equals("crystal")) {
triggerCrystal();
} }
else if(type.equals("tpLeft")) {
map.gameManager.teleportPlayer("tpRight");
}
else if(type.equals("tpRight")) {
map.gameManager.teleportPlayer("tpLeft");
}
else { else {
//System.out.println("cell contains no item!"); //System.out.println("cell contains no item!");
} }
} }
} }

43
src/main/java/pacmanGame/GameManager.java

@ -12,6 +12,8 @@ public class GameManager {
public final int ghostCount = 10; public final int ghostCount = 10;
public int livesRemaining; public int livesRemaining;
public int failedAttempts; public int failedAttempts;
public int timeStopPillEffect;
public int moveSpeed;
public GameManager() { public GameManager() {
livesRemaining = 3; livesRemaining = 3;
@ -20,6 +22,8 @@ public class GameManager {
} }
public void setupGame(){ public void setupGame(){
time = 0;
moveSpeed = 5;
ghosts = new Ghost[ghostCount]; ghosts = new Ghost[ghostCount];
for (int i = 0; i < ghosts.length; i++) { for (int i = 0; i < ghosts.length; i++) {
ghosts[i] = new Ghost(this, i); ghosts[i] = new Ghost(this, i);
@ -41,40 +45,53 @@ public class GameManager {
if(livesRemaining <= 0) { if(livesRemaining <= 0) {
gameOver(); //wenn Keine Leben mehr brig sind. gameOver(); //wenn Keine Leben mehr brig sind.
} }
} }
public void gameOver() { public void gameOver() {
System.out.println("Das Spiel ist vorbei! Pech!!"); System.out.println("Das Spiel ist vorbei! Pech!!");
} }
public void Update() { public void Update() {
if(!isPaused) {
visualizer.Update(); visualizer.Update();
if(time%5 == 0) {
if(!isPaused) {
if(time%moveSpeed == 0) {
player.Move(); player.Move();
} }
time++;
} }
if(time == 300) { if(time == 300) {
spawnCherry();
spawnFruit();
} }
if (time == 600) { if (time == 600) {
destroyCherry();
destroyFruit();
} }
time++;
if(ghostIsEdible) {
if(time == timeStopPillEffect)
ghostIsEdible = false;
} }
public void spawnCherry() {
map.GetCell(map.playerSpawn).type = "cherry";
} }
public void destroyCherry() {
public void spawnFruit() {
map.GetCell(map.playerSpawn).type = randomFruit();
}
public void destroyFruit() {
map.GetCell(map.playerSpawn).type = "empty"; map.GetCell(map.playerSpawn).type = "empty";
} }
public String randomFruit() {
String fruitTypes[] = {"cherry","strawberry","orange","apple","crystal"};
int randomNumber = (int) (Math. random() * (fruitTypes.length));
return fruitTypes[randomNumber];
}
public void ProcessInput(char inputChar) { public void ProcessInput(char inputChar) {
if(inputChar == 27) {// 27 = ESC if(inputChar == 27) {// 27 = ESC
TogglePause(); TogglePause();
} }
else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a') {
else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a' || inputChar == 'r') {
player.processInput(inputChar); player.processInput(inputChar);
} }
else { else {
@ -82,7 +99,13 @@ public class GameManager {
} }
} }
public boolean GhostPlayerCollisionTest() {
public void teleportPlayer(String destination) {
player.position = map.findCellByString(destination).pos;
}
public boolean GhostPlayerColisionTest() {
for(int i = 0; i < ghosts.length; i++) { for(int i = 0; i < ghosts.length; i++) {

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

@ -10,7 +10,7 @@ public class Map {
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"ww............ww............ww", "ww............ww............ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"wwpwwww.wwwww.ww.wwwww.wwwwpww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww..........................ww", "ww..........................ww",
"ww.wwww.ww.wwwwwwww.ww.wwww.ww", "ww.wwww.ww.wwwwwwww.ww.wwww.ww",
@ -21,7 +21,7 @@ public class Map {
"wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww",
"eeeeeee.eeew804629weee.eeeeeee",
"Teeeeee.eeew804629weee.eeeeeet",
"wwwwwww.wwewe1573eweww.wwwwwww", "wwwwwww.wwewe1573eweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww",
@ -30,7 +30,7 @@ public class Map {
"ww............ww............ww", "ww............ww............ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww...ww.......s........ww...ww",
"wwp..ww.......s........ww..pww",
"wwww.ww.ww.wwwwwwww.ww.ww.wwww", "wwww.ww.ww.wwwwwwww.ww.ww.wwww",
"wwww.ww.ww.wwwwwwww.ww.ww.wwww", "wwww.ww.ww.wwwwwwww.ww.ww.wwww",
"ww......ww....ww....ww......ww", "ww......ww....ww....ww......ww",
@ -50,6 +50,9 @@ public class Map {
this.put("s", "empty"); this.put("s", "empty");
this.put(".", "dot"); this.put(".", "dot");
this.put("w", "wall"); this.put("w", "wall");
this.put("p", "pill");
this.put("T", "tpLeft");
this.put("t", "tpRight");
this.put("0", "empty"); this.put("0", "empty");
this.put("1", "empty"); this.put("1", "empty");
this.put("2", "empty"); this.put("2", "empty");
@ -62,20 +65,19 @@ public class Map {
this.put("9", "empty"); this.put("9", "empty");
}}; }};
public Cell[][] cells; public Cell[][] cells;
public Vector2 size; public Vector2 size;
public Vector2 playerSpawn = new Vector2(2,2);
public Vector2 playerSpawn = new Vector2(2, 2);
public char playerSpawnChar = 's'; public char playerSpawnChar = 's';
public Map(String[] mapData, GameManager gameManager) { public Map(String[] mapData, GameManager gameManager) {
this.gameManager = gameManager; this.gameManager = gameManager;
GenerateMap(mapData); GenerateMap(mapData);
} }
public void GenerateMap(String[] mapData) { public void GenerateMap(String[] mapData) {
int sizeY = mapData.length; int sizeY = mapData.length;
int sizeX = mapData[0].length(); int sizeX = mapData[0].length();
@ -106,20 +108,32 @@ public class Map {
} }
} }
} }
} }
public Cell GetCell(Vector2 pos) { public Cell GetCell(Vector2 pos) {
if(pos.isWithin(size)) {
if (pos.isWithin(size)) {
return cells[pos.x][pos.y]; return cells[pos.x][pos.y];
}
else {
} else {
return cells[0][0]; 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.equals(target)) {
return cells[x][y];
}
}
}
System.out.println("couldnt find cell " + target);
return null;
}
public String FindPath(Vector2 from, Vector2 to) public String FindPath(Vector2 from, Vector2 to)
{ {
char[][] tempMap = new char[size.x][size.y]; char[][] tempMap = new char[size.x][size.y];

35
src/main/java/pacmanGame/Player.java

@ -4,8 +4,7 @@ public class Player {
public Vector2 position; public Vector2 position;
public Vector2 direction; public Vector2 direction;
public GameManager gameManager; public GameManager gameManager;
public Vector2 oldDirection;
public Vector2 futureDirection;
public Vector2 savedDirection;
public Player(GameManager gameManager) { public Player(GameManager gameManager) {
this.gameManager = gameManager; this.gameManager = gameManager;
@ -16,7 +15,6 @@ public class Player {
public void Spawn() { public void Spawn() {
position = gameManager.map.playerSpawn; position = gameManager.map.playerSpawn;
direction = new Vector2(0, -1); direction = new Vector2(0, -1);
oldDirection = direction;
} }
public void Move() { public void Move() {
@ -26,32 +24,47 @@ public class Player {
if(!newPosIsWall) { if(!newPosIsWall) {
position = newPosition; position = newPosition;
} }
else if (oldDirection != direction) {
direction = oldDirection;
Move();
if(savedDirection != null) {
checkInput(savedDirection);
} }
gameManager.updatePlayerCell(); gameManager.updatePlayerCell();
} }
public void processInput(char inputchar) { public void processInput(char inputchar) {
oldDirection = direction;
if(inputchar == 'w') { if(inputchar == 'w') {
direction = new Vector2(0,1);
checkInput(new Vector2(0,1));
} }
else if(inputchar == 's') { else if(inputchar == 's') {
direction = new Vector2(0,-1);
checkInput(new Vector2(0,-1));
} }
else if(inputchar == 'd') { else if(inputchar == 'd') {
direction = new Vector2(1,0);
checkInput(new Vector2(1,0));
} }
else if(inputchar == 'a') { else if(inputchar == 'a') {
direction = new Vector2(-1,0);
checkInput(new Vector2(-1,0));
}
else if(inputchar == 'r') {
gameManager.setupGame();
} }
} }
public void checkInput(Vector2 input) {
Vector2 newPosition = position.Add(input);
boolean newPosIsWall = gameManager.map.GetCell(newPosition).type.equals("wall");
if(!newPosIsWall) {
direction = input;
savedDirection = null;
}
else {
savedDirection = input;
}
}
} }

11
src/main/java/pacmanGame/VisualizerPlainText.java

@ -12,6 +12,13 @@ public class VisualizerPlainText implements Visualizer {
this.put("dot", ". "); this.put("dot", ". ");
this.put("wall", "[]"); this.put("wall", "[]");
this.put("cherry", "OO"); this.put("cherry", "OO");
this.put("strawberry", "T7");
this.put("orange", "CO");
this.put("apple", "@ ");
this.put("crystal", "<>");
this.put("pill", "o ");
this.put("tpRight" , " ");
this.put("tpLeft" , " ");
}}; }};
public final String ghostSprite = "AA"; public final String ghostSprite = "AA";
@ -37,6 +44,7 @@ public class VisualizerPlainText implements Visualizer {
@Override @Override
public void Update() { public void Update() {
output = ""; output = "";
if(showScore) { if(showScore) {
output = "score: " + gameManager.score + "\n"; output = "score: " + gameManager.score + "\n";
@ -88,7 +96,4 @@ public class VisualizerPlainText implements Visualizer {
output += "\n"; output += "\n";
} }
} }
} }

28
src/main/java/pacmanGame/VisualizerPlainTextUltra.java

@ -26,10 +26,38 @@ public class VisualizerPlainTextUltra implements Visualizer {
"|''|", "|''|",
"|__|" "|__|"
}); });
this.put("pill", new String[] {
" O ",
" "
});
this.put("tpLeft", new String[] {
" ",
" "
});
this.put("tpRight", new String[] {
" ",
" "
});
this.put("cherry", new String[] { this.put("cherry", new String[] {
" /\\ ", " /\\ ",
" 0O " " 0O "
}); });
this.put("strawberry", new String[] {
"( )",
" \\_/"
});
this.put("orange", new String[] {
" __ ",
"(__)"
});
this.put("apple", new String[] {
" , ",
" @ "
});
this.put("crystal", new String[] {
" /\\ ",
" \\/ "
});
}}; }};
public final String[][] ghostSprites = { public final String[][] ghostSprites = {

110
src/test/java/pacmanTests/CellTest.java

@ -0,0 +1,110 @@
package pacmanTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import pacmanGame.*;
class CellTest {
@Test
void cell_triggerItem_505cherryScore() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "cherry";
int expectedScore = 505;
// act
cell.triggerItem();
int resultingScore = gameManager.score;
// assert
assertThat(expectedScore).isEqualTo(resultingScore);
}
@Test
void cell_triggerItem_10dotScore() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "dot";
int expectedScore = 10;
// act
cell.triggerItem();
int resultingScore = gameManager.score;
// assert
assertThat(expectedScore).isEqualTo(resultingScore);
}
@Test
void cell_triggerItem_100pillScore() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "pill";
int expectedScore = 100;
// act
cell.triggerItem();
int resultingScore = gameManager.score;
// assert
assertThat(expectedScore).isEqualTo(resultingScore);
}
@Test
void cell_triggerItem_405strawberryScore() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "strawberry";
int expectedScore = 405;
// act
cell.triggerItem();
int resultingScore = gameManager.score;
// assert
assertThat(expectedScore).isEqualTo(resultingScore);
}
@Test
void cell_triggerItem_305orangeScore() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "orange";
int expectedScore = 305;
// act
cell.triggerItem();
int resultingScore = gameManager.score;
// assert
assertThat(expectedScore).isEqualTo(resultingScore);
}
@Test
void cell_triggerItem_205appleScore() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "apple";
int expectedScore = 205;
// act
cell.triggerItem();
int resultingScore = gameManager.score;
// assert
assertThat(expectedScore).isEqualTo(resultingScore);
}
@Test
void cell_triggerItem_3crystalMoveSpeed() {
// arrange
GameManager gameManager = new GameManager();
Cell cell = gameManager.map.GetCell(new Vector2(0,0));
cell.type = "crystal";
int expectedMoveSpeed = 3;
// act
cell.triggerItem();
int resultingMoveSpeed = gameManager.moveSpeed;
// assert
assertThat(expectedMoveSpeed).isEqualTo(resultingMoveSpeed);
}
}

4
src/test/java/pacmanTests/GameManagerTest.java

@ -55,7 +55,7 @@ class GameManagerTest {
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
gameManager.player.position = gameManager.ghosts[0].position.Clone(); gameManager.player.position = gameManager.ghosts[0].position.Clone();
//act //act
boolean collision = gameManager.GhostPlayerCollisionTest();
boolean collision = gameManager.GhostPlayerColisionTest();
boolean expected = true; boolean expected = true;
// assert // assert
@ -70,7 +70,7 @@ class GameManagerTest {
gameManager.player.position = new Vector2(2,3); gameManager.player.position = new Vector2(2,3);
gameManager.ghosts[0].position = new Vector2(4,5); gameManager.ghosts[0].position = new Vector2(4,5);
//act //act
boolean collision = gameManager.GhostPlayerCollisionTest();
boolean collision = gameManager.GhostPlayerColisionTest();
boolean expected = false; boolean expected = false;
// assert // assert

18
src/test/java/pacmanTests/MapTest.java

@ -111,4 +111,22 @@ class MapTest {
// assert // assert
assertThat(path).isEqualTo(expectedPath); assertThat(path).isEqualTo(expectedPath);
} }
@Test
void Map_findCellByString_cellT() {
// arrange
String[] mapTest = {
"wew",
"w.t",
"ee."
};
GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest, gameManager);
Map testMap = gameManager.map;
Cell expectedT = testMap.cells[2][1];
// act
Cell foundT = testMap.findCellByString("tpRight");
// assert
assertThat(expectedT).isEqualTo(foundT);
}
} }

1
src/test/java/pacmanTests/VisualizerPlainTextTest.java

@ -23,6 +23,7 @@ class VisualizerPlainTextTest {
gameManager.map = new Map(mapTest, gameManager); gameManager.map = new Map(mapTest, gameManager);
VisualizerPlainText vpt = new VisualizerPlainText(gameManager); VisualizerPlainText vpt = new VisualizerPlainText(gameManager);
vpt.showScore = false; vpt.showScore = false;
String expected = "" String expected = ""
+ "[]. []\n" + "[]. []\n"
+ " . \n" + " . \n"

Loading…
Cancel
Save