Aufgabe 1-10 bearbeitet. Musik (Piratenmusik.ogg) hinzugefügt. Schiebregler hinzugefügt (VolumeLSider); update Methode überschrieben; Lemur Doc gelesen

This commit is contained in:
Dennis_Malkmus
2024-10-13 23:45:26 +02:00
parent f47cda7dc4
commit f5dc8a0f05
66 changed files with 476090 additions and 4 deletions

View File

@@ -72,7 +72,7 @@ class BattleState extends ClientState {
* @return the map (either the opponent's or player's own map) that is affected by the shot
*/
private ShipMap affectedMap(EffectMessage msg) {
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOpponentMap();
}
/**

View File

@@ -8,6 +8,7 @@
package pp.battleship.game.server;
import pp.battleship.BattleshipConfig;
import pp.battleship.model.Battleship;
import pp.battleship.model.ShipMap;
/**
@@ -51,4 +52,9 @@ public class Player {
public ShipMap getMap() {
return map;
}
}

View File

@@ -168,11 +168,19 @@ public class ServerGameLogic implements ClientInterpreter {
* @param ships the list of ships placed by the player
*/
void playerReady(Player player, List<Battleship> ships) {
// Überprüfe, ob die Karte des Spielers gültig ist
if (!player.getMap().isValid2(ships)) {
LOGGER.log(Level.ERROR, "Invalid map configuration for player {0}", player); //NON-NLS
return;
}
if (!readyPlayers.add(player)) {
LOGGER.log(Level.ERROR, "{0} was already ready", player); //NON-NLS
return;
}
ships.forEach(player.getMap()::add);
if (readyPlayers.size() == 2) {
for (Player p : players)
send(p, new StartBattleMessage(p == activePlayer));
@@ -217,4 +225,5 @@ public class ServerGameLogic implements ClientInterpreter {
}
}
}
}

View File

@@ -10,6 +10,7 @@ package pp.battleship.model;
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 class ShipMap {
*/
public void remove(Item item) {
items.remove(item);
notifyListeners(new ItemAddedEvent(item, this));
notifyListeners(new ItemRemovedEvent(item, this));
}
/**
@@ -184,6 +185,35 @@ public class ShipMap {
getShips().filter(s -> s != ship).noneMatch(ship::collidesWith);
}
//ich brauche eine zweite isValid Methode, da in der ServerGameLogic eine Liste von Schiffen und
//kein einzelnes übergeben wird. Da ich keinen Bock habe, dort eine for Schleife zu bauen, mache ich
//das hier.
public boolean isValid2(List<Battleship> ships) {
// Überprüfe, ob jedes Schiff innerhalb der Spielfeldgrenzen liegt
//istWithinBounds kommt 20 Zeilen weiter unten. Minas gab uns schon Code vor, um das ganze zu lösen.
for (Battleship ship : ships) {
if (!isShipWithinBounds(ship)) {
return false; // Ein Schiff liegt außerhalb der Grenzen
}
}
// Überprüfe, ob sich keine Schiffe überlappen
for (int i = 0; i < ships.size(); i++) {
Battleship ship1 = ships.get(i);
for (int j = i + 1; j < ships.size(); j++) {
Battleship ship2 = ships.get(j);
if (ship1.collidesWith(ship2)) {
return false; // Schiffe überlappen sich
}
}
}
return true; // Alle Schiffe sind korrekt platziert
}
private boolean isShipWithinBounds(Battleship ship) {
return isValid(ship.getMinX(), ship.getMinY()) &&
isValid(ship.getMaxX(), ship.getMaxY());
}
/**
* Finds a battleship at the specified coordinates.
*