diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Grafik/mainmenubackground.png b/graphics/mainmenubackground.png similarity index 100% rename from Grafik/mainmenubackground.png rename to graphics/mainmenubackground.png diff --git a/Grafik/sound button muted.png b/graphics/sound button muted.png similarity index 100% rename from Grafik/sound button muted.png rename to graphics/sound button muted.png diff --git a/Grafik/sound button.png b/graphics/sound button.png similarity index 100% rename from Grafik/sound button.png rename to graphics/sound button.png diff --git a/src/AiPlayer.java b/src/AiPlayer.java new file mode 100644 index 0000000..05fc045 --- /dev/null +++ b/src/AiPlayer.java @@ -0,0 +1,27 @@ +import java.util.Random; +import java.awt.Point; + +public abstract class AiPlayer extends Player { + + public AiPlayer(int size) { + super(size); + } + public Point RandomPoint() { + Random random = new Random(); // Pseudo Random für zufallszahlen + int posx = random.nextInt(super.board.size); // Generiert 0 - 13 + int posy = random.nextInt(super.board.size); // + return new Point(posx,posy); + } + + public void AiSetShips() { + for(int i = 0; i < super.board.ships.size(); i++) { // Interiert durch alle Shiffe + while(!super.board.ships.get(i).setPosition(RandomPoint())) + } // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert + return; + } + + public void AiShoot() { + super.board.hit(RandomPoint()); + return; + } +} \ No newline at end of file diff --git a/src/Board.java b/src/Board.java index edb598b..5cec0cd 100644 --- a/src/Board.java +++ b/src/Board.java @@ -1,2 +1,75 @@ +import java.awt.*; +import java.util.List; + public class Board { + + private List hits; + private List ships; + private final int size; + + + public Board(int size) { + this.size = size; + this.createShip(size - 13); + } + + public HitResponse hit (Point point){ + HitResponse response = new HitResponse(HitResponseType.MISS,point); + for (int i = 0; i < this.ships.size(); i++) { + HitResponseType type = this.ships.get(i).shootOnShip(point); + if ( type == HitResponseType.SUNK) { + for (int ii = 0; i < this.ships.size(); ii++) { + if (!this.ships.get(ii).isSunk()) { + response.setType(type); + this.addHits(response); + return response; + } + } + response.setType(HitResponseType.VICTORY); + this.addHits(response); + return response; + } else if (type == HitResponseType.HIT) { + response.setType(type); + this.addHits(response); + return response; + } + } + + this.addHits(response); + return response; + } + + private void createShip(int semester){ + List shipData = Ship.semeterList.get(semester -1); + for (int i = 0; i < shipData.size(); i++) { + this.ships.add(new Ship(shipData.get(i).size(), shipData.get(i).name())); + } + } + + public List getShips() { + return ships; + } + + public boolean addHits(HitResponse hitResponse) { + if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){ + this.hits.add(hitResponse); + return true; + } + return false; + } + + public HitResponse getHitResponsOnPoint(Point point) { + for (int i = 0; i < this.hits.size(); i++){ + if (this.hits.get(i).getPoint().equals(point)){ + return this.hits.get(i); + } + } + return null; + } + + public int getSize() { + return size; + } + } + diff --git a/src/GameBoard.java b/src/GameBoard.java new file mode 100644 index 0000000..61bdb72 --- /dev/null +++ b/src/GameBoard.java @@ -0,0 +1,3 @@ +public class GameBoard { + +} diff --git a/src/GameController.java b/src/GameController.java new file mode 100644 index 0000000..797def8 --- /dev/null +++ b/src/GameController.java @@ -0,0 +1,16 @@ +public class GameController { + + + public void startOnlineGame() { + // fuck you Luca and Ole von Florian und nicht von Florian + } + + public void startLocalGame(Class localPlayerClass, Class enemyClass, int size) throws InstantiationException, IllegalAccessException { + + LocalPlayer localPlayer = localPlayerClass.newInstance(); + AiPlayer aiPlayer = enemyClass.newInstance(); + localPlayer.setEnemy(aiPlayer); + aiPlayer.setEnemy(localPlayer); + } + +} \ No newline at end of file diff --git a/src/HalloSchiffeVersenken.java b/src/HalloSchiffeVersenken.java index eaa1be8..066890e 100644 --- a/src/HalloSchiffeVersenken.java +++ b/src/HalloSchiffeVersenken.java @@ -1,14 +1,12 @@ public class HalloSchiffeVersenken { public static void main(String[] args) throws InterruptedException { - /*Luccas Methoden - MainMenuModel model = new MainMenuModel(); - MainMenuView view = new MainMenuView(); - MainMenuController controller = new MainMenuController(model, view); -*/ + MainFrame mf = new MainFrame(); + mf.setVisible(true); + System.out.println("HelloSchiffeVersenekn"); -/* + System.out.println("sound"); SoundHandler.playSound("hit"); @@ -17,7 +15,7 @@ public class HalloSchiffeVersenken { SoundHandler.setSoundOn(false); System.out.println("sound off"); SoundHandler.playSound("hit"); -*/ - startLocalGame huso = new startLocalGame(); + + } } diff --git a/src/HitResponse.java b/src/HitResponse.java index f5c3913..bc9fca0 100644 --- a/src/HitResponse.java +++ b/src/HitResponse.java @@ -3,4 +3,21 @@ import java.awt.*; public class HitResponse { private HitResponseType type; private Point point; + + public HitResponse(HitResponseType type, Point point) { + this.type = type; + this.point = point; + } + + public HitResponseType getHitResponse() { + return this.type; + } + + public Point getPoint() { + return this.point; + } + + public void setType(HitResponseType type) { + this.type = type; + } } diff --git a/src/HumanPlayer.java b/src/HumanPlayer.java new file mode 100644 index 0000000..6723e71 --- /dev/null +++ b/src/HumanPlayer.java @@ -0,0 +1,7 @@ + +public class HumanPlayer extends LocalPlayer { + + public HumanPlayer(int size) { + super(size); + } +} diff --git a/src/JoinGame.java b/src/JoinGame.java new file mode 100644 index 0000000..e980b58 --- /dev/null +++ b/src/JoinGame.java @@ -0,0 +1,44 @@ +import java.awt.*; +import java.awt.event.ActionEvent; +import javax.swing.JFrame; +import javax.swing.*; + +public class JoinGame extends JPanel { + JLabel spielBeitretenLabel= new JLabel("Spiel beitreten"); + JLabel ipLabel = new JLabel("IP-Adresse"); + JLabel portLabel = new JLabel("Port"); + + JTextField ipTextField = new JTextField(20); + JTextField portTextField = new JTextField(20); + + JButton losButton = new JButton("Los!"); + + Font robotoFont = new Font("Roboto", Font.BOLD, 45); + + public JoinGame(MainFrame frame) { + setLayout(null); + buildPanel(frame); + } + + private void buildPanel(MainFrame frame) { + spielBeitretenLabel.setBounds(20,20,700, 100); + losButton.setBounds(320, 225, 100, 50); + + ipLabel.setBounds(50, 125, 200, 30); + portLabel.setBounds(50, 200, 200, 30); + + ipTextField.setBounds(50, 150, 250, 50); + portTextField.setBounds(50, 225, 250, 50); + + + spielBeitretenLabel.setFont(robotoFont.deriveFont(50f)); + + add(spielBeitretenLabel); + add(ipLabel); + add(portLabel); + add(losButton); + add(ipTextField); + add(portTextField); + } + +} diff --git a/src/LocalPlayer.java b/src/LocalPlayer.java new file mode 100644 index 0000000..cfa7cab --- /dev/null +++ b/src/LocalPlayer.java @@ -0,0 +1,8 @@ +public class LocalPlayer extends Player { + + LocalPlayer(int size) { + super(size); + } + + +} \ No newline at end of file diff --git a/src/MainFrame.java b/src/MainFrame.java new file mode 100644 index 0000000..f8a5204 --- /dev/null +++ b/src/MainFrame.java @@ -0,0 +1,55 @@ +import javax.swing.*; +import java.awt.*; + +public class MainFrame extends JFrame { + + private CardLayout cardLayout; + private JPanel mainPanel; + + public MainFrame() { + setTitle("Studium Versenken"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(1500, 1000); + setLocationRelativeTo(null); + + //JLabel backgroundLabel = new JLabel(new ImageIcon("graphics/mainmenubackground.png")); + // backgroundLabel.setBounds(0, 0, 1500, 1000); + // getContentPane().add(backgroundLabel); + + // backgroundLabel.setOpaque(true); + + // CardLayout und Hauptpanel erstellen + cardLayout = new CardLayout(); + mainPanel = new JPanel(cardLayout); + + // Verschiedene Panels erstellen und hinzufügen + MainMenuView mainMenuView = new MainMenuView(this); + startLocalGame localGame = new startLocalGame(this); + startMultiplayerGame multiplayerGame = new startMultiplayerGame(this); + coinToss coinToss = new coinToss(this); + JoinGame joinGame = new JoinGame(this); + + mainPanel.add(mainMenuView, "MainMenu"); + mainPanel.add(localGame, "LocalGame"); + mainPanel.add(multiplayerGame, "MultiplayerGame"); + mainPanel.add(coinToss, "coinToss"); + mainPanel.add(joinGame, "JoinGame"); + // Hauptpanel in JFrame hinzufügen + add(mainPanel); + + // Hauptmenü anzeigen + cardLayout.show(mainPanel, "MainMenu"); + } + + // Methode, um die Ansicht zu wechseln + public void showPanel(String panelName) { + cardLayout.show(mainPanel, panelName); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + MainFrame frame = new MainFrame(); + frame.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/src/MainMenuController.java b/src/MainMenuController.java index 4abc54b..c0ddcb1 100644 --- a/src/MainMenuController.java +++ b/src/MainMenuController.java @@ -9,23 +9,27 @@ public class MainMenuController implements ActionListener { public MainMenuController(MainMenuModel model, MainMenuView view) { this.view = view; this.model = model; - this.view.getLocalButton().addActionListener(this); - this.view.getMultiButton().addActionListener(this); - this.view.getSoundButton().addActionListener(this); + //this.view.getLocalButton().addActionListener(this); + //this.view.getMultiButton().addActionListener(this); + // this.view.getSoundButton().addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == view.getMultiButton()) { - view.closeWindOw(); - MultiMenuModel model = new MultiMenuModel(); - MultiMenuView view = new MultiMenuView(); - MultiMenuController controller2 = new MultiMenuController(model, view); - } else if (e.getSource() == view.getMultiButton()) { - model.setGameMode("Multiplayer"); - JOptionPane.showMessageDialog(view.getFrame(), "Multiplayer game selected."); - }else if (e.getSource() == view.getSoundButton()) { - view.toggleMute(); + //if (e.getSource() == view.getMultiButton()) { + // model.setGameMode("Multiplayer"); + // MultiMenuModel multiModel = new MultiMenuModel(); + // MultiMenuView multiView = new MultiMenuView(); + // MultiMenuController multiMenuController = new MultiMenuController(multiModel, multiView); + // view.addPanel(multiView.getMultiPanel(), "MultiMenu"); + // view.showPanel("MultiMenu"); + // }else if (e.getSource() == view.getSoundButton()) { + // view.toggleMute(); + // }else if(e.getSource() == view.getLocalButton()) { + model.setGameMode("LocalGame"); + //startLocalGame localGame = new startLocalGame(); + // view.addPanel(localGame.getLocalPanel(), "LocalMenu"); + // view.showPanel("LocalMenu"); } } -} \ No newline at end of file +//} \ No newline at end of file diff --git a/src/MainMenuView.java b/src/MainMenuView.java index a79fafc..958d610 100644 --- a/src/MainMenuView.java +++ b/src/MainMenuView.java @@ -1,73 +1,51 @@ import java.awt.*; +import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.*; -public class MainMenuView { - ImageIcon SoundIcon = new ImageIcon("Grafik/sound button.png"); - private JFrame frame = new JFrame(); +public class MainMenuView extends JPanel { + private JLabel titelLabel = new JLabel("Studium versenken"); private JButton lokalButton = new JButton("Lokal"); - private JButton multiButton= new JButton("Multiplayer"); - private JButton soundButton = new JButton(SoundIcon); - Font robotoFont = new Font("Roboto", Font.BOLD, 45); - public MainMenuView() { - buildFrame(); - buildComponents(); + private JButton multiButton = new JButton("Multiplayer"); + private JButton soundButton; + Font robotoFont = new Font("Roboto", Font.BOLD, 45); + private ImageIcon soundIcon = new ImageIcon("graphics/sound button.png"); + private ImageIcon muteIcon = new ImageIcon("graphics/sound button muted.png"); + + public MainMenuView(MainFrame frame) { + setLayout(null); + buildPanel(frame); } + private void buildPanel(MainFrame frame) { + lokalButton.setBounds(200, 200, 500, 500); + multiButton.setBounds(800, 200, 500, 500); + titelLabel.setBounds(550, 10, 700, 100); + soundButton = new JButton(soundIcon); + soundButton.setBounds(20, 20, 128, 128); - public void buildComponents(){ - lokalButton.setBounds(200,200,500,500); - multiButton.setBounds(800,200,500,500); - titelLabel.setBounds(550,10,700,100); - soundButton.setBounds(20,20,128,128); titelLabel.setFont(robotoFont); lokalButton.setFont(robotoFont.deriveFont(50f)); multiButton.setFont(robotoFont.deriveFont(50f)); + + add(titelLabel); + add(lokalButton); + add(multiButton); + add(soundButton); + + // Event Listener für Buttons + lokalButton.addActionListener(e -> frame.showPanel("LocalGame")); + multiButton.addActionListener(e -> frame.showPanel("MultiplayerGame")); + soundButton.addActionListener(e -> toggleMute()); } - public void buildFrame() { - frame.setLayout(null); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(1500,1000); - frame.setVisible(true); - frame.getContentPane().setBackground( Color.decode("#98F5FF")); - JLabel backgroundLabel = new JLabel(new ImageIcon("Grafik/mainmenubackground.png")); - frame.setContentPane(backgroundLabel); - frame.add(titelLabel); - frame.add(lokalButton); - frame.add(multiButton); - frame.add(soundButton); - frame.setLocationRelativeTo(null); - } - - public void toggleMute(){ - ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png"); - if(soundButton.getIcon()==SoundIcon) { - soundButton.setIcon(MuteIcon); - }else{ - soundButton.setIcon(SoundIcon); + private void toggleMute() { + if (soundButton.getIcon() == soundIcon) { + soundButton.setIcon(muteIcon); + } else { + soundButton.setIcon(soundIcon); } } - - public void closeWindOw(){ - frame.dispose(); - } - public JFrame getFrame() { - return frame; - } - - public JButton getLocalButton() { - return lokalButton; - } - - public JButton getMultiButton() { - return multiButton; - } - - public JButton getSoundButton() { - return soundButton; - } - -} +} \ No newline at end of file diff --git a/src/MultiMenuController.java b/src/MultiMenuController.java index ea97136..79d8dfb 100644 --- a/src/MultiMenuController.java +++ b/src/MultiMenuController.java @@ -9,13 +9,14 @@ public class MultiMenuController implements ActionListener { public MultiMenuController(MultiMenuModel model, MultiMenuView view) { this.view = view; this.model = model; - this.view.getSoundButton().addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == view.getSoundButton()) { - view.toggleMute(); + if (e.getSource() == view.getPlayerLeftButton()){ + view.togglePlayerIcon(); + }else if(e.getSource() == view.getPlayerRightButton()){ + view.togglePlayerIcon(); } } } \ No newline at end of file diff --git a/src/MultiMenuView.java b/src/MultiMenuView.java index 6b1b6b0..b5e7fec 100644 --- a/src/MultiMenuView.java +++ b/src/MultiMenuView.java @@ -3,51 +3,81 @@ import javax.swing.JFrame; import javax.swing.*; public class MultiMenuView { - ImageIcon SoundIcon = new ImageIcon("Grafik/sound button.png"); - private JFrame frame = new JFrame(); - private JButton soundButton = new JButton(SoundIcon); - Font robotoFont = new Font("Roboto", Font.BOLD, 45); + int semesterCounter = 1; + private JPanel multiPanel = new JPanel(null); + + private ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png"); + private ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png"); + + JButton PlayerLeftButton = new JButton("<-"); + JButton PlayerRightButton = new JButton("->"); + JButton semesterUpButton = new JButton("^"); + JButton semesterDownButton = new JButton("v"); + + JLabel semesterLabel = new JLabel("Semester"); + private JLabel titelLabel = new JLabel("Multiplayer"); + private JLabel PlayerIcon = new JLabel(humanPlayerIcon); + private JLabel PlayerName = new JLabel("Name"); + private JTextField PlayerTextField = new JTextField(20); + JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter)); + + private Font robotoFont = new Font("Roboto", Font.BOLD, 45); public MultiMenuView() { - buildFrame(); - buildComponents(); - + buildPanel(); } - public void buildComponents(){ - soundButton.setBounds(20,20,128,128); + public void buildPanel(){ + titelLabel.setBounds(20,20,700,100); + PlayerIcon.setBounds(75, 400, 200, 128); + PlayerName.setBounds(50, 625, 200, 30); + PlayerTextField.setBounds(50, 650, 250, 50); + semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons + PlayerLeftButton.setBounds(50, 450, 50, 50); + PlayerRightButton.setBounds(250, 450, 50, 50); + semesterLabel.setBounds(500, 400, 200, 30); + semesterUpButton.setBounds(700, 400, 50, 50); + semesterDownButton.setBounds(700, 550, 50, 50); + + + multiPanel.setLayout(null); + titelLabel.setFont(robotoFont.deriveFont(50f)); + semesterLabel.setFont(robotoFont.deriveFont(20f)); + + multiPanel.add(PlayerName); + multiPanel.add(titelLabel); + multiPanel.add(PlayerIcon); + multiPanel.add(PlayerTextField); + multiPanel.add(semesterCounterLabel); + multiPanel.add(semesterUpButton); + multiPanel.add(semesterDownButton); + multiPanel.add(PlayerLeftButton); + multiPanel.add(PlayerRightButton); + multiPanel.add(semesterLabel); } - public void buildFrame() { - frame.setLayout(null); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(1500,1000); - frame.setVisible(true); - frame.getContentPane().setBackground( Color.decode("#98F5FF")); - JLabel backgroundLabel = new JLabel(new ImageIcon("Grafik/mainmenubackground.png")); - frame.setContentPane(backgroundLabel); - frame.add(soundButton); - frame.setLocationRelativeTo(null); + public JPanel getMultiPanel() { + return multiPanel; } - public void toggleMute(){ - ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png"); - if(soundButton.getIcon()==SoundIcon) { - soundButton.setIcon(MuteIcon); - }else{ - soundButton.setIcon(SoundIcon); + //public void showMultiPanel(String panelName) { + // CardLayout cl = (CardLayout)mainPanel.getLayout(); + // cl.show(multiPanel, panelName); + //} + + public void togglePlayerIcon() { + if (PlayerIcon.getIcon() == humanPlayerIcon) { + PlayerIcon.setIcon(aiPlayerIcon); + } else { + PlayerIcon.setIcon(humanPlayerIcon); } } - public void closeWindOw(){ - frame.dispose(); - } - public JFrame getFrame() { - return frame; + public JButton getPlayerLeftButton() { + return PlayerLeftButton; } - public JButton getSoundButton() { - return soundButton; + public JButton getPlayerRightButton() { + return PlayerRightButton; } - } diff --git a/src/Player.java b/src/Player.java index b298c07..192b3f4 100644 --- a/src/Player.java +++ b/src/Player.java @@ -1,19 +1,29 @@ import java.awt.*; +import java.util.List; public abstract class Player { - private boolean myTurn; - private boolean isServer; - private boolean waitingForResponse; - private Player enemy; - private String name; - private Board board; + protected boolean myTurn; + protected boolean isServer; + protected boolean waitingForResponse; + protected Player enemy; + protected String name; + protected Board board; + + public Player(int size) { + this.board = new Board(size); + } public void receiveShoot(Point point) { - + HitResponse hitResponse = board.getHitResponsOnPoint(point); + if (!(hitResponse == null)){ + enemy.receiveHit(hitResponse); + } else { + enemy.receiveHit(this.board.hit(point)); + } } public void receiveHit(HitResponse hitResponse) { - + enemy.board.addHits(hitResponse); } public void click(Point point) { @@ -23,4 +33,8 @@ public abstract class Player { public void beginTrun() { } + + public void setEnemy(Player enemy) { + this.enemy = enemy; + } } diff --git a/src/Ship.java b/src/Ship.java new file mode 100644 index 0000000..08db6b6 --- /dev/null +++ b/src/Ship.java @@ -0,0 +1,100 @@ +import java.awt.Point; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +record ShipData (int size, String name){ +} + +public class Ship { + static List> semeterList = Arrays.asList(Arrays.asList( + new ShipData(2, "PRG 1"), + new ShipData(2, "GDI"), + new ShipData(2, "MAT 1"), + new ShipData(2, "THI"), + new ShipData(4, "STP"), + new ShipData(6, "ENG")), + Arrays.asList( + new ShipData(2, "PRG 2"), + new ShipData(2, "DBS 1"), + new ShipData(2, "MAT 2"), + new ShipData(2, "STA"), + new ShipData(2, "AUD")), + Arrays.asList( + new ShipData(2, "PRG 3"), + new ShipData(2, "DBS 2"), + new ShipData(2, "MAT 3"), + new ShipData(2, "BSN 1"), + new ShipData(4, "PRP"), + new ShipData(6, "BWL")), + Arrays.asList( + new ShipData(2, "WEB"), + new ShipData(2, "SE 1"), + new ShipData(2, "CG 1"), + new ShipData(2, "BSN 2"), + new ShipData(4, "SEM"), + new ShipData(6, "E BWL")), + Arrays.asList( + new ShipData(2, "WPF 1"), + new ShipData(2, "SE 2"), + new ShipData(2, "CG 2"), + new ShipData(2, "PXP 1"), + new ShipData(6, "EF 1")), + Arrays.asList( + new ShipData(2, "WPF 2"), + new ShipData(1, "PXP 2"), + new ShipData(8, "BAA")) + ); + + private int size; + private boolean horizontal; + private Point position; + private String name; + private int hitsOnMe; + private boolean sunk; + + public Ship (int size, String name) { + this.size = size; + this.name = name; + this.horizontal = false; + this.position = null; + this.hitsOnMe = 0; + this.sunk = false; + } + + public boolean setPosition(Point pos) { + //TODO Conte abrfrage ob shif da sein darf + this.position = pos; + return true; + } + + public Point getPosition() { + return position; + } + + public boolean isShipOnPos(Point pos){ + if ((this.horizontal && pos.y == this.position.y && pos.x >= this.position.x && pos.x < this.position.x + size) || + (!(this.horizontal) && pos.x == this.position.x && pos.y >= this.position.y && pos.y < this.position.y + size)) { + return true; + } + return false; + } + + public HitResponseType shootOnShip(Point pos) { + if (this.isShipOnPos(pos)) { + hitsOnMe++; + if (hitsOnMe >= size) { + this.sunk = true; + return HitResponseType.SUNK; + } else { + return HitResponseType.HIT; + } + } else { + return HitResponseType.MISS; + } + } + + public boolean isSunk() { + return sunk; + } +} diff --git a/src/SpecificAiPlayerEasy.java b/src/SpecificAiPlayerEasy.java new file mode 100644 index 0000000..0bcce8c --- /dev/null +++ b/src/SpecificAiPlayerEasy.java @@ -0,0 +1,7 @@ + +public class SpecificAiPlayer1 extends AiPlayer{ + + public SpecificAiPlayer1(int size) { + super(size); + } +} diff --git a/src/SpecificAiPlayerHard.java b/src/SpecificAiPlayerHard.java new file mode 100644 index 0000000..0bcce8c --- /dev/null +++ b/src/SpecificAiPlayerHard.java @@ -0,0 +1,7 @@ + +public class SpecificAiPlayer1 extends AiPlayer{ + + public SpecificAiPlayer1(int size) { + super(size); + } +} diff --git a/src/SpecificAiPlayerMedium.java b/src/SpecificAiPlayerMedium.java new file mode 100644 index 0000000..b808ff9 --- /dev/null +++ b/src/SpecificAiPlayerMedium.java @@ -0,0 +1,65 @@ + +public class SpecificAiPlayerMedium extends AiPlayer{ + + private List hitsQueue = new ArrayList<>(); + + public SpecificAiPlayerMedium(int size) { + super(size); + } + + @Override + public void AiShoot() { + Point nextShot = ComputeNextShot(); + // Shoot at the enemy and receive the hit response + enemy.receiveShoot(nextShot); + + HitResponse hitResponse = enemy.board.getHitResponsOnPoint(nextShot) + // If it's a hit or sunk, add adjacent cells to the hitsQueue + if (hitResponse.getHitResponse() == HitResponseType.HIT) { + addAdjacentPoints(nextShot); + } + } + + public Point ComputeNextShot() { + Point nextShot; + + // Prioritize shots from the hitsQueue + while (!hitsQueue.isEmpty()) { + nextShot = hitsQueue.remove(0); + if (!alreadyShot(nextShot)) { + return nextShot; + } + } + + // If hitsQueue is empty, pick a random point that hasn't been shot + do { + nextShot = RandomPoint(); + } while (alreadyShot(nextShot)); + + return nextShot; + } + + private void addAdjacentPoints(Point point) { + int x = point.x; + int y = point.y; + + // Possible adjacent positions (up, down, left, right) + Point[] adjacentPoints = { + new Point(x, y - 1), + new Point(x, y + 1), + new Point(x - 1, y), + new Point(x + 1, y) + }; + + for (Point p : adjacentPoints) { + if (isValidPoint(p) && !alreadyShot(p) && !hitsQueue.contains(p)) { + hitsQueue.add(p); + } + } + } + + private boolean isValidPoint(Point point) { + return point.x >= 0 && point.x < board.getSize() && + point.y >= 0 && point.y < board.getSize(); + } +} \ No newline at end of file diff --git a/src/coinToss.java b/src/coinToss.java new file mode 100644 index 0000000..87ed50b --- /dev/null +++ b/src/coinToss.java @@ -0,0 +1,39 @@ +import javax.swing.*; +import java.awt.*; + +public class coinToss extends JPanel { + private int reihenfolge = 1; // 1 = Spieler 1 fängt an, 0 = Spieler 2 fängt an + private Timer timer; + private JLabel infoLabel; + + // Konstruktor + public coinToss(MainFrame frame) { + setLayout(new BorderLayout()); + + // Info-Label für den Anzeigetext + infoLabel = new JLabel("", SwingConstants.CENTER); + infoLabel.setFont(new Font("Arial", Font.BOLD, 24)); + add(infoLabel, BorderLayout.CENTER); + + // Bestimme den Anfangstext basierend auf der "reihenfolge" Variable + if (reihenfolge == 1) { + infoLabel.setText("Du fängst an, mach dich bereit..."); + } else { + infoLabel.setText("Dein Gegner fängt an, mach dich bereit..."); + } + + // Erster Timer, der den Text nach 3 Sekunden auf "Es geht Los!" setzt + /*timer = new Timer(3000, e -> { + infoLabel.setText("Es geht Los!"); + + // Zweiter Timer, der nach weiteren 3 Sekunden zum Hauptmenü zurückkehrt + Timer backToMenuTimer = new Timer(3000, ev -> { + frame.showPanel("MainMenu"); + }); + //backToMenuTimer.setRepeats(false); // Timer nur einmal ausführen + backToMenuTimer.start(); + }); + //timer.setRepeats(false); // Erster Timer soll nur einmal ausgeführt werden + timer.start();*/ + } +} diff --git a/src/startLocalGame.java b/src/startLocalGame.java index 66212b4..8dcc513 100644 --- a/src/startLocalGame.java +++ b/src/startLocalGame.java @@ -1,29 +1,28 @@ import javax.swing.*; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class startLocalGame { +public class startLocalGame extends JPanel { // Funktionshilfen int semesterCounter = 1; // Semester Counter Label + String leftPlayerNickname = "Spieler 1"; + String rightPlayerNickname = "Spieler 2"; // Grafiken ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png"); ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png"); ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png"); - // Frame - JFrame frame = new JFrame("Lokales Spiel"); - - // Labels + // Labels und Buttons JLabel frameTitle = new JLabel("Lokales Spiel"); - JLabel semesterlable = new JLabel("Semester"); + JLabel semesterLabel = new JLabel("Semester"); JLabel leftPlayerName = new JLabel("Name"); JLabel rightPlayerName = new JLabel("KI-Level"); JLabel leftPlayerIcon = new JLabel(humanPlayerIcon); JLabel rightPlayerIcon = new JLabel(aiPlayerIcon); JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter)); - // Buttons JButton backButton = new JButton(backButtonIcon); JButton leftPlayerLeftButton = new JButton("<-"); JButton leftPlayerRightButton = new JButton("->"); @@ -33,141 +32,150 @@ public class startLocalGame { JButton rightPlayerRightButton = new JButton("->"); JButton startButton = new JButton("Start!"); - // Textfelder JTextField leftPlayerTextField = new JTextField(20); JTextField rightPlayerTextField = new JTextField(20); - startLocalGame() { - // Erstelle Frame - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(1500, 1000); - // Layout Manager (absolute ositionierung) - frame.setLayout(null); + // Methode zur Erstellung des Panels + startLocalGame(MainFrame frame) { + setLayout(null); // Stelle das Layout des Panels ein - // Erstelle Label + // Setze Komponentenpositionen frameTitle.setBounds(20, 20, 200, 30); - frame.add(frameTitle); + add(frameTitle); - semesterlable.setBounds(700, 300, 200, 30); - frame.add(semesterlable); + semesterLabel.setBounds(700, 300, 200, 30); + add(semesterLabel); leftPlayerName.setBounds(50, 625, 200, 30); - frame.add(leftPlayerName); + add(leftPlayerName); rightPlayerName.setBounds(1200, 625, 200, 30); - frame.add(rightPlayerName); + add(rightPlayerName); leftPlayerIcon.setBounds(75, 400, 200, 128); - frame.add(leftPlayerIcon); + add(leftPlayerIcon); rightPlayerIcon.setBounds(1225, 400, 200, 128); - frame.add(rightPlayerIcon); + add(rightPlayerIcon); - semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons + semesterCounterLabel.setBounds(725, 475, 50, 50); semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER); - frame.add(semesterCounterLabel); + add(semesterCounterLabel); - // Erstellt Buttons backButton.setBounds(1380, 20, 80, 80); - frame.add(backButton); + add(backButton); leftPlayerLeftButton.setBounds(50, 450, 50, 50); - frame.add(leftPlayerLeftButton); + add(leftPlayerLeftButton); leftPlayerRightButton.setBounds(250, 450, 50, 50); - frame.add(leftPlayerRightButton); + add(leftPlayerRightButton); semesterUpButton.setBounds(725, 400, 50, 50); - frame.add(semesterUpButton); + add(semesterUpButton); semesterDownButton.setBounds(725, 550, 50, 50); - frame.add(semesterDownButton); + add(semesterDownButton); rightPlayerLeftButton.setBounds(1200, 450, 50, 50); - frame.add(rightPlayerLeftButton); + add(rightPlayerLeftButton); rightPlayerRightButton.setBounds(1400, 450, 50, 50); - frame.add(rightPlayerRightButton); + add(rightPlayerRightButton); startButton.setBounds(700, 750, 100, 50); - frame.add(startButton); + add(startButton); - // Erstellt Textfelder leftPlayerTextField.setBounds(50, 650, 250, 50); - frame.add(leftPlayerTextField); + leftPlayerTextField.setText(leftPlayerNickname); + add(leftPlayerTextField); rightPlayerTextField.setBounds(1200, 650, 250, 50); - frame.add(rightPlayerTextField); + rightPlayerTextField.setText(rightPlayerNickname); + add(rightPlayerTextField); - // ActionListener für die Buttons - semesterUpButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (semesterCounter < 6) { - semesterCounter++; - semesterCounterLabel.setText(String.valueOf(semesterCounter)); - } + // ActionListener für Buttons + semesterUpButton.addActionListener(e -> { + if (semesterCounter < 6) { + semesterCounter++; + semesterCounterLabel.setText(String.valueOf(semesterCounter)); } }); - semesterDownButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (semesterCounter > 1) { // Verhindert, dass der Wert unter 1 fällt - semesterCounter--; - semesterCounterLabel.setText(String.valueOf(semesterCounter)); - } + semesterDownButton.addActionListener(e -> { + if (semesterCounter > 1) { + semesterCounter--; + semesterCounterLabel.setText(String.valueOf(semesterCounter)); } }); leftPlayerLeftButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png"); - if(leftPlayerIcon.getIcon()==humanPlayerIcon) { - leftPlayerIcon.setIcon(aiPlayerIcon); - }else{ - leftPlayerIcon.setIcon(humanPlayerIcon); - } + toggleLeftPlayerIcon(); + updateTextFields(); } }); leftPlayerRightButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png"); - if(leftPlayerIcon.getIcon()==humanPlayerIcon) { - leftPlayerIcon.setIcon(aiPlayerIcon); - }else{ - leftPlayerIcon.setIcon(humanPlayerIcon); - } + toggleLeftPlayerIcon(); + updateTextFields(); } }); rightPlayerLeftButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png"); - if(rightPlayerIcon.getIcon()==humanPlayerIcon) { - rightPlayerIcon.setIcon(aiPlayerIcon); - }else{ - rightPlayerIcon.setIcon(humanPlayerIcon); - } + toggleRightPlayerIcon(); + updateTextFields(); } }); rightPlayerRightButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ImageIcon MuteIcon = new ImageIcon("Grafik/sound button muted.png"); - if(rightPlayerIcon.getIcon()==humanPlayerIcon) { - rightPlayerIcon.setIcon(aiPlayerIcon); - }else{ - rightPlayerIcon.setIcon(humanPlayerIcon); - } + toggleRightPlayerIcon(); + updateTextFields(); } }); - frame.setVisible(true); + backButton.addActionListener(e -> frame.showPanel("MainMenu")); + + startButton.addActionListener(e -> frame.showPanel("coinToss")); // TODO ECHTE FUNKTION EINFÜGEN } -} + + private void toggleLeftPlayerIcon() { + if (leftPlayerIcon.getIcon() == humanPlayerIcon) { + leftPlayerIcon.setIcon(aiPlayerIcon); + } else { + leftPlayerIcon.setIcon(humanPlayerIcon); + } + } + + private void toggleRightPlayerIcon() { + if (rightPlayerIcon.getIcon() == humanPlayerIcon) { + rightPlayerIcon.setIcon(aiPlayerIcon); + } else { + rightPlayerIcon.setIcon(humanPlayerIcon); + } + } + + // Methode zum Aktualisieren der Textfelder basierend auf den ausgewählten Icons + private void updateTextFields() { + // Linker Spieler + if (leftPlayerIcon.getIcon() == humanPlayerIcon) { + leftPlayerTextField.setText("Spieler 1"); + } else { + leftPlayerTextField.setText("Leicht"); + } + + // Rechter Spieler + if (rightPlayerIcon.getIcon() == humanPlayerIcon) { + rightPlayerTextField.setText("Spieler 2"); + } else { + rightPlayerTextField.setText("Leicht"); + } + } +} \ No newline at end of file diff --git a/src/startMultiplayerGame.java b/src/startMultiplayerGame.java new file mode 100644 index 0000000..4598994 --- /dev/null +++ b/src/startMultiplayerGame.java @@ -0,0 +1,131 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class startMultiplayerGame extends JPanel { + // Funktionshilfen + int semesterCounter = 1; // Semester Counter Label + String PlayerNickname = "Spieler 1"; + + // Grafiken + ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png"); + ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png"); + ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png"); + + // Labels + JLabel frameTitle = new JLabel("Multiplayer Spiel"); + JLabel semesterLabel = new JLabel("Semester"); + JLabel PlayerName = new JLabel("Name"); + JLabel PlayerIcon = new JLabel(humanPlayerIcon); + JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter)); + + // Buttons + JButton backButton = new JButton(backButtonIcon); + JButton PlayerLeftButton = new JButton("<-"); + JButton PlayerRightButton = new JButton("->"); + JButton semesterUpButton = new JButton("^"); + JButton semesterDownButton = new JButton("v"); + JButton createGameButton = new JButton("Spiel erstellen"); + JButton joinGameButton = new JButton("Spiel beitreten"); + + // Textfelder + JTextField PlayerTextField = new JTextField(20); + + // Konstruktor + startMultiplayerGame(MainFrame frame) { + setLayout(null); // Setze das Layout für das Panel auf null + + // Setze Komponentenpositionen und füge sie dem Panel hinzu + frameTitle.setBounds(20, 20, 200, 30); + add(frameTitle); + + semesterLabel.setBounds(700, 300, 200, 30); + add(semesterLabel); + + PlayerName.setBounds(50, 625, 200, 30); + add(PlayerName); + + PlayerIcon.setBounds(75, 400, 200, 128); + add(PlayerIcon); + + semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons + semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER); + add(semesterCounterLabel); + + // Füge Buttons hinzu und setze ihre Positionen + backButton.setBounds(1380, 20, 80, 80); + add(backButton); + + PlayerLeftButton.setBounds(50, 450, 50, 50); + add(PlayerLeftButton); + + PlayerRightButton.setBounds(250, 450, 50, 50); + add(PlayerRightButton); + + semesterUpButton.setBounds(725, 400, 50, 50); + add(semesterUpButton); + + semesterDownButton.setBounds(725, 550, 50, 50); + add(semesterDownButton); + + joinGameButton.setBounds(1100, 350, 200, 50); + add(joinGameButton); + + createGameButton.setBounds(1100, 550, 200, 50); + add(createGameButton); + + // Füge das Textfeld hinzu + PlayerTextField.setBounds(50, 650, 250, 50); + PlayerTextField.setText(PlayerNickname); + add(PlayerTextField); + + // ActionListener für Buttons + // SEMESTERBUTTONS + semesterUpButton.addActionListener(e -> { + if (semesterCounter < 6) { + semesterCounter++; + semesterCounterLabel.setText(String.valueOf(semesterCounter)); + } + }); + + semesterDownButton.addActionListener(e -> { + if (semesterCounter > 1) { + semesterCounter--; + semesterCounterLabel.setText(String.valueOf(semesterCounter)); + } + }); + + // PLAYERTOGGLEBUTTONS + PlayerLeftButton.addActionListener(e -> { + toggleLeftPlayerIcon(); + updateTextFields(); + }); + + PlayerRightButton.addActionListener(e -> { + toggleLeftPlayerIcon(); + updateTextFields(); + }); + + // ActionListener für den "Back" Button, um zum vorherigen Panel zurückzukehren + + backButton.addActionListener(e -> frame.showPanel("MainMenu")); + joinGameButton.addActionListener(e -> frame.showPanel("JoinGame")); + } + + // TOGGLE METHODEN + private void toggleLeftPlayerIcon() { + if (PlayerIcon.getIcon() == humanPlayerIcon) { + PlayerIcon.setIcon(aiPlayerIcon); + } else { + PlayerIcon.setIcon(humanPlayerIcon); + } + } + + private void updateTextFields() { + if (PlayerIcon.getIcon() == humanPlayerIcon) { + PlayerTextField.setText(PlayerNickname); + } else { + PlayerTextField.setText("Leicht"); + } + } +}