Browse Source

United AI with GameLogic

Lorenz Hohmann 3 years ago
parent
commit
879f2c6221
  1. 17
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java
  2. 6
      src/test/java/de/tims/fleetstorm/gui/LogicTest.java

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

@ -12,15 +12,18 @@ import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.border.MatteBorder; import javax.swing.border.MatteBorder;
import de.tims.fleetstorm.ai.Logic;
import de.tims.fleetstorm.matchfield.Matchfield; import de.tims.fleetstorm.matchfield.Matchfield;
public class Logic extends JPanel {
public class GameLogic extends JPanel {
// GameManager stuff // GameManager stuff
private int gameState; private int gameState;
private Matchfield matchfield; private Matchfield matchfield;
private Matchfield enemyMatchfield;
private int matchfieldSize = 10; private int matchfieldSize = 10;
private boolean playerMove = true; private boolean playerMove = true;
private Logic aiLogic;
public static final int PREPARATION = 1; public static final int PREPARATION = 1;
public static final int RUNNING = 2; public static final int RUNNING = 2;
@ -39,7 +42,7 @@ public class Logic extends JPanel {
private JLabel enemy4Ship; private JLabel enemy4Ship;
private JLabel enemy5Ship; private JLabel enemy5Ship;
public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) {
public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) {
this.fields = new ArrayList<>(); this.fields = new ArrayList<>();
setSize(640, 480); setSize(640, 480);
@ -248,7 +251,7 @@ public class Logic extends JPanel {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
JFrame frame = new JFrame("Test GUI"); JFrame frame = new JFrame("Test GUI");
Logic gui = new Logic(15, 28, 1);
GameLogic gui = new GameLogic(15, 28, 1);
frame.setContentPane(gui); frame.setContentPane(gui);
frame.setSize(640, 480); frame.setSize(640, 480);
frame.setResizable(true); frame.setResizable(true);
@ -256,9 +259,15 @@ public class Logic extends JPanel {
} }
public void start() { public void start() {
this.gameState = Logic.PREPARATION;
this.gameState = GameLogic.PREPARATION;
// create player matchfield
this.matchfield = new Matchfield(matchfieldSize); this.matchfield = new Matchfield(matchfieldSize);
// create enemy matchfield and initialize AI (with player's matchfield)
this.enemyMatchfield = new Matchfield(matchfieldSize);
this.aiLogic = new Logic();
this.aiLogic.setMatchfield(this.matchfield);
} }
public void nextMove() { public void nextMove() {

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

@ -7,11 +7,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import de.tims.fleetstorm.gui.Logic;
import de.tims.fleetstorm.gui.GameLogic;
class LogicTest { class LogicTest {
Logic guiLogic = new Logic(0, 0, 0);
GameLogic guiLogic = new GameLogic(0, 0, 0);
@BeforeEach @BeforeEach
void setup() { void setup() {
@ -20,7 +20,7 @@ class LogicTest {
@Test @Test
void testIfGameStateIsPreparationAfterStart() { void testIfGameStateIsPreparationAfterStart() {
int expectedState = Logic.PREPARATION;
int expectedState = GameLogic.PREPARATION;
int calculatedState = guiLogic.getGameState(); int calculatedState = guiLogic.getGameState();

Loading…
Cancel
Save