Class AbstractView

java.lang.Object
battleship.view.AbstractView
All Implemented Interfaces:
View
Direct Known Subclasses:
Terminal, Window

public abstract class AbstractView
extends java.lang.Object
implements View
Abstract view class
See Also:
View, Window, Terminal
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected Game game  

    Fields inherited from interface battleship.view.View

    shipsSize
  • 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 by Computer player to play a move in the grid depending of its algorithm
    protected Direction getDirectionFromChar()
    ask player for keyboard input and parse it into one of Direction value
    protected abstract java.lang.String getKeyInput()
    ask player for keyboard input and return result
    private 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()
    ask Game.currentPlayer for keyboard input
    protected abstract Pair<java.lang.Integer,​java.lang.Integer> mouseInput​(Player player)
    ask player for mouse input
    void setShips​(Player player)
    used if player instance of Computer
    protected abstract void setUpperText​(java.lang.String s)
    Display a text above the grid on Window, simply print text on Terminal
    java.lang.String toString()
    Used during debugging, used in terminal to display grids too
    java.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

      protected final Game game
  • Constructor Details

    • AbstractView

      public AbstractView​(Game game)
  • Method Details

    • toString

      public java.lang.String toString()
      Used during debugging, used in terminal to display grids too
      Overrides:
      toString in class java.lang.Object
      Returns:
      all player grids
    • toString

      public java.lang.String toString​(boolean debug)
    • isShipPosition

      private boolean isShipPosition​(Ship ship, Pair<java.lang.Integer,​java.lang.Integer> boardsCoords)
      Returns:
      true if Ship.getFullCoords() contains boardsCoords, false otherwise
    • getDirectionFromChar

      protected Direction getDirectionFromChar() throws java.lang.InterruptedException
      ask player for keyboard input and parse it into one of Direction value
      Returns:
      Direction depending of player input
      Throws:
      java.lang.InterruptedException - caused by Window.getKeyInput()
      See Also:
      getDirectionFromChar()
    • getKeyInput

      protected abstract java.lang.String getKeyInput() throws java.lang.InterruptedException
      ask player for keyboard input and return result
      Returns:
      String given by player
      Throws:
      java.lang.InterruptedException - see Window.getKeyInput()
    • setUpperText

      protected abstract void setUpperText​(java.lang.String s)
      Display a text above the grid on Window, simply print text on Terminal
      Parameters:
      s - text to display
      See Also:
      Window.setUpperText(String), Terminal.setUpperText(String)
    • setShips

      public void setShips​(Player player) throws java.lang.InterruptedException
      used if player instance of Computer
      Specified by:
      setShips in interface View
      Parameters:
      player - player we ask to set position of its ships
      Throws:
      java.lang.InterruptedException - see Window.setShips(Player)
    • isPositionDrowned

      private int isPositionDrowned​(Player other, Ship ship, Pair<java.lang.Integer,​java.lang.Integer> pair)
      Parameters:
      other - other than the current player
      ship - check if this ship at this position has been hit
      pair - coords
      Returns:
      1 if ship fully drowned, 2 if only damaged, 0 if not
      See Also:
      isPositionDrowned(Player, Pair)
    • isPositionDrowned

      private int isPositionDrowned​(Player other, Pair<java.lang.Integer,​java.lang.Integer> pair)
      Parameters:
      other - other than the current player
      pair - 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.InterruptedException
      used by Computer player to play a move in the grid depending of its algorithm
      Specified by:
      chooseMove in interface View
      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 - see Window.chooseMove(Player)
    • mouseInput

      protected abstract Pair<java.lang.Integer,​java.lang.Integer> mouseInput​(Player player) throws java.lang.InterruptedException
      ask player for mouse input
      Parameters:
      player - Game.currentPlayer
      Returns:
      coordinate of player opponent grid
      Throws:
      java.lang.InterruptedException - see Window.mouseInput(Player)
      See Also:
      Window.mouseInput(Player)
    • keyboardInput

      protected abstract java.lang.String keyboardInput() throws java.lang.InterruptedException
      ask Game.currentPlayer for keyboard input
      Returns:
      String given by player
      Throws:
      java.lang.InterruptedException - see Window.keyboardInput()
      See Also:
      Window.keyboardInput(), Terminal.keyboardInput(), Terminal.keyboardInputInteger()