Browse Source

Refactored LogicTest: No longer useful with general game loop

Lorenz Hohmann 3 years ago
parent
commit
26135f4d3a
  1. 17
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java
  2. 24
      src/test/java/de/tims/fleetstorm/gui/LogicTest.java

17
src/main/java/de/tims/fleetstorm/gui/GameLogic.java

@ -204,8 +204,8 @@ public class GameLogic extends JPanel {
boolean shotSuccess = enemyMatchfield.getField(coordX, coordY).shoot();
gui.updateField(playerMove, enemyMatchfield);
if (!shotSuccess)
nextMove();
// if (!shotSuccess)
// nextMove();
}
});
}
@ -327,7 +327,6 @@ public class GameLogic extends JPanel {
}
public void start() {
this.playerMove = true;
this.gameState = GameLogic.PREPARATION;
// create player matchfield
@ -359,11 +358,11 @@ public class GameLogic extends JPanel {
// enter game loop
this.gameState = GameLogic.RUNNING;
this.nextMove();
this.nextMove(true);
}
public void nextMove() {
this.playerMove = !this.playerMove;
public void nextMove(boolean playerMove) {
this.playerMove = playerMove;
if (this.playerMove) {
gui.updateField(this.playerMove, this.enemyMatchfield);
@ -387,7 +386,7 @@ public class GameLogic extends JPanel {
e.printStackTrace();
}
this.nextMove();
// this.nextMove();
}
}
@ -399,4 +398,8 @@ public class GameLogic extends JPanel {
public boolean isPlayerMove() {
return playerMove;
}
public static void setGui(GameLogic gui) {
GameLogic.gui = gui;
}
}

24
src/test/java/de/tims/fleetstorm/gui/LogicTest.java

@ -1,45 +1,27 @@
package de.tims.fleetstorm.gui;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import de.tims.fleetstorm.gui.GameLogic;
class LogicTest {
GameLogic guiLogic = new GameLogic(0, 0, 0);
@BeforeEach
void setup() {
GameLogic.setGui(new GameLogic(0, 0, 0));
guiLogic.start();
}
@Test
void testIfGameStateIsPreparationAfterStart() {
int expectedState = GameLogic.PREPARATION;
void testIfGameStateIsRunningAfterStart() {
int expectedState = GameLogic.RUNNING;
int calculatedState = guiLogic.getGameState();
assertEquals(expectedState, calculatedState);
}
@Test
void testNextMoveIsNotPlayer() {
// 20 tries => every even move is a player move
for (int i = 0; i < 20; i++) {
guiLogic.nextMove();
boolean calculatedResult = guiLogic.isPlayerMove();
if (i % 2 == 0) {
assertFalse(calculatedResult);
} else {
assertTrue(calculatedResult);
}
}
}
}
Loading…
Cancel
Save