Browse Source

United GameManager with GUI class (Refactoring)

Lorenz Hohmann 3 years ago
parent
commit
4b71ac31e4
  1. 34
      src/main/java/de/tims/fleetstorm/GameManager.java
  2. 37
      src/main/java/de/tims/fleetstorm/gui/Logic.java
  3. 18
      src/test/java/de/tims/fleetstorm/gui/LogicTest.java

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

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

37
src/main/java/de/tims/fleetstorm/gui/GUI.java → src/main/java/de/tims/fleetstorm/gui/Logic.java

@ -12,8 +12,21 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
public class GUI extends JPanel {
import de.tims.fleetstorm.matchfield.Matchfield;
public class Logic extends JPanel {
// GameManager stuff
private int gameState;
private Matchfield matchfield;
private int matchfieldSize = 10;
private boolean playerMove = true;
public static final int PREPARATION = 1;
public static final int RUNNING = 2;
public static final int OVER = 3;
// GUI stuff
private ArrayList<JPanel> fields;
private JLabel matchfieldValue;
private JLabel moveValue;
@ -26,7 +39,7 @@ public class GUI extends JPanel {
private JLabel enemy4Ship;
private JLabel enemy5Ship;
public GUI(int gapToFrameBorder, int fieldWidth, int spaceBetween) {
public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) {
this.fields = new ArrayList<>();
setSize(640, 480);
@ -235,10 +248,28 @@ public class GUI extends JPanel {
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Test GUI");
GUI gui = new GUI(15, 28, 1);
Logic gui = new Logic(15, 28, 1);
frame.setContentPane(gui);
frame.setSize(640, 480);
frame.setResizable(true);
frame.setVisible(true);
}
public void start() {
this.gameState = Logic.PREPARATION;
this.matchfield = new Matchfield(matchfieldSize);
}
public void nextMove() {
this.playerMove = !this.playerMove;
}
public int getGameState() {
return gameState;
}
public boolean isPlayerMove() {
return playerMove;
}
}

18
src/test/java/de/tims/fleetstorm/GameManagerTest.java → src/test/java/de/tims/fleetstorm/gui/LogicTest.java

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