Package battleship.view
Class AbstractView
java.lang.Object
battleship.view.AbstractView
- All Implemented Interfaces:
View
public abstract class AbstractView extends java.lang.Object implements View
Abstract view class
-
Field Summary
-
Constructor Summary
Constructors Constructor Description AbstractView(Game game)
-
Method Summary
Modifier and Type Method Description Pair<java.lang.Integer,java.lang.Integer>
chooseMove(Player player)
used byComputer
player to play a move in the grid depending of its algorithmprotected Direction
getDirectionFromChar()
ask player for keyboard input and parse it into one ofDirection
valueprotected abstract java.lang.String
getKeyInput()
ask player for keyboard input and return resultprivate int
isPositionDrowned(Player other, Ship ship, Pair<java.lang.Integer,java.lang.Integer> pair)
private int
isPositionDrowned(Player other, Pair<java.lang.Integer,java.lang.Integer> pair)
private boolean
isShipPosition(Ship ship, Pair<java.lang.Integer,java.lang.Integer> boardsCoords)
protected abstract java.lang.String
keyboardInput()
askGame.currentPlayer
for keyboard inputprotected abstract Pair<java.lang.Integer,java.lang.Integer>
mouseInput(Player player)
askplayer
for mouse inputvoid
setShips(Player player)
used ifplayer
instance ofComputer
protected abstract void
setUpperText(java.lang.String s)
java.lang.String
toString()
Used during debugging, used in terminal to display grids toojava.lang.String
toString(boolean debug)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface battleship.view.View
displayBoard, displayWinner
-
Field Details
-
game
-
-
Constructor Details
-
AbstractView
-
-
Method Details
-
toString
public java.lang.String toString()Used during debugging, used in terminal to display grids too- Overrides:
toString
in classjava.lang.Object
- Returns:
- all player grids
-
toString
public java.lang.String toString(boolean debug) -
isShipPosition
- Returns:
true
ifShip.getFullCoords()
containsboardsCoords
,false
otherwise
-
getDirectionFromChar
ask player for keyboard input and parse it into one ofDirection
value- Returns:
- Direction depending of player input
- Throws:
java.lang.InterruptedException
- caused byWindow.getKeyInput()
- See Also:
getDirectionFromChar()
-
getKeyInput
protected abstract java.lang.String getKeyInput() throws java.lang.InterruptedExceptionask player for keyboard input and return result- Returns:
- String given by player
- Throws:
java.lang.InterruptedException
- seeWindow.getKeyInput()
-
setUpperText
protected abstract void setUpperText(java.lang.String s)- Parameters:
s
- text to display- See Also:
Window.setUpperText(String)
,Terminal.setUpperText(String)
-
setShips
used ifplayer
instance ofComputer
- Specified by:
setShips
in interfaceView
- Parameters:
player
- player we ask to set position of its ships- Throws:
java.lang.InterruptedException
- seeWindow.setShips(Player)
-
isPositionDrowned
private int isPositionDrowned(Player other, Ship ship, Pair<java.lang.Integer,java.lang.Integer> pair)- Parameters:
other
- other than the current playership
- check if this ship at this position has been hitpair
- coords- Returns:
- 1 if ship fully drowned, 2 if only damaged, 0 if not
- See Also:
isPositionDrowned(Player, Pair)
-
isPositionDrowned
- Parameters:
other
- other than the current playerpair
- coords to check- Returns:
- 2 if player already played here, 0 otherwise
-
chooseMove
public Pair<java.lang.Integer,java.lang.Integer> chooseMove(Player player) throws java.lang.InterruptedExceptionused byComputer
player to play a move in the grid depending of its algorithm- Specified by:
chooseMove
in interfaceView
- Parameters:
player
-Game.currentPlayer
- Returns:
- a couple (
Pair
containing the x and y coordinate (left side store Y and right side X) - Throws:
java.lang.InterruptedException
- seeWindow.chooseMove(Player)
-
mouseInput
protected abstract Pair<java.lang.Integer,java.lang.Integer> mouseInput(Player player) throws java.lang.InterruptedExceptionaskplayer
for mouse input- Parameters:
player
-Game.currentPlayer
- Returns:
- coordinate of
player
opponent grid - Throws:
java.lang.InterruptedException
- seeWindow.mouseInput(Player)
- See Also:
Window.mouseInput(Player)
-
keyboardInput
protected abstract java.lang.String keyboardInput() throws java.lang.InterruptedExceptionaskGame.currentPlayer
for keyboard input- Returns:
- String given by player
- Throws:
java.lang.InterruptedException
- seeWindow.keyboardInput()
- See Also:
Window.keyboardInput()
,Terminal.keyboardInput()
,Terminal.keyboardInputInteger()
-