Class Position
java.lang.Object
battleship.Position
- All Implemented Interfaces:
IPosition
-
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
-
getColumn
-
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
-
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
-
occupy
-
shoot
-
equals
Compares this position to another object for equality. Two positions are equal if their row and column coordinates are the same. -
hashCode
-
toString
-