Klasse Battleship

java.lang.Object
pp.battleship.model.Battleship
Alle implementierten Schnittstellen:
Item

public class Battleship extends Object implements Item
Represents a battleship in the game. A battleship is characterized by its length, position, rotation, and status. It can be moved, rotated, and hit during the game. This class also provides methods to check for collisions with other ships and to determine whether the battleship has been destroyed.
  • Verschachtelte Klassen - Übersicht

    Verschachtelte Klassen
    Modifizierer und Typ
    Klasse
    Beschreibung
    static enum 
    Enumeration representing the different statuses a battleship can have during the game.
  • Konstruktorübersicht

    Konstruktoren
    Konstruktor
    Beschreibung
    Battleship(int length, int x, int y, Rotation rot)
    Constructs a new Battleship with the specified length, position, and rotation.
  • Methodenübersicht

    Modifizierer und Typ
    Methode
    Beschreibung
    <T> T
    accept(Visitor<T> visitor)
    Accepts a visitor that returns a value of type T.
    void
    Accepts a visitor that does not return a value.
    boolean
    Checks whether this battleship collides with another battleship.
    boolean
    contains(int x, int y)
    Checks whether the specified position is covered by the battleship.
    boolean
    Checks whether the specified position is covered by the battleship.
    Returns the positions of this battleship that have been hit.
    int
    Returns the length of the battleship.
    int
    Returns the maximum x-coordinate that the battleship occupies based on its current position and rotation.
    int
    Returns the maximum y-coordinate that the battleship occupies based on its current position and rotation.
    int
    Returns the minimum x-coordinate that the battleship occupies based on its current position and rotation.
    int
    Returns the minimum y-coordinate that the battleship occupies based on its current position and rotation.
    Returns the current rotation of the battleship.
    Returns the current status of the battleship.
    int
    Returns the current x-coordinate of the battleship's position.
    int
    Returns the current y-coordinate of the battleship's position.
    boolean
    hit(int x, int y)
    Attempts to hit the battleship at the specified position.
    boolean
    hit(IntPosition position)
    Attempts to hit the battleship at the specified position.
    boolean
    Determines if the battleship has been completely destroyed.
    void
    moveTo(int x, int y)
    Moves the battleship to the specified coordinates.
    void
    Moves the battleship to the specified position.
    void
    Rotates the battleship by 90 degrees clockwise.
    void
    Sets the rotation of the battleship.
    void
    Sets the status of the battleship.
    Returns a string representation of the battleship, including its length, position, and rotation.

    Von Klasse geerbte Methoden java.lang.Object

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

    • Battleship

      public Battleship(int length, int x, int y, Rotation rot)
      Constructs a new Battleship with the specified length, position, and rotation.
      Parameter:
      length - the length of the battleship
      x - the x-coordinate of the battleship's initial position
      y - the y-coordinate of the battleship's initial position
      rot - the rotation of the battleship
  • Methodendetails

    • getX

      public int getX()
      Returns the current x-coordinate of the battleship's position.
      Gibt zurück:
      the x-coordinate of the battleship
    • getY

      public int getY()
      Returns the current y-coordinate of the battleship's position.
      Gibt zurück:
      the y-coordinate of the battleship
    • moveTo

      public void moveTo(int x, int y)
      Moves the battleship to the specified coordinates.
      Parameter:
      x - the new x-coordinate of the battleship's position
      y - the new y-coordinate of the battleship's position
    • moveTo

      public void moveTo(IntPosition pos)
      Moves the battleship to the specified position.
      Parameter:
      pos - the new position of the battleship
    • getStatus

      public Battleship.Status getStatus()
      Returns the current status of the battleship.
      Gibt zurück:
      the status of the battleship
    • setStatus

      public void setStatus(Battleship.Status status)
      Sets the status of the battleship.
      Parameter:
      status - the new status to be set for the battleship
    • getLength

      public int getLength()
      Returns the length of the battleship.
      Gibt zurück:
      the length of the battleship
    • getMinX

      public int getMinX()
      Returns the minimum x-coordinate that the battleship occupies based on its current position and rotation.
      Gibt zurück:
      the minimum x-coordinate of the battleship
    • getMaxX

      public int getMaxX()
      Returns the maximum x-coordinate that the battleship occupies based on its current position and rotation.
      Gibt zurück:
      the maximum x-coordinate of the battleship
    • getMinY

      public int getMinY()
      Returns the minimum y-coordinate that the battleship occupies based on its current position and rotation.
      Gibt zurück:
      the minimum y-coordinate of the battleship
    • getMaxY

      public int getMaxY()
      Returns the maximum y-coordinate that the battleship occupies based on its current position and rotation.
      Gibt zurück:
      the maximum y-coordinate of the battleship
    • getRot

      public Rotation getRot()
      Returns the current rotation of the battleship.
      Gibt zurück:
      the rotation of the battleship
    • setRotation

      public void setRotation(Rotation rot)
      Sets the rotation of the battleship.
      Parameter:
      rot - the new rotation to be set for the battleship
    • rotated

      public void rotated()
      Rotates the battleship by 90 degrees clockwise.
    • hit

      public boolean hit(int x, int y)
      Attempts to hit the battleship at the specified position. If the position is part of the battleship, the hit is recorded.
      Parameter:
      x - the x-coordinate of the position to hit
      y - the y-coordinate of the position to hit
      Gibt zurück:
      true if the position is part of the battleship, false otherwise
      Siehe auch:
    • hit

      public boolean hit(IntPosition position)
      Attempts to hit the battleship at the specified position. If the position is part of the battleship, the hit is recorded. This is a convenience method for hit(int, int).
      Parameter:
      position - the position to hit
      Gibt zurück:
      true if the position is part of the battleship, false otherwise
    • getDamaged

      public Set<IntPoint> getDamaged()
      Returns the positions of this battleship that have been hit.
      Gibt zurück:
      a set of positions that have been hit
      Siehe auch:
    • contains

      public boolean contains(IntPosition pos)
      Checks whether the specified position is covered by the battleship. This method does not record a hit, only checks coverage. This is a convenience method for contains(int, int).
      Parameter:
      pos - the position to check
      Gibt zurück:
      true if the position is covered by the battleship, false otherwise
    • contains

      public boolean contains(int x, int y)
      Checks whether the specified position is covered by the battleship. This method does not record a hit, only checks coverage.
      Parameter:
      x - the x-coordinate of the position to check
      y - the y-coordinate of the position to check
      Gibt zurück:
      true if the position is covered by the battleship, false otherwise
    • isDestroyed

      public boolean isDestroyed()
      Determines if the battleship has been completely destroyed. A battleship is considered destroyed if all of its positions have been hit.
      Gibt zurück:
      true if the battleship is destroyed, false otherwise
      Siehe auch:
    • collidesWith

      public boolean collidesWith(Battleship other)
      Checks whether this battleship collides with another battleship. Two battleships collide if any of their occupied positions overlap.
      Parameter:
      other - the other battleship to check collision with
      Gibt zurück:
      true if the battleships collide, false otherwise
    • toString

      public String toString()
      Returns a string representation of the battleship, including its length, position, and rotation.
      Setzt außer Kraft:
      toString in Klasse Object
      Gibt zurück:
      a string representation of the battleship
    • accept

      public <T> T accept(Visitor<T> visitor)
      Accepts a visitor that returns a value of type T. This method is part of the Visitor design pattern.
      Angegeben von:
      accept in Schnittstelle Item
      Typparameter:
      T - the type of the value returned by the visitor
      Parameter:
      visitor - the visitor to accept
      Gibt zurück:
      the value returned by the visitor
    • accept

      public void accept(VoidVisitor visitor)
      Accepts a visitor that does not return a value. This method is part of the Visitor design pattern.
      Angegeben von:
      accept in Schnittstelle Item
      Parameter:
      visitor - the visitor to accept