diff --git a/src/main/java/de/tims/fleetstorm/gui/Logic.java b/src/main/java/de/tims/fleetstorm/gui/Logic.java new file mode 100644 index 0000000..a014d0b --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/gui/Logic.java @@ -0,0 +1,275 @@ +package de.tims.fleetstorm.gui; + +import java.awt.Color; +import java.awt.Font; +import java.awt.GridLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.MatteBorder; + +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 fields; + private JLabel matchfieldValue; + private JLabel moveValue; + private JLabel own2Ship; + private JLabel own3Ship; + private JLabel own4Ship; + private JLabel own5Ship; + private JLabel enemy2Ship; + private JLabel enemy3Ship; + private JLabel enemy4Ship; + private JLabel enemy5Ship; + + public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { + this.fields = new ArrayList<>(); + setSize(640, 480); + + JPanel fieldWrapper = new JPanel(); + fieldWrapper.setBounds(10, 11, 362, 458); + fieldWrapper.setLayout(null); + + JPanel infoWrapper = new JPanel(); + infoWrapper.setBounds(382, 11, 248, 458); + infoWrapper.setLayout(null); + setLayout(null); + add(fieldWrapper); + add(infoWrapper); + + JPanel yourFleet = new JPanel(); + yourFleet.setBounds(10, 110, 228, 93); + infoWrapper.add(yourFleet); + yourFleet.setLayout(null); + + JPanel ownFleetHeadingPanel = new JPanel(); + ownFleetHeadingPanel.setBounds(0, 0, 228, 27); + yourFleet.add(ownFleetHeadingPanel); + ownFleetHeadingPanel.setLayout(null); + + JLabel ownFleetHeading = new JLabel("Deine Flotte:"); + ownFleetHeading.setBounds(0, 0, 228, 17); + ownFleetHeadingPanel.add(ownFleetHeading); + ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + + JPanel ownFleetContentPanel = new JPanel(); + ownFleetContentPanel.setBounds(0, 26, 228, 67); + yourFleet.add(ownFleetContentPanel); + ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel own2ShipLabel = new JLabel("1x2 Schiff"); + ownFleetContentPanel.add(own2ShipLabel); + + own2Ship = new JLabel("OK"); + ownFleetContentPanel.add(own2Ship); + + JLabel own3ShipLabel = new JLabel("1x3 Schiff"); + ownFleetContentPanel.add(own3ShipLabel); + + own3Ship = new JLabel("OK"); + ownFleetContentPanel.add(own3Ship); + + JLabel own4ShipLabel = new JLabel("1x4 Schiff"); + ownFleetContentPanel.add(own4ShipLabel); + + own4Ship = new JLabel("OK"); + ownFleetContentPanel.add(own4Ship); + + JLabel own5ShipLabel = new JLabel("1x5 Schiff"); + ownFleetContentPanel.add(own5ShipLabel); + + own5Ship = new JLabel("OK"); + ownFleetContentPanel.add(own5Ship); + + JPanel enemyFleet = new JPanel(); + enemyFleet.setLayout(null); + enemyFleet.setBounds(10, 237, 228, 93); + infoWrapper.add(enemyFleet); + + JPanel enemyFleetHeadingPanel = new JPanel(); + enemyFleetHeadingPanel.setLayout(null); + enemyFleetHeadingPanel.setBounds(0, 0, 228, 27); + enemyFleet.add(enemyFleetHeadingPanel); + + JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); + enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + enemyFleetHeading.setBounds(0, 0, 228, 17); + enemyFleetHeadingPanel.add(enemyFleetHeading); + + JPanel enemyFleetContentPanel = new JPanel(); + enemyFleetContentPanel.setBounds(0, 26, 228, 67); + enemyFleet.add(enemyFleetContentPanel); + enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); + enemyFleetContentPanel.add(enemy2ShipLabel); + + enemy2Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy2Ship); + + JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); + enemyFleetContentPanel.add(enemy3ShipLabel); + + enemy3Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy3Ship); + + JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); + enemyFleetContentPanel.add(enemy4ShipLabel); + + enemy4Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy4Ship); + + JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); + enemyFleetContentPanel.add(enemy5ShipLabel); + + enemy5Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy5Ship); + + JPanel infos = new JPanel(); + infos.setLayout(null); + infos.setBounds(10, 11, 228, 68); + infoWrapper.add(infos); + + JPanel gameInfoHeadingPanel = new JPanel(); + gameInfoHeadingPanel.setLayout(null); + gameInfoHeadingPanel.setBounds(0, 0, 228, 27); + infos.add(gameInfoHeadingPanel); + + JLabel gameInfoHeading = new JLabel("Spielinfos"); + gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + gameInfoHeading.setBounds(0, 0, 228, 17); + gameInfoHeadingPanel.add(gameInfoHeading); + + JPanel gameInfoContentPanel = new JPanel(); + gameInfoContentPanel.setBounds(0, 26, 228, 31); + infos.add(gameInfoContentPanel); + gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel matchfieldLabel = new JLabel("Spielfeld"); + gameInfoContentPanel.add(matchfieldLabel); + + matchfieldValue = new JLabel("Eigenes"); + gameInfoContentPanel.add(matchfieldValue); + + JLabel moveLabel = new JLabel("Am Zug"); + gameInfoContentPanel.add(moveLabel); + + moveValue = new JLabel("Du"); + gameInfoContentPanel.add(moveValue); + + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + JPanel field = new JPanel(); + + int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween); + int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween); + + field.setBounds(xPos, yPos, fieldWidth, fieldWidth); + field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); + fields.add(field); + fieldWrapper.add(field); + + field.addMouseListener(new MouseAdapter() { + + public void mouseClicked(MouseEvent e) { + field.setBackground(Color.RED); + field.revalidate(); + field.repaint(); + } + }); + } + } + + } + + public ArrayList getFields() { + return fields; + } + + public JLabel getMatchfieldValue() { + return matchfieldValue; + } + + public JLabel getMoveValue() { + return moveValue; + } + + public JLabel getOwn2Ship() { + return own2Ship; + } + + public JLabel getOwn3Ship() { + return own3Ship; + } + + public JLabel getOwn4Ship() { + return own4Ship; + } + + public JLabel getOwn5Ship() { + return own5Ship; + } + + public JLabel getEnemy2Ship() { + return enemy2Ship; + } + + public JLabel getEnemy3Ship() { + return enemy3Ship; + } + + public JLabel getEnemy4Ship() { + return enemy4Ship; + } + + public JLabel getEnemy5Ship() { + return enemy5Ship; + } + + /** + * This function is only for testing + */ + public static void main(String[] args) { + JFrame frame = new JFrame("Test GUI"); + 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; + } +} diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java new file mode 100644 index 0000000..5d5e47c --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -0,0 +1,45 @@ +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.Logic; + +class LogicTest { + + Logic guiLogic = new Logic(0, 0, 0); + + @BeforeEach + void setup() { + guiLogic.start(); + } + + @Test + void testIfGameStateIsPreparationAfterStart() { + int expectedState = Logic.PREPARATION; + + 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); + } + } + } + +} diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 7a6d218..5bda4fe 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -1,3 +1,4 @@ + package de.tims.fleetstorm.matchfield; import static org.assertj.core.api.Assertions.assertThat;