From d9a93952d2ccc678242bd6ca8992c815c8813d18 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Sun, 15 Dec 2024 15:59:26 +0100 Subject: [PATCH] PEW PEW --- src/Board.java | 2 +- src/BoardDisplay.java | 38 ++++++++++++++++++++++++++++++++++++-- src/GameBoard.java | 4 ++-- src/LocalPlayer.java | 2 ++ src/Player.java | 11 +++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/Board.java b/src/Board.java index f0ca737..08fb3a2 100644 --- a/src/Board.java +++ b/src/Board.java @@ -21,7 +21,7 @@ public class Board { 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++) { + for (int ii = 0; ii < this.ships.size(); ii++) { if (!this.ships.get(ii).isSunk()) { response.setType(type); this.addHits(response); diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index 7d10ab1..749ae84 100644 --- a/src/BoardDisplay.java +++ b/src/BoardDisplay.java @@ -17,6 +17,7 @@ public class BoardDisplay extends JPanel { private Player player; private boolean horizontal = false; private List shipButtonList; + private boolean enemyBoard; public void addShipButton(ShipButton button) { shipButtonList.add(button); @@ -33,13 +34,14 @@ public class BoardDisplay extends JPanel { * @param gridSize * @param player */ - public BoardDisplay(int gridSize, Player player) { + public BoardDisplay(int gridSize, Player player, boolean enemyBoard) { super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Spalte this.fields = new JButton[gridSize][gridSize]; //this.ships = new ArrayList<>(); this.shipButtonList = new ArrayList<>(); this.player = player; this.gridSize = gridSize; + this.enemyBoard = enemyBoard; System.out.println("Name in Boarddisplay: " + player.getName());//Testausgabe // Erstellung von Spielfeld for (int i = 0; i <= gridSize; i++) { @@ -141,7 +143,12 @@ public class BoardDisplay extends JPanel { * @param o Die Koordinaten des Klicks. */ private void handleFieldClick(Point o) { - if (!this.currentShip.setPosition(o, horizontal, player.getBoard().getShips(),this.gridSize)) { + System.out.println("CLICK " + o); + if (!this.enemyBoard && !this.player.isGameRunning() && !this.player.isReady()) { + this.currentShip.setPosition(o, horizontal, player.getBoard().getShips(),this.gridSize); + } + if (this.enemyBoard && this.player.isGameRunning() && this.player.enemy.myTurn) { + this.player.enemy.shoot(o); } paintFields(); //if(this.currentShip.isPlaced()){ @@ -159,6 +166,20 @@ public class BoardDisplay extends JPanel { if(fields[i][j] == null) { continue; } + if (this.enemyBoard) { + // enemy board only accessible if game is running AND it's "enemy's" so local player's turn + if (!this.player.isGameRunning() || !this.player.enemy.myTurn) { + fields[i][j].setEnabled(false); + } else { + fields[i][j].setEnabled(true); + } + } else { + if (this.player.isGameRunning() || this.player.isReady()) { + fields[i][j].setEnabled(false); + } else { + fields[i][j].setEnabled(true); + } + } fields[i][j].setBackground(Color.BLUE); for(Ship ship: player.getBoard().getShips()) { if(ship.isShipOnPos(new Point(i,j))) { @@ -166,6 +187,19 @@ public class BoardDisplay extends JPanel { break; } } + HitResponse hit = this.player.getBoard().getHitResponsOnPoint(new Point(i, j)); + if (hit != null) { + switch (hit.getType()) { + case HIT: + case SUNK: + case VICTORY: + fields[i][j].setBackground(Color.ORANGE); + break; + case MISS: + fields[i][j].setBackground(Color.CYAN); + break; + } + } } } for( ShipButton shipButton: shipButtonList) { diff --git a/src/GameBoard.java b/src/GameBoard.java index 0f22fa7..393bcdc 100644 --- a/src/GameBoard.java +++ b/src/GameBoard.java @@ -111,8 +111,8 @@ public class GameBoard extends JPanel { // Spielfelder erstellen (eigenes und gegnerisches) // Spielfelder werden in BoardDisplay erstellt int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds - this.ownBoardPanel = new BoardDisplay(gridSize,p1); - this.opponentBoardPanel = new BoardDisplay(gridSize, p2); + this.ownBoardPanel = new BoardDisplay(gridSize,p1, false); + this.opponentBoardPanel = new BoardDisplay(gridSize, p2, true); // Panel für das Kontext-Text-Feld JPanel headerPanel = new JPanel(); diff --git a/src/LocalPlayer.java b/src/LocalPlayer.java index 2b67b44..2c6980c 100644 --- a/src/LocalPlayer.java +++ b/src/LocalPlayer.java @@ -24,6 +24,7 @@ public class LocalPlayer extends Player { case MISS -> this.myTurn = true; case VICTORY -> System.out.println("Game Over"); //TODO Was halt bei victory passiert ist hier wurder verloheren } + GameController.getMainFrame().refreshGameBoard(); } @Override @@ -34,6 +35,7 @@ public class LocalPlayer extends Player { case MISS -> this.myTurn = false; case VICTORY -> System.out.println("Win"); // TODO was halt beim victory passier ist hier wurde gewonnen } + GameController.getMainFrame().refreshGameBoard(); } @Override diff --git a/src/Player.java b/src/Player.java index 7595594..f36702d 100644 --- a/src/Player.java +++ b/src/Player.java @@ -6,6 +6,7 @@ public abstract class Player { protected String name; protected Board board; protected Boolean myCoin; + protected boolean gameRunning; protected boolean sentCoin; @@ -16,6 +17,7 @@ public abstract class Player { this.hasReceivedCoin = false; this.sentCoin = false; this.myTurn = false; + this.gameRunning = false; } public void createBoard(int size) { @@ -62,8 +64,17 @@ public abstract class Player { if (this.myTurn) { this.beginTurn(); } + this.gameRunning = true; GameController.getMainFrame().refreshGameBoard(); } public abstract void receiveCoin(boolean coin); + + public boolean isGameRunning() { + return this.gameRunning; + } + + public boolean isReady() { + return this.sentCoin; + } } -- 2.40.1