Klasse ShipMap

java.lang.Object
pp.battleship.model.ShipMap

public class ShipMap extends Object
Represents a rectangular map that holds ships and registers shots fired. It also supports event notification for game state changes such as item addition or removal. Valid positions on this map have x-coordinates in the range of 0 to width-1 and y-coordinates in the range of 0 to height-1.
Siehe auch:
  • Konstruktorübersicht

    Konstruktoren
    Konstruktor
    Beschreibung
    ShipMap(int width, int height, GameEventBroker eventBroker)
    Constructs an empty map with the given dimensions.
  • Methodenübersicht

    Modifizierer und Typ
    Methode
    Beschreibung
    void
    Adds a battleship to the map and triggers an item addition event.
    void
    add(Shot shot)
    Registers a shot on the map, updates the state of the affected ship (if any), and triggers an item addition event.
    void
    Removes all items from the map and triggers corresponding removal events for each.
    findShipAt(int x, int y)
    Finds a battleship at the specified coordinates.
    Finds a battleship at the specified position.
    int
    Returns the height (number of rows) of the map.
    Returns an unmodifiable list of all items currently on the map.
    Returns a list of all remaining battleships that have not been destroyed.
    Returns a stream of all battleships currently on the map.
    Returns a stream of all shots fired on the map.
    int
    Returns the width (number of columns) of the map.
    boolean
    isValid(int x, int y)
    Checks if the specified coordinates are within the map boundaries.
    boolean
    Checks if the given ship is in a valid position (within the map bounds and non-colliding with other ships).
    boolean
    Validates whether the specified position is within the map boundaries.
    void
    remove(Item item)
    Removes an item from the map and triggers an item removal event.
    Returns a string representation of the ship map.

    Von Klasse geerbte Methoden java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Konstruktordetails

    • ShipMap

      public ShipMap(int width, int height, GameEventBroker eventBroker)
      Constructs an empty map with the given dimensions. The specified event broker will handle the notification of changes in the map state, such as adding or removing items. Passing null as the event broker is allowed, but in that case, no notifications will occur.
      Parameter:
      width - the number of columns (width) of the map
      height - the number of rows (height) of the map
      eventBroker - the event broker used for notifying listeners, or null if event distribution is not needed
  • Methodendetails

    • add

      public void add(Battleship ship)
      Adds a battleship to the map and triggers an item addition event.
      Parameter:
      ship - the battleship to be added to the map
    • add

      public void add(Shot shot)
      Registers a shot on the map, updates the state of the affected ship (if any), and triggers an item addition event.
      Parameter:
      shot - the shot to be registered on the map
    • remove

      public void remove(Item item)
      Removes an item from the map and triggers an item removal event.
      Parameter:
      item - the item to be removed from the map
    • clear

      public void clear()
      Removes all items from the map and triggers corresponding removal events for each.
    • getShips

      public Stream<Battleship> getShips()
      Returns a stream of all battleships currently on the map.
      Gibt zurück:
      a stream of battleships
    • getRemainingShips

      public List<Battleship> getRemainingShips()
      Returns a list of all remaining battleships that have not been destroyed.
      Gibt zurück:
      a list of remaining battleships
    • getShots

      public Stream<Shot> getShots()
      Returns a stream of all shots fired on the map.
      Gibt zurück:
      a stream of shots
    • getItems

      public List<Item> getItems()
      Returns an unmodifiable list of all items currently on the map.
      Gibt zurück:
      an unmodifiable list of all items
    • getWidth

      public int getWidth()
      Returns the width (number of columns) of the map.
      Gibt zurück:
      the width of the map
    • getHeight

      public int getHeight()
      Returns the height (number of rows) of the map.
      Gibt zurück:
      the height of the map
    • isValid

      public boolean isValid(Battleship ship)
      Checks if the given ship is in a valid position (within the map bounds and non-colliding with other ships).
      Parameter:
      ship - the battleship to validate
      Gibt zurück:
      true if the ship's position is valid, false otherwise
    • findShipAt

      public Battleship findShipAt(int x, int y)
      Finds a battleship at the specified coordinates.
      Parameter:
      x - the x-coordinate of the position
      y - the y-coordinate of the position
      Gibt zurück:
      the ship at the specified coordinates, or null if none is found
    • findShipAt

      public Battleship findShipAt(IntPosition position)
      Finds a battleship at the specified position. This is a convenience method.
      Parameter:
      position - the position within the map
      Gibt zurück:
      the ship at the specified position, or null if none is found
    • isValid

      public boolean isValid(IntPosition pos)
      Validates whether the specified position is within the map boundaries.
      Parameter:
      pos - the position to validate
      Gibt zurück:
      true if the position is within the map, false otherwise
    • isValid

      public boolean isValid(int x, int y)
      Checks if the specified coordinates are within the map boundaries.
      Parameter:
      x - the x-coordinate to validate
      y - the y-coordinate to validate
      Gibt zurück:
      true if the coordinates are valid, false otherwise
    • toString

      public String toString()
      Returns a string representation of the ship map.
      Setzt außer Kraft:
      toString in Klasse Object
      Gibt zurück:
      a string representation of the ship map