Class Game
java.lang.Object
battleship.Game
- All Implemented Interfaces:
IGame
-
Nested Class Summary
Nested classes/interfaces inherited from interface IGame
IGame.ShotResult -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidFires a set of shots during a player's move.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.Gets the alien fleetGets alien movesintgetHits()Gets hits.intGets invalid shots.Gets my fleet.Gets my movesintGets remaining ships.intGets repeated shots.intGets sunk ships.static StringSerializes a list of shot positions into a JSON string.voidover()voidprintAlienBoard(boolean show_shots, boolean show_legend) Print the alien board (alien fleet + my shots).static voidprintBoard(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.voidprintMyBoard(boolean show_shots, boolean show_legend) Print my board (my fleet + alien shots).Simulates a random firing action by the enemy, generating a set of unique shot coordinates and serializing them into a JSON string.readEnemyFire(Scanner in) Reads and processes the enemy fire input from the specified scanner.booleanrepeatedShot(IPosition pos)
-
Field Details
-
BOARD_SIZE
public static final int BOARD_SIZE- See Also:
-
NUMBER_SHOTS
public static final int NUMBER_SHOTS- See Also:
-
-
Constructor Details
-
Game
-
-
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
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 theIPositioninterface. 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
Description copied from interface:IGameGets my fleet.- Specified by:
getMyFleetin interfaceIGame- Returns:
- my fleet
-
getAlienMoves
Description copied from interface:IGameGets alien moves- Specified by:
getAlienMovesin interfaceIGame- Returns:
- the alien moves
-
getAlienFleet
Description copied from interface:IGameGets the alien fleet- Specified by:
getAlienFleetin interfaceIGame- Returns:
- the alien fleet
-
getMyMoves
Description copied from interface:IGameGets my moves- Specified by:
getMyMovesin interfaceIGame- Returns:
- my moves
-
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:
randomEnemyFirein interfaceIGame- 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
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:
readEnemyFirein interfaceIGame- 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
Stringdescribing 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
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 exactlyNUMBER_SHOTSshots are fired, validates each shot's position, and increments the move counter after completing the operation.- Specified by:
fireShotsin interfaceIGame- 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 toNUMBER_SHOTS.- Throws:
IllegalArgumentException- if the list of shots is null, contains an invalid number of positions, or includes duplicate positions.
-
fireSingleShot
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:
fireSingleShotin interfaceIGame- 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:IGameGets repeated shots.- Specified by:
getRepeatedShotsin interfaceIGame- Returns:
- the repeated shots
-
getInvalidShots
public int getInvalidShots()Description copied from interface:IGameGets invalid shots.- Specified by:
getInvalidShotsin interfaceIGame- Returns:
- the invalid shots
-
getHits
-
getSunkShips
public int getSunkShips()Description copied from interface:IGameGets sunk ships.- Specified by:
getSunkShipsin interfaceIGame- Returns:
- the sunk ships
-
getRemainingShips
public int getRemainingShips()Description copied from interface:IGameGets remaining ships.- Specified by:
getRemainingShipsin interfaceIGame- Returns:
- the remaining ships
-
repeatedShot
-
printMyBoard
public void printMyBoard(boolean show_shots, boolean show_legend) Description copied from interface:IGamePrint my board (my fleet + alien shots).- Specified by:
printMyBoardin interfaceIGame
-
printAlienBoard
public void printAlienBoard(boolean show_shots, boolean show_legend) Description copied from interface:IGamePrint the alien board (alien fleet + my shots).- Specified by:
printAlienBoardin interfaceIGame
-
over
-