Package pp.battleship.model
Klasse ShipMap
java.lang.Object
pp.battleship.model.ShipMap
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
KonstruktorenKonstruktorBeschreibungShipMap(int width, int height, GameEventBroker eventBroker) Constructs an empty map with the given dimensions. -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungvoidadd(Battleship ship) Adds a battleship to the map and triggers an item addition event.voidRegisters a shot on the map, updates the state of the affected ship (if any), and triggers an item addition event.voidclear()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.findShipAt(IntPosition position) Finds a battleship at the specified position.intReturns the height (number of rows) of the map.getItems()Returns an unmodifiable list of all items currently on the map.Returns a list of all remaining battleships that have not been destroyed.getShips()Returns a stream of all battleships currently on the map.getShots()Returns a stream of all shots fired on the map.intgetWidth()Returns the width (number of columns) of the map.booleanisValid(int x, int y) Checks if the specified coordinates are within the map boundaries.booleanisValid(Battleship ship) Checks if the given ship is in a valid position (within the map bounds and non-colliding with other ships).booleanisValid(IntPosition pos) Validates whether the specified position is within the map boundaries.voidRemoves an item from the map and triggers an item removal event.toString()Returns a string representation of the ship map.
-
Konstruktordetails
-
ShipMap
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 mapheight- the number of rows (height) of the mapeventBroker- the event broker used for notifying listeners, or null if event distribution is not needed
-
-
Methodendetails
-
add
Adds a battleship to the map and triggers an item addition event.- Parameter:
ship- the battleship to be added to the map
-
add
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
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
Returns a stream of all battleships currently on the map.- Gibt zurück:
- a stream of battleships
-
getRemainingShips
Returns a list of all remaining battleships that have not been destroyed.- Gibt zurück:
- a list of remaining battleships
-
getShots
Returns a stream of all shots fired on the map.- Gibt zurück:
- a stream of shots
-
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
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
Finds a battleship at the specified coordinates.- Parameter:
x- the x-coordinate of the positiony- the y-coordinate of the position- Gibt zurück:
- the ship at the specified coordinates, or null if none is found
-
findShipAt
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
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 validatey- the y-coordinate to validate- Gibt zurück:
- true if the coordinates are valid, false otherwise
-
toString
Returns a string representation of the ship map.
-