Class RandomStrategy

java.lang.Object
student.micro.battleship.RandomStrategy
All Implemented Interfaces:
BattleshipStrategy

public class RandomStrategy extends Object implements BattleshipStrategy
A strategy for the classic board game "Battleship" that methodically plods along, firing at cells in order.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new strategy object.
  • Method Summary

    Modifier and Type
    Method
    Description
    callNextShot(GameState currentGameState)
    The main "play" method for this strategy--called when it is this player's turn to call out a shot on the opponent's board to try to hit one of the opponent's ships.
    boolean
    In some games, strategies that wish to may play "deviously", meaning they will reposition their ships when allowed.
    Gets the name of this player/strategy.
    void
    Called to initialize (or re-initialize) this strategy to play a new game.
    placeShips(GameState currentGameState)
    Called when this strategy can place its ships on the board, so that it can indicate where it wants its ships to go (typically at the start of a game).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RandomStrategy

      public RandomStrategy()
      Create a new strategy object.
  • Method Details

    • getName

      public String getName()
      Gets the name of this player/strategy.
      Specified by:
      getName in interface BattleshipStrategy
      Returns:
      This strategy's name.
    • newGame

      public void newGame()
      Called to initialize (or re-initialize) this strategy to play a new game. This method is called before a game begins. The same strategy might play multiple games over time, and this is how the strategy is "reset" before the start of the game it is about to play right now.
      Specified by:
      newGame in interface BattleshipStrategy
    • placeShips

      public ShipPlacementMove placeShips(GameState currentGameState)
      Called when this strategy can place its ships on the board, so that it can indicate where it wants its ships to go (typically at the start of a game). Note that in some "devious" versions of the game, this might be called more than once, giving a strategy a chance to reposition its ships after the opponent has called a shot. In that situation, the new ship placement must remain consistent with all the hits/misses scored by the opponent in all previous turns.
      Specified by:
      placeShips in interface BattleshipStrategy
      Parameters:
      currentGameState - The current state of this player's board and the opponent's board.
      Returns:
      The new ship placement this strategy wants to use.
    • callNextShot

      public CallShotMove callNextShot(GameState currentGameState)
      The main "play" method for this strategy--called when it is this player's turn to call out a shot on the opponent's board to try to hit one of the opponent's ships.
      Specified by:
      callNextShot in interface BattleshipStrategy
      Parameters:
      currentGameState - The current state of this player's board and the opponent's board.
      Returns:
      The next shot this strategy is calling (this strategy's next move).
    • canPlayDeviously

      public boolean canPlayDeviously()
      In some games, strategies that wish to may play "deviously", meaning they will reposition their ships when allowed. Ships can be repositioned as long as sunken ships don't move and as long as the new position of each ship does not occupy any MISSes, and occupies the same number of HITs as before.
      Specified by:
      canPlayDeviously in interface BattleshipStrategy
      Returns:
      True if this strategy is able to reposition its ships when allowed to play deviously, or false if it is not designed to be devious and expects to leave its ships in the same position for the entire game.