Browse Source

Added nextMove() function to check wheter the player or AI has move

Lorenz Hohmann 3 years ago
parent
commit
7f53ab77af
  1. 15
      src/main/java/de/tims/fleetstorm/GameManager.java
  2. 24
      src/test/java/de/tims/fleetstorm/GameManagerTest.java

15
src/main/java/de/tims/fleetstorm/GameManager.java

@ -1,8 +1,13 @@
package de.tims.fleetstorm; package de.tims.fleetstorm;
import de.tims.fleetstorm.matchfield.Matchfield;
public class GameManager { public class GameManager {
private int gameState; private int gameState;
private Matchfield matchfield;
private int matchfieldSize = 10;
private boolean playerMove = true;
public static final int PREPARATION = 1; public static final int PREPARATION = 1;
public static final int RUNNING = 2; public static final int RUNNING = 2;
@ -10,10 +15,20 @@ public class GameManager {
public void start() { public void start() {
this.gameState = GameManager.PREPARATION; this.gameState = GameManager.PREPARATION;
this.matchfield = new Matchfield(matchfieldSize);
}
public void nextMove() {
this.playerMove = !this.playerMove;
} }
public int getGameState() { public int getGameState() {
return gameState; return gameState;
} }
public boolean isPlayerMove() {
return playerMove;
}
} }

24
src/test/java/de/tims/fleetstorm/GameManagerTest.java

@ -1,16 +1,23 @@
package de.tims.fleetstorm; package de.tims.fleetstorm;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class GameManagerTest { class GameManagerTest {
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
@BeforeEach
void setup() {
gameManager.start();
}
@Test @Test
void testIfGameStateIsPreparationAfterStart() { void testIfGameStateIsPreparationAfterStart() {
gameManager.start();
int expectedState = GameManager.PREPARATION; int expectedState = GameManager.PREPARATION;
int calculatedState = gameManager.getGameState(); int calculatedState = gameManager.getGameState();
@ -18,4 +25,19 @@ class GameManagerTest {
assertEquals(expectedState, calculatedState); assertEquals(expectedState, calculatedState);
} }
@Test
void testNextMoveIsNotPlayer() {
// 20 tries => every even move is a player move
for (int i = 0; i < 20; i++) {
gameManager.nextMove();
boolean calculatedResult = gameManager.isPlayerMove();
if (i % 2 == 0) {
assertFalse(calculatedResult);
} else {
assertTrue(calculatedResult);
}
}
}
} }
Loading…
Cancel
Save