From 114692a63f142e80262da46d9c43890ca17e2100 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 14:20:27 +0100 Subject: [PATCH] GUI Refactoring --- .../de/tims/fleetstorm/gui/GameLogic.java | 68 ++++++++----------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 0bf44d3..557ce00 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -35,6 +35,7 @@ public class GameLogic extends JPanel { public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) { this.playerFields = new ArrayList<>(); this.enemyFields = new ArrayList<>(); + setSize(640, 480); JPanel fieldWrapper = new JPanel(); @@ -64,8 +65,9 @@ public class GameLogic extends JPanel { private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween, JPanel panel, ArrayList fields, boolean registerMouseListener) { - for (int x = 0; x < 10; x++) { - for (int y = 0; y < 10; y++) { + + for (int x = 0; x < this.matchfieldSize; x++) { + for (int y = 0; y < this.matchfieldSize; y++) { JPanel field = new JPanel(); int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween); @@ -74,47 +76,40 @@ public class GameLogic extends JPanel { field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); field.setName(x + ":" + y); + fields.add(field); panel.add(field); - if (registerMouseListener) { - field.addMouseListener(new MouseAdapter() { + if (!registerMouseListener) + continue; - public void mouseClicked(MouseEvent e) { - System.out.println("mouse clicked"); - if (!playerMove) - return; + field.addMouseListener(new MouseAdapter() { - int[] coords = getCoordinateFromLabel(field); - int coordX = coords[0]; - int coordY = coords[1]; + public void mouseClicked(MouseEvent e) { + int[] coords = getCoordinateFromLabel(field); + int coordX = coords[0]; + int coordY = coords[1]; - Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); + Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); - boolean shotSuccess = chosenField.shoot(); - gui.updateFields(); - - if (shotSuccess && chosenField.getState() != Coordinate.HIT) { - nextMove(false); - } else { - nextMove(true); - } + boolean shotSuccess = chosenField.shoot(); + if (shotSuccess && chosenField.getState() != Coordinate.HIT) { + nextMove(false); + } else { + nextMove(true); } - }); - } + + } + }); + } } } - public ArrayList getFields() { - return playerFields; - } - private JLabel getXLabel() { JLabel x = new JLabel("x"); x.setForeground(Color.WHITE); - return x; } @@ -185,7 +180,7 @@ public class GameLogic extends JPanel { this.gameState = GameLogic.PREPARATION; this.playerMove = true; - // create player matchfield + // create player matchfield and set ships this.matchfield = new Matchfield(matchfieldSize); this.matchfield.createMatchfield(); this.matchfield.setShip(new Coordinate(4, 6), 2, 0); @@ -193,15 +188,17 @@ public class GameLogic extends JPanel { this.matchfield.setShip(new Coordinate(9, 1), 4, 1); this.matchfield.setShip(new Coordinate(0, 0), 5, 0); - // create enemy matchfield and initialize AI (with player's matchfield) + this.aiLogic = new Logic(); + this.aiLogic.setMatchfield(this.matchfield); + + // create enemy matchfield, set ships and initialize AI (with player's + // matchfield) this.enemyMatchfield = new Matchfield(matchfieldSize); this.enemyMatchfield.createMatchfield(); this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); - this.aiLogic = new Logic(); - this.aiLogic.setMatchfield(this.matchfield); // enter game loop this.gameState = GameLogic.RUNNING; @@ -213,7 +210,7 @@ public class GameLogic extends JPanel { if (this.playerMove) { gui.updateFields(); - // waiting for mouse click event.... + // waiting here for mouse click event.... } else { Coordinate aiChosenField = null; @@ -221,12 +218,11 @@ public class GameLogic extends JPanel { aiChosenField = this.aiLogic.chooseField(); } - aiChosenField.print(); aiChosenField.shoot(); + gui.updateFields(); boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; - this.nextMove(aiHasNextMove); } @@ -235,8 +231,4 @@ public class GameLogic extends JPanel { public int getGameState() { return gameState; } - - public boolean isPlayerMove() { - return playerMove; - } }