From fff174937b7adfe84fe2e6806776f574f076db32 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:28:20 +0100 Subject: [PATCH] Refactoring: getCoordinateFromLabel() --- .../de/tims/fleetstorm/gui/GameLogic.java | 725 +++++++++--------- 1 file changed, 373 insertions(+), 352 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index f8ba163..9225309 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -1,352 +1,373 @@ -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.ai.Logic; -import de.tims.fleetstorm.matchfield.Coordinate; -import de.tims.fleetstorm.matchfield.Matchfield; - -public class GameLogic extends JPanel { - - // GameManager stuff - private int gameState; - private Matchfield matchfield; - private Matchfield enemyMatchfield; - private int matchfieldSize = 10; - private boolean playerMove; - private Logic aiLogic; - - 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 GameLogic(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))); - field.setName(x + ":" + y); - 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; - } - - private void updateField(boolean playerMove, Matchfield matchfield) { - for (JPanel coordinatePanel : this.fields) { - String panelName = coordinatePanel.getName(); - String[] panelNameSplit = panelName.split(":"); - int coordX = Integer.parseInt(panelNameSplit[0]); - int coordY = Integer.parseInt(panelNameSplit[1]); - - // reset field - coordinatePanel.setBackground(null); - coordinatePanel.removeAll(); - - int state = matchfield.getField(coordX, coordY).getState(); - - switch (state) { - case Coordinate.SHIP: - if (playerMove) - coordinatePanel.setBackground(new Color(91, 58, 41)); - break; - case Coordinate.SHOT: - coordinatePanel.setBackground(new Color(0, 94, 184)); - break; - case Coordinate.HIT: - coordinatePanel.setBackground(new Color(0, 94, 184)); - coordinatePanel.add(new JLabel("x")); - break; - } - - coordinatePanel.revalidate(); - coordinatePanel.repaint(); - } - } - - /** - * This function is only for testing - */ - private static GameLogic gui; - - public static void main(String[] args) { - JFrame frame = new JFrame("Test GUI"); - gui = new GameLogic(15, 28, 1); - frame.setContentPane(gui); - frame.setSize(640, 480); - frame.setResizable(true); - frame.setVisible(true); - - gui.start(); - } - - public void start() { - this.playerMove = true; - this.gameState = GameLogic.PREPARATION; - - // create player matchfield - this.matchfield = new Matchfield(matchfieldSize); - this.matchfield.createMatchfield(); - this.matchfield.setShip(new Coordinate(4, 6), 2, 0); - this.matchfield.setShip(new Coordinate(1, 3), 3, 0); - 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.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); - - // show player own matchfield - gui.updateField(this.playerMove, this.matchfield); - - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - // enter game loop - this.nextMove(); - } - - public void nextMove() { - this.playerMove = !this.playerMove; - - if (this.playerMove) { - gui.updateField(this.playerMove, this.enemyMatchfield); - } else { - gui.updateField(this.playerMove, this.matchfield); - } - - } - - public int getGameState() { - return gameState; - } - - public boolean isPlayerMove() { - return playerMove; - } -} +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.ai.Logic; +import de.tims.fleetstorm.matchfield.Coordinate; +import de.tims.fleetstorm.matchfield.Matchfield; + +public class GameLogic extends JPanel { + + // GameManager stuff + private int gameState; + private Matchfield matchfield; + private Matchfield enemyMatchfield; + private int matchfieldSize = 10; + private boolean playerMove; + private Logic aiLogic; + + 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 GameLogic(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("-"); + gameInfoContentPanel.add(matchfieldValue); + + JLabel moveLabel = new JLabel("Am Zug"); + gameInfoContentPanel.add(moveLabel); + + moveValue = new JLabel("-"); + 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))); + field.setName(x + ":" + y); + fields.add(field); + fieldWrapper.add(field); + + field.addMouseListener(new MouseAdapter() { + + public void mouseClicked(MouseEvent e) { + if (!playerMove) + return; + + int[] coords = getCoordinateFromLabel(field); + int coordX = coords[0]; + int coordY = coords[1]; + + // TODO add shoot function + } + }); + } + } + + } + + 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; + } + + private int[] getCoordinateFromLabel(JPanel panel) { + String panelName = panel.getName(); + String[] panelNameSplit = panelName.split(":"); + int coordX = Integer.parseInt(panelNameSplit[0]); + int coordY = Integer.parseInt(panelNameSplit[1]); + return new int[] { coordX, coordY }; + } + + private void updateField(boolean playerMove, Matchfield matchfield) { + for (JPanel coordinatePanel : this.fields) { + int[] coords = this.getCoordinateFromLabel(coordinatePanel); + int coordX = coords[0]; + int coordY = coords[1]; + + // reset field + coordinatePanel.setBackground(null); + coordinatePanel.removeAll(); + + int state = matchfield.getField(coordX, coordY).getState(); + + switch (state) { + case Coordinate.SHIP: + if (playerMove) + coordinatePanel.setBackground(new Color(91, 58, 41)); + break; + case Coordinate.SHOT: + coordinatePanel.setBackground(new Color(0, 94, 184)); + break; + case Coordinate.HIT: + coordinatePanel.setBackground(new Color(0, 94, 184)); + coordinatePanel.add(new JLabel("x")); + break; + } + + coordinatePanel.revalidate(); + coordinatePanel.repaint(); + } + + // set game infos + if (this.playerMove) { + this.getMatchfieldValue().setText("Gegner"); + this.getMoveValue().setText("Du"); + } else { + this.getMatchfieldValue().setText("Eigenes"); + this.getMoveValue().setText("Gegner"); + } + } + + /** + * This function is only for testing + */ + private static GameLogic gui; + + public static void main(String[] args) { + JFrame frame = new JFrame("Test GUI"); + gui = new GameLogic(15, 28, 1); + frame.setContentPane(gui); + frame.setSize(640, 480); + frame.setResizable(true); + frame.setVisible(true); + + gui.start(); + } + + public void start() { + this.playerMove = true; + this.gameState = GameLogic.PREPARATION; + + // create player matchfield + this.matchfield = new Matchfield(matchfieldSize); + this.matchfield.createMatchfield(); + this.matchfield.setShip(new Coordinate(4, 6), 2, 0); + this.matchfield.setShip(new Coordinate(1, 3), 3, 0); + 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.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); + + // show player own matchfield + gui.updateField(this.playerMove, this.matchfield); + + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // enter game loop + this.nextMove(); + } + + public void nextMove() { + this.playerMove = !this.playerMove; + + if (this.playerMove) { + gui.updateField(this.playerMove, this.enemyMatchfield); + } else { + gui.updateField(this.playerMove, this.matchfield); + } + + } + + public int getGameState() { + return gameState; + } + + public boolean isPlayerMove() { + return playerMove; + } +}