Class Position

java.lang.Object
battleship.Position
All Implemented Interfaces:
IPosition

public class Position extends Object implements IPosition
Represents a position on the game board. A position is defined by its row and column coordinates, and it can be occupied or hit during the game.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Position(char classicRow, int classicColumn)
    Constructs a new Position with the specified row and column.
    Position(int row, int column)
    Constructs a new Position with the specified row and column.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns all valid adjacent positions (up, right, down, left) for this position.
    boolean
    equals(Object otherPosition)
    Compares this position to another object for equality.
    int
    Gets traditional column.
    char
    Gets traditional row.
    int
    Returns the column coordinate of the position.
    int
    Returns the row coordinate of the position.
    int
    Returns a hash code for this position based on its row and column.
    boolean
    Checks if this position is adjacent to another position.
    boolean
    Checks if this position has been hit by an attack.
    boolean
    Checks if this position is valid on the game board.
    boolean
    Checks if this position is occupied by a ship.
    void
    Marks this position as occupied by a ship.
    static Position
     
    void
    Marks this position as hit by an attack.
    Returns a string representation of this position.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Position

      public Position(char classicRow, int classicColumn)
      Constructs a new Position with the specified row and column. By default, the position is not occupied and not hit.
      Parameters:
      classicRow - the row coordinate of the position
      classicColumn - the column coordinate of the position
    • Position

      public Position(int row, int column)
      Constructs a new Position with the specified row and column. By default, the position is not occupied and not hit.
      Parameters:
      row - the row coordinate of the position
      column - the column coordinate of the position
  • Method Details

    • randomPosition

      public static Position randomPosition()
    • getRow

      public int getRow()
      Returns the row coordinate of the position.
      Specified by:
      getRow in interface IPosition
      Returns:
      the row coordinate
    • getColumn

      public int getColumn()
      Returns the column coordinate of the position.
      Specified by:
      getColumn in interface IPosition
      Returns:
      the column coordinate
    • getClassicRow

      public char getClassicRow()
      Gets traditional row.
      Specified by:
      getClassicRow in interface IPosition
      Returns:
      the traditional row within [A-J]
    • getClassicColumn

      public int getClassicColumn()
      Gets traditional column.
      Specified by:
      getClassicColumn in interface IPosition
      Returns:
      the traditional column within [1-10]
    • isInside

      public boolean isInside()
      Checks if this position is valid on the game board. A position is "inside" if its row and column are within the board's boundaries.
      Specified by:
      isInside in interface IPosition
      Returns:
      true if the position is within the board, false otherwise
    • isAdjacentTo

      public boolean isAdjacentTo(IPosition other)
      Checks if this position is adjacent to another position. Two positions are adjacent if they are next to each other horizontally, vertically, or diagonally.
      Specified by:
      isAdjacentTo in interface IPosition
      Parameters:
      other - the other position to compare
      Returns:
      true if the positions are adjacent, false otherwise
    • adjacentPositions

      public List<IPosition> adjacentPositions()
      Returns all valid adjacent positions (up, right, down, left) for this position. A valid position is one that exists within the board boundaries.
      Specified by:
      adjacentPositions in interface IPosition
      Returns:
      List of valid adjacent positions
    • isOccupied

      public boolean isOccupied()
      Checks if this position is occupied by a ship.
      Specified by:
      isOccupied in interface IPosition
      Returns:
      true if the position is occupied, false otherwise
    • isHit

      public boolean isHit()
      Checks if this position has been hit by an attack.
      Specified by:
      isHit in interface IPosition
      Returns:
      true if the position is hit, false otherwise
    • occupy

      public void occupy()
      Marks this position as occupied by a ship.
      Specified by:
      occupy in interface IPosition
    • shoot

      public void shoot()
      Marks this position as hit by an attack.
      Specified by:
      shoot in interface IPosition
    • equals

      public boolean equals(Object otherPosition)
      Compares this position to another object for equality. Two positions are equal if their row and column coordinates are the same.
      Specified by:
      equals in interface IPosition
      Overrides:
      equals in class Object
      Parameters:
      otherPosition - the object to compare
      Returns:
      true if the positions are equal, false otherwise
    • hashCode

      public int hashCode()
      Returns a hash code for this position based on its row and column.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • toString

      public String toString()
      Returns a string representation of this position. The string includes the row and column coordinates.
      Overrides:
      toString in class Object
      Returns:
      the string representation of the position