app.ui module

exception ActionAborted

Bases: Exception

class Styles(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Text styles for the CLI

Parameters

IntEnum – color number

GRID = 1
SHIP = 2
DESTROYED = 3
SELECTOR = 4
ERROR = 5
SUNK = 6
class CLI

Bases: object

_calculate_edge_indexes(current_orientation: Literal['UP', 'DOWN', 'LEFT', 'RIGHT'], board_size: int, ship_size: int) tuple

A helper function to calculate the edge positions of the ship on the board

Parameters
  • current_orientation (Literal["UP", "DOWN", "LEFT", "RIGHT"]) – current orientation of the ship

  • board_size (int) – board size

  • ship_size (int) – ship size

Returns

(max_x, min_x, max_y, min_y)

Return type

tuple

_show_remaining_fleet(board: Board) None

Shows the remaining fleet during positioning

Parameters

board (Board) – board on which the positioning occurs

_draw_ship(ship: Ship, board: Board, ship_square_locations: list, possible_location: bool = True) None

Draws a ship on the board. It clolors the ship cli_config.colors["error"] if it’s not possible to place it there.

Parameters
  • ship (Ship) – ship object

  • board (Board) – board object

  • ship_square_locations (list) – list of (x, y) locations of the ship

  • possible_location (bool, optional) – indicates if it’s possible to place

  • True. (the ship in that location. Defaults to) –

_next_orientation(current_orientation: Literal['UP', 'DOWN', 'LEFT', 'RIGHT'], location: tuple, board_size: int, ship_size: int) Literal['UP', 'DOWN', 'LEFT', 'RIGHT']

Calculates the next (clockwise) orientation of the ship. If it is not possible to rotate the ship in a given location, it returns the current orientation.

Returns

Ship orientation

Return type

Literal[“UP”, “DOWN”, “LEFT”, “RIGHT”]

_transform_location(key: int, location: tuple, max_x: int, min_x: int, max_y: int, min_y: int) tuple

Transforms a given location based on the user input (key) and given boundaries.

Parameters
  • key (int) – user input key code

  • location (tuple) – (x, y)

  • max_x (int) – max x index

  • min_x (int) – min x index

  • max_y (int) – max y index

  • min_y (int) – min y index

Returns

(x, y) location

Return type

tuple

input(prompt: str) str

Reads user input.

Parameters

prompt (str) – prompt to be shown

Returns

user input

Return type

str

show_menu(title: str, options: dict[str, any], board: boards.Board | None = None) any

Shows a menu on the screen

Parameters
  • title (str) – title of the menu

  • options (dict[str, any]) – option_name -> value dictionary

  • board (Board | None, optional) – Board to show. Defaults to None.

Returns

value of the selected option

Return type

any

show_settings()

Shows the settings menu

show_instructions(data: dict) None

Shows given instructions on the screen

Parameters

data (dict) – data from cli_config.instructions to be shown

show_board(board: Board, hilight: None | tuple = None, ommit_locations: list | None = None, skip_refresh: bool = False, show_hits_only: bool = False, display_strength: bool = False) None

Prints board to the console.

Parameters
  • board (Board) – The board to be printed

  • hilight (None | tuple, optional) – (x, y) location to be highlited. Defaults to None.

  • ommit_locations (list | None, optional) – list of (x, y) locations not to be shown. Defaults to None.

  • skip_refresh (bool, optional) – Decides of the function will skip the screen refresh. Defaults to False.

  • show_hits_only (bool, optional) – Decides if only hits will be shown. Defaults to False.

  • display_strength (bool, optional) – Decides if the ships strength will be shown. Defaults to False.

get_location(board: Board, additional_board: boards.Board | None = None, show_hits_only: bool = False, instructions: dict | None = None, abortable: bool = False) tuple

Gets a location from the user

Parameters
  • board (Board) – board to get the location from

  • additional_board (Board, optional) – board to be shown but not to be interacted with. Defaults to None

  • show_hits_only (bool, optional) – Decides if only hits will be shown on the board. Defaults to False.

  • instructions (dict | None, optional) – Instructions from cli_config.instructions to be shown to the user. Defaults to None.

  • abortable (bool, optional) – Decides if the user can abort the action. Defaults to False.

Returns

(x, y) location

Return type

tuple

Raises

ActionAborted – If the user aborts the action

get_move_ship_data(ship: Ship, board: Board, randomizable: bool = False) tuple | None

Gets the new position and orientation of a ship from user. It ensures validity of the data.

Parameters
  • ship (Ship) – ship to move

  • board (Board) – board to move the ship on

  • randomizable (bool, Optional) – Decides if the user can randomize the ship placement. Defaults to False.

Returns

(x, y, orientation). If None, the user decided to randomize the ship placement

Return type

tuple | None

Raises

ActionAborted – if the user aborts the ship placement

wrap(function: Callable)

Wraps a function to catch keyboard interrupts or other errors and close the screen

Parameters

function (Callable) – function to wrap

close()

Closes the screen