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
This commit is contained in:
Fleischer Hanno hanno.fleischer@unibw.de
2024-10-02 11:01:13 +02:00
parent 71a4ac8d12
commit f759eddda1
3 changed files with 7 additions and 3 deletions

View File

@@ -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));
}
}

View File

@@ -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));
}
/**

View File

@@ -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);