games.battleship.utils¶

Battleship game utility functions.

This module provides helper functions for the Battleship game, including:
  • Board visualization

  • Coordinate formatting

  • Game status checking

Functions¶

calculate_game_stats(move_history)

Calculate game statistics from move history.

check_all_ships_placed(ship_placements)

Check if all required ships have been placed.

format_coordinates_list(coords_list)

Format a list of coordinates for display.

format_ship_types(ship_types)

Format a list of ship types for display.

visualize_board(board[, is_opponent])

Create a string representation of the Battleship board.

Module Contents¶

games.battleship.utils.calculate_game_stats(move_history)¶

Calculate game statistics from move history.

Parameters:

move_history (list[tuple[str, dict[str, Any]]]) – List of (player, outcome) tuples

Returns:

Dictionary of game statistics

Return type:

dict[str, Any]

games.battleship.utils.check_all_ships_placed(ship_placements)¶

Check if all required ships have been placed.

Parameters:

ship_placements (list[dict[str, Any]]) – List of ship placement dictionaries

Returns:

Tuple of (is_complete, error_message)

Return type:

tuple[bool, str | None]

games.battleship.utils.format_coordinates_list(coords_list)¶

Format a list of coordinates for display.

Parameters:

coords_list (list[dict[str, int]]) – List of coordinate dictionaries

Returns:

Formatted string of coordinates

Return type:

str

games.battleship.utils.format_ship_types(ship_types)¶

Format a list of ship types for display.

Parameters:

ship_types (list[str]) – List of ship type strings

Returns:

Formatted string of ship types

Return type:

str

games.battleship.utils.visualize_board(board, is_opponent=False)¶

Create a string representation of the Battleship board.

Parameters:
  • board (dict[str, Any]) – Dictionary containing board information

  • is_opponent (bool) – Whether this is the opponent’s board

Returns:

String representation of the board

Return type:

str