Package battleship

Class Game

java.lang.Object
battleship.Game
All Implemented Interfaces:
IGame

public class Game extends Object implements IGame
  • Field Details

  • Constructor Details

    • Game

      public Game(IFleet myFleet)
  • Method Details

    • printBoard

      public static void printBoard(IFleet fleet, List<IMove> moves, boolean show_shots, boolean showLegend)
      Prints the game board by representing the positions of ships, adjacent tiles, shots, and other game elements onto the console. The method also optionally displays shot positions and a legend explaining the symbols used on the board.
      Parameters:
      fleet - the fleet of ships to be displayed on the board. Ships are marked and their positions are shown according to their placement.
      moves - the list of moves containing shots. If shot positions are shown, they will be rendered based on their outcome (hit, miss, etc.).
      show_shots - if true, displays the shots taken during the game and marks their result (hit or miss) on the board.
      showLegend - if true, displays an explanatory legend of the symbols used to represent various elements such as ships, misses, hits, etc.
    • jsonShots

      public static String jsonShots(List<IPosition> shots)
      Serializes a list of shot positions into a JSON string. Each shot is represented with its classic row and column values. The method uses the Jackson library for JSON serialization.
      Parameters:
      shots - a list of shot positions to be serialized. Each position is represented by an implementation of the IPosition interface. The list must not be null.
      Returns:
      a formatted JSON string containing the shot positions. Each shot includes its classic row and column.
      Throws:
      RuntimeException - if an error occurs during JSON serialization.
    • getMyFleet

      public IFleet getMyFleet()
      Description copied from interface: IGame
      Gets my fleet.
      Specified by:
      getMyFleet in interface IGame
      Returns:
      my fleet
    • getAlienMoves

      public List<IMove> getAlienMoves()
      Description copied from interface: IGame
      Gets alien moves
      Specified by:
      getAlienMoves in interface IGame
      Returns:
      the alien moves
    • getAlienFleet

      public IFleet getAlienFleet()
      Description copied from interface: IGame
      Gets the alien fleet
      Specified by:
      getAlienFleet in interface IGame
      Returns:
      the alien fleet
    • getMyMoves

      public List<IMove> getMyMoves()
      Description copied from interface: IGame
      Gets my moves
      Specified by:
      getMyMoves in interface IGame
      Returns:
      my moves
    • randomEnemyFire

      public String randomEnemyFire()
      Simulates a random firing action by the enemy, generating a set of unique shot coordinates and serializing them into a JSON string. The method ensures that the random shots are valid and do not duplicate existing shots in the game or previous enemy moves. After generating the shots, it applies the firing logic and serializes the result for further processing.
      Specified by:
      randomEnemyFire in interface IGame
      Returns:
      A JSON string representing the list of randomly generated enemy shots.
      Throws:
      RuntimeException - if there is an error during the JSON serialization of the shots.
    • readEnemyFire

      public String readEnemyFire(Scanner in)
      Reads and processes the enemy fire input from the specified scanner. The method expects input describing positions for enemy shots. It verifies the format, ensures the correct number of positions are provided, and then fires on those positions.
      Specified by:
      readEnemyFire in interface IGame
      Parameters:
      in - the scanner object to read the enemy fire positions from, input must be formatted either as a single token combining the column and row (e.g., "A3") or as separate tokens (e.g., "A" followed by "3").
      Returns:
      a String describing the outcome of the enemy fire, such as a hit, a miss, or other results
      Throws:
      IllegalArgumentException - if the provided positions are incomplete, incorrectly formatted, or do not match the required number of shots (NUMBER_SHOTS).
    • fireShots

      public void fireShots(List<IPosition> shots)
      Fires a set of shots during a player's move. Each shot is resolved and consolidated into a move, which is processed and added to the list of alien moves. The method ensures exactly NUMBER_SHOTS shots are fired, validates each shot's position, and increments the move counter after completing the operation.
      Specified by:
      fireShots in interface IGame
      Parameters:
      shots - a list of positions representing the locations to fire shots at. The positions should be unique and valid within the bounds of the game board. The size of the list must be equal to NUMBER_SHOTS.
      Throws:
      IllegalArgumentException - if the list of shots is null, contains an invalid number of positions, or includes duplicate positions.
    • fireSingleShot

      public IGame.ShotResult fireSingleShot(IPosition pos, boolean isRepeated)
      Fires a single shot at the specified position, handling scenarios such as invalid positions, repeated shots, hits, misses, and sinking a ship. The method updates the necessary counters for invalid shots, repeated shots, hits, and sunk ships.
      Specified by:
      fireSingleShot in interface IGame
      Parameters:
      pos - the position to fire the shot at; must be valid and within the game board boundaries.
      isRepeated - true if the shot is marked as a repeat attempt, false otherwise.
      Returns:
      a ShotResult object containing the result of the shot, including whether the shot was valid, repeated, a hit, and whether a ship was sunk.
    • getRepeatedShots

      public int getRepeatedShots()
      Description copied from interface: IGame
      Gets repeated shots.
      Specified by:
      getRepeatedShots in interface IGame
      Returns:
      the repeated shots
    • getInvalidShots

      public int getInvalidShots()
      Description copied from interface: IGame
      Gets invalid shots.
      Specified by:
      getInvalidShots in interface IGame
      Returns:
      the invalid shots
    • getHits

      public int getHits()
      Description copied from interface: IGame
      Gets hits.
      Specified by:
      getHits in interface IGame
      Returns:
      the hits
    • getSunkShips

      public int getSunkShips()
      Description copied from interface: IGame
      Gets sunk ships.
      Specified by:
      getSunkShips in interface IGame
      Returns:
      the sunk ships
    • getRemainingShips

      public int getRemainingShips()
      Description copied from interface: IGame
      Gets remaining ships.
      Specified by:
      getRemainingShips in interface IGame
      Returns:
      the remaining ships
    • repeatedShot

      public boolean repeatedShot(IPosition pos)
    • printMyBoard

      public void printMyBoard(boolean show_shots, boolean show_legend)
      Description copied from interface: IGame
      Print my board (my fleet + alien shots).
      Specified by:
      printMyBoard in interface IGame
    • printAlienBoard

      public void printAlienBoard(boolean show_shots, boolean show_legend)
      Description copied from interface: IGame
      Print the alien board (alien fleet + my shots).
      Specified by:
      printAlienBoard in interface IGame
    • over

      public void over()
      Specified by:
      over in interface IGame