From f759eddda195b4b4255859c4b1497975e7e4e58b Mon Sep 17 00:00:00 2001 From: "Fleischer Hanno hanno.fleischer@unibw.de" Date: Wed, 2 Oct 2024 11:01:13 +0200 Subject: [PATCH] solution exercise 7 edited in BattleState.java the receivedMsg() method so that if the game moves to the game over state the remaining opponent ships will be added to the list of the opponenets instead of your own list. edited the ShipMap.java so that when the notifylisteners is called for removing an object it will be handled as an ItemRemovedEvent instead of an ItemAddedEvent --- .../src/main/java/pp/battleship/game/client/BattleState.java | 5 +++-- .../model/src/main/java/pp/battleship/model/ShipMap.java | 3 ++- .../pp/battleship/game/client/ClientGame1Player1Test.java | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java index fa8d731d..e67c3b88 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java @@ -57,10 +57,11 @@ public void receivedEffect(EffectMessage msg) { myTurn = msg.isMyTurn(); logic.setInfoText(msg.getInfoTextKey()); affectedMap(msg).add(msg.getShot()); - if (destroyedOpponentShip(msg)) + if (destroyedOpponentShip(msg)) { logic.getOpponentMap().add(msg.getDestroyedShip()); + } if (msg.isGameOver()) { - msg.getRemainingOpponentShips().forEach(logic.getOwnMap()::add); + msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add); logic.setState(new GameOverState(logic)); } } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java index bb73d6b0..5a162785 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java @@ -10,6 +10,7 @@ import pp.battleship.notification.GameEvent; import pp.battleship.notification.GameEventBroker; import pp.battleship.notification.ItemAddedEvent; +import pp.battleship.notification.ItemRemovedEvent; import java.util.ArrayList; import java.util.Collections; @@ -97,7 +98,7 @@ public void add(Shot shot) { */ public void remove(Item item) { items.remove(item); - notifyListeners(new ItemAddedEvent(item, this)); + notifyListeners(new ItemRemovedEvent(item, this)); } /** diff --git a/Projekte/battleship/model/src/test/java/pp/battleship/game/client/ClientGame1Player1Test.java b/Projekte/battleship/model/src/test/java/pp/battleship/game/client/ClientGame1Player1Test.java index 3cc236bd..d67884b0 100644 --- a/Projekte/battleship/model/src/test/java/pp/battleship/game/client/ClientGame1Player1Test.java +++ b/Projekte/battleship/model/src/test/java/pp/battleship/game/client/ClientGame1Player1Test.java @@ -222,6 +222,7 @@ public void testClient() { assertEquals(p(1, 5), shootMsg.getPosition()); clientLogic.received(EffectMessage.shipDestroyed(true, p(1, 5), new Battleship(2, 1, 5, DOWN))); assertEquals("its.your.turn", infoTexts.poll()); + ships = clientLogic.getOpponentMap().getShips().toList(); assertEquals(1, ships.size()); checkShip(ships.get(0), 2, 1, 5, DOWN, NORMAL); @@ -234,6 +235,7 @@ public void testClient() { assertEquals("you.lost.the.game", infoTexts.poll()); ships = clientLogic.getOpponentMap().getShips().toList(); assertEquals(2, ships.size()); + checkShip(ships.get(0), 2, 1, 5, DOWN, NORMAL); checkShip(ships.get(1), 1, 1, 2, RIGHT, NORMAL);