Package battleship
Class Position
java.lang.Object
battleship.Position
- All Implemented Interfaces:
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 -
Method Summary
Modifier and TypeMethodDescriptionReturns all valid adjacent positions (up, right, down, left) for this position.booleanCompares this position to another object for equality.intGets traditional column.charGets traditional row.intReturns the column coordinate of the position.intgetRow()Returns the row coordinate of the position.inthashCode()Returns a hash code for this position based on its row and column.booleanisAdjacentTo(IPosition other) Checks if this position is adjacent to another position.booleanisHit()Checks if this position has been hit by an attack.booleanisInside()Checks if this position is valid on the game board.booleanChecks if this position is occupied by a ship.voidoccupy()Marks this position as occupied by a ship.static Positionvoidshoot()Marks this position as hit by an attack.toString()Returns a string representation of this position.
-
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 positionclassicColumn- 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 positioncolumn- the column coordinate of the position
-
-
Method Details
-
randomPosition
-
getRow
public int getRow()Returns the row coordinate of the position. -
getColumn
public int getColumn()Returns the column coordinate of the position. -
getClassicRow
public char getClassicRow()Gets traditional row.- Specified by:
getClassicRowin interfaceIPosition- Returns:
- the traditional row within [A-J]
-
getClassicColumn
public int getClassicColumn()Gets traditional column.- Specified by:
getClassicColumnin interfaceIPosition- 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. -
isAdjacentTo
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:
isAdjacentToin interfaceIPosition- Parameters:
other- the other position to compare- Returns:
- true if the positions are adjacent, false otherwise
-
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:
adjacentPositionsin interfaceIPosition- Returns:
- List of valid adjacent positions
-
isOccupied
public boolean isOccupied()Checks if this position is occupied by a ship.- Specified by:
isOccupiedin interfaceIPosition- Returns:
- true if the position is occupied, false otherwise
-
isHit
public boolean isHit()Checks if this position has been hit by an attack. -
occupy
public void occupy()Marks this position as occupied by a ship. -
shoot
public void shoot()Marks this position as hit by an attack. -
equals
Compares this position to another object for equality. Two positions are equal if their row and column coordinates are the same. -
hashCode
public int hashCode()Returns a hash code for this position based on its row and column. -
toString
Returns a string representation of this position. The string includes the row and column coordinates.
-