From 66f48dd8d16ace6beea259e41b665016bbb1a91c Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 11:41:14 +0100 Subject: [PATCH 1/5] Added GameManager and check if GameState is correct after start function call --- .../java/de/tims/fleetstorm/GameManager.java | 19 +++++++++++++++++ .../de/tims/fleetstorm/GameManagerTest.java | 21 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/main/java/de/tims/fleetstorm/GameManager.java create mode 100644 src/test/java/de/tims/fleetstorm/GameManagerTest.java diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java new file mode 100644 index 0000000..8f682cc --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/GameManager.java @@ -0,0 +1,19 @@ +package de.tims.fleetstorm; + +public class GameManager { + + private int gameState; + + 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; + } + + public int getGameState() { + return gameState; + } + +} diff --git a/src/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/GameManagerTest.java new file mode 100644 index 0000000..d90e85f --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/GameManagerTest.java @@ -0,0 +1,21 @@ +package de.tims.fleetstorm; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class GameManagerTest { + + GameManager gameManager = new GameManager(); + + @Test + void testIfGameStateIsPreparationAfterStart() { + gameManager.start(); + int expectedState = GameManager.PREPARATION; + + int calculatedState = gameManager.getGameState(); + + assertEquals(expectedState, calculatedState); + } + +} From 8b2f6e2a2fbd9597bb278bb1a629f8b189e79911 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 22 Jan 2022 17:43:06 +0100 Subject: [PATCH 2/5] gameexplorer_gui: added logout and exit button --- .../de/tims/gameexplorer/GameExplorer.java | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index 6f67e58..07915d1 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -25,11 +25,14 @@ public class GameExplorer { private JPanel border4; private JPanel border5; private JPanel border6; + private JPanel border7; private JButton loginBtn; private JButton fleetstormBtn; private JButton fourwinsBtn; private JButton tictactoeBtn; private JButton leaderboardBtn; + private JButton logoutBtn; + private JButton exitBtn; private JButton backBtn; private JLabel username; private JLabel loginWarning; @@ -38,6 +41,7 @@ public class GameExplorer { private Dimension minSize; private Dimension loginBtnSize; private Dimension btnSize; + private Dimension smallBtnSize; private GridBagConstraints gbc; private static final String playerFile = "src/main/java/resources/player_data.csv"; @@ -55,6 +59,7 @@ public class GameExplorer { minSize = new Dimension(400, 300); loginBtnSize = new Dimension(91, 20); btnSize = new Dimension(160, 40); + smallBtnSize = new Dimension(75, 30); gbc = new GridBagConstraints(); buildExplorerPanel(); @@ -87,6 +92,10 @@ public class GameExplorer { leaderboardBtn = new JButton("Leaderboard"); leaderboardBtn.setPreferredSize(btnSize); leaderboardBtn.addActionListener(new GameAction()); + logoutBtn = new JButton("Logout"); + logoutBtn.setPreferredSize(smallBtnSize); + exitBtn = new JButton("Exit"); + exitBtn.setPreferredSize(smallBtnSize); border1 = new JPanel(); border1.setOpaque(false); @@ -98,51 +107,82 @@ public class GameExplorer { border4.setOpaque(false); border5 = new JPanel(); border5.setOpaque(false); + border7 = new JPanel(); + border7.setOpaque(false); gbc.gridx = 0; gbc.gridy = 0; - gbc.weighty = 0.2; + gbc.gridwidth = 3; + gbc.weighty = 1.0 / 6; explorerPanel.add(border1, gbc); gbc.gridx = 0; gbc.gridy = 1; + gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(fleetstormBtn, gbc); gbc.gridx = 0; gbc.gridy = 2; - gbc.weighty = 0.2; + gbc.gridwidth = 3; + gbc.weighty = 1.0 / 6; explorerPanel.add(border2, gbc); gbc.gridx = 0; gbc.gridy = 3; + gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(fourwinsBtn, gbc); gbc.gridx = 0; gbc.gridy = 4; - gbc.weighty = 0.2; + gbc.gridwidth = 3; + gbc.weighty = 1.0 / 6; explorerPanel.add(border3, gbc); gbc.gridx = 0; gbc.gridy = 5; + gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(tictactoeBtn, gbc); gbc.gridx = 0; gbc.gridy = 6; - gbc.weighty = 0.2; + gbc.gridwidth = 3; + gbc.weighty = 1.0 / 6; explorerPanel.add(border4, gbc); gbc.gridx = 0; gbc.gridy = 7; + gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(leaderboardBtn, gbc); - + gbc.gridx = 0; gbc.gridy = 8; - gbc.weighty = 0.2; + gbc.weighty = 1.0 / 6; explorerPanel.add(border5, gbc); + + gbc.gridx = 0; + gbc.gridy = 9; + gbc.gridwidth = 1; + gbc.weighty = 0.0; + gbc.insets = new Insets(0, 0, 0, 5); + explorerPanel.add(logoutBtn, gbc); + + gbc.gridx = 2; + gbc.gridy = 9; + gbc.gridwidth = 1; + gbc.weighty = 0.0; + gbc.insets = new Insets(0, 5, 0, 0); + explorerPanel.add(exitBtn, gbc); + + gbc.insets = new Insets(0, 0, 0, 0); + gbc.gridx = 0; + gbc.gridy = 10; + gbc.gridwidth = 3; + gbc.weighty = 1.0 / 6; + explorerPanel.add(border7, gbc); } private void buildLoginPanel() { @@ -245,8 +285,6 @@ public class GameExplorer { frame.add(explorerPanel); frame.revalidate(); frame.repaint(); - - System.out.println("Actual Player: " + actualPlayer.getName() + ", Points: " + actualPlayer.getPoints()); } else { loginWarning.setText("Kein Name eingegeben!"); } From 713f6b19c8e4232c0543ea50ac0bf185971a118b Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 22 Jan 2022 17:54:02 +0100 Subject: [PATCH 3/5] gameexplorer_gui: added actionListener for logout button --- .../de/tims/gameexplorer/GameExplorer.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index 07915d1..f78f205 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -44,7 +44,7 @@ public class GameExplorer { private Dimension smallBtnSize; private GridBagConstraints gbc; - private static final String playerFile = "src/main/java/resources/player_data.csv"; + private static final String PLAYER_FILE = "src/main/java/resources/player_data.csv"; private enum Game { FLEETSTORM, FOURWINS, TICTACTOE, LEADERBOARD }; private Game actualGame; @@ -94,6 +94,7 @@ public class GameExplorer { leaderboardBtn.addActionListener(new GameAction()); logoutBtn = new JButton("Logout"); logoutBtn.setPreferredSize(smallBtnSize); + logoutBtn.addActionListener(new LogoutAction()); exitBtn = new JButton("Exit"); exitBtn.setPreferredSize(smallBtnSize); @@ -278,9 +279,10 @@ public class GameExplorer { if (!userInput.equals("")) { loginWarning.setText(""); - manager.loadPlayers(playerFile); + manager.loadPlayers(PLAYER_FILE); actualPlayer = manager.selectPlayer(userInput); + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.remove(loginPanel); frame.add(explorerPanel); frame.revalidate(); @@ -321,6 +323,21 @@ public class GameExplorer { } } + private class LogoutAction implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + manager.savePlayers(PLAYER_FILE); + + buildLoginPanel(); + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.remove(explorerPanel); + frame.add(loginPanel); + frame.revalidate(); + frame.repaint(); + } + } + private class BackAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { From bea41ebeb427e9e6ef8d86a2dced5df4159b7477 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 22 Jan 2022 18:00:54 +0100 Subject: [PATCH 4/5] gameexplorer_gui: added actionListener for exit button --- src/main/java/de/tims/gameexplorer/GameExplorer.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index f78f205..cba4803 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -97,6 +97,7 @@ public class GameExplorer { logoutBtn.addActionListener(new LogoutAction()); exitBtn = new JButton("Exit"); exitBtn.setPreferredSize(smallBtnSize); + exitBtn.addActionListener(new ExitAction()); border1 = new JPanel(); border1.setOpaque(false); @@ -338,6 +339,15 @@ public class GameExplorer { } } + private class ExitAction implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + manager.savePlayers(PLAYER_FILE); + + System.exit(0); + } + } + private class BackAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { From e4efb4ae0d31cdea412779ecce59da50498bb43a Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 22 Jan 2022 18:59:11 +0100 Subject: [PATCH 5/5] gameexplorer_gui: gameexplorer gui shows name and points of actual player --- .../de/tims/gameexplorer/GameExplorer.java | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index cba4803..2eb997f 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -36,6 +36,10 @@ public class GameExplorer { private JButton backBtn; private JLabel username; private JLabel loginWarning; + private JLabel playerName; + private JLabel playerPoints; + private JLabel actualPlayerName; + private JLabel actualPlayerPoints; private JLabel chosenGame; private JTextField usernameInput; private Dimension minSize; @@ -56,13 +60,12 @@ public class GameExplorer { frame = new JFrame("1000 infomagische Spiele"); - minSize = new Dimension(400, 300); + minSize = new Dimension(480, 360); loginBtnSize = new Dimension(91, 20); btnSize = new Dimension(160, 40); smallBtnSize = new Dimension(75, 30); gbc = new GridBagConstraints(); - buildExplorerPanel(); buildLoginPanel(); buildNavigationPanel(); buildGamePanels(); @@ -80,6 +83,11 @@ public class GameExplorer { explorerPanel = new JPanel(); explorerPanel.setLayout(new GridBagLayout()); + playerName = new JLabel("Spieler:"); + playerPoints = new JLabel("Punkte:"); + actualPlayerName = new JLabel(actualPlayer.getName()); + actualPlayerPoints = new JLabel("" + actualPlayer.getPoints()); + fleetstormBtn = new JButton("Schiffe versenken"); fleetstormBtn.setPreferredSize(btnSize); fleetstormBtn.addActionListener(new GameAction()); @@ -112,6 +120,9 @@ public class GameExplorer { border7 = new JPanel(); border7.setOpaque(false); + gbc.weightx = 0.0; + gbc.anchor = GridBagConstraints.CENTER; + gbc.insets = new Insets(0, 0, 0, 0); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 3; @@ -162,6 +173,7 @@ public class GameExplorer { gbc.gridx = 0; gbc.gridy = 8; + gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border5, gbc); @@ -169,6 +181,7 @@ public class GameExplorer { gbc.gridy = 9; gbc.gridwidth = 1; gbc.weighty = 0.0; + gbc.anchor = GridBagConstraints.LINE_END; gbc.insets = new Insets(0, 0, 0, 5); explorerPanel.add(logoutBtn, gbc); @@ -176,15 +189,47 @@ public class GameExplorer { gbc.gridy = 9; gbc.gridwidth = 1; gbc.weighty = 0.0; + gbc.anchor = GridBagConstraints.LINE_START; gbc.insets = new Insets(0, 5, 0, 0); explorerPanel.add(exitBtn, gbc); + gbc.anchor = GridBagConstraints.CENTER; gbc.insets = new Insets(0, 0, 0, 0); gbc.gridx = 0; gbc.gridy = 10; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border7, gbc); + + gbc.gridx = 0; + gbc.gridy = 11; + gbc.gridwidth = 1; + gbc.weighty = 0.0; + gbc.anchor = GridBagConstraints.LINE_END; + explorerPanel.add(playerName, gbc); + + gbc.gridx = 2; + gbc.gridy = 11; + gbc.gridwidth = 1; + gbc.weighty = 0.0; + gbc.insets = new Insets(0, 5, 0, 0); + gbc.anchor = GridBagConstraints.LINE_START; + explorerPanel.add(actualPlayerName, gbc); + + gbc.gridx = 0; + gbc.gridy = 12; + gbc.gridwidth = 1; + gbc.weighty = 0.0; + gbc.anchor = GridBagConstraints.LINE_END; + explorerPanel.add(playerPoints, gbc); + + gbc.gridx = 2; + gbc.gridy = 12; + gbc.gridwidth = 1; + gbc.weighty = 0.0; + gbc.insets = new Insets(0, 5, 0, 0); + gbc.anchor = GridBagConstraints.LINE_START; + explorerPanel.add(actualPlayerPoints, gbc); } private void buildLoginPanel() { @@ -198,7 +243,10 @@ public class GameExplorer { loginWarning = new JLabel(); usernameInput = new JTextField(8); - gbc.weighty = 0; + gbc.gridwidth = 1; + gbc.weightx = 0.0; + gbc.weighty = 0.0; + gbc.anchor = GridBagConstraints.CENTER; gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new Insets(0, 0, 5, 0); @@ -232,7 +280,9 @@ public class GameExplorer { border6 = new JPanel(); border6.setOpaque(false); + gbc.gridwidth = 1; gbc.weighty = 0.0; + gbc.anchor = GridBagConstraints.CENTER; gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 0.0; @@ -283,6 +333,8 @@ public class GameExplorer { manager.loadPlayers(PLAYER_FILE); actualPlayer = manager.selectPlayer(userInput); + buildExplorerPanel(); + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.remove(loginPanel); frame.add(explorerPanel); @@ -365,6 +417,8 @@ public class GameExplorer { gamePanel.remove(leaderboardPanel); } + actualPlayerPoints.setText("" + actualPlayer.getPoints()); + frame.remove(gamePanel); frame.add(explorerPanel); frame.revalidate();