app.boards module

exception DoubleDestructionError

Bases: IndexError

exception CellAlreadyOccupiedError

Bases: IndexError

exception ShipDoesNotExistError

Bases: KeyError

exception UnlocatedShipRemovalError

Bases: IndexError

class Cell(shipUUID: int, squareIndex: int, alive: bool)

Bases: object

property alive

Defines wether the square has not been destroyed yet

destroy()

Destroys the cell

Raises

DoubleDestructionError – if the cell has already been destroyed

class Board(player: Player)

Bases: object

property size: int

Board size

Returns

board size

Return type

int

property player: Player

Player who owns the board

Returns

player object

Return type

Player

calculate_square_locations(start_location: tuple, orientation: Literal['UP', 'DOWN', 'LEFT', 'RIGHT'], size: int) list

Calculates the locations of the squares of a ship

Parameters
  • start_location (tuple) – location of the first square of the ship

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

  • size (int) – size of the ship

Raises

LocationOutsideOfRangeError – if the ship would not fit on the board

Returns

tuple[0]: list of the locations of the squares of the ship, tuple[1]: list of the locations of the squares surrounding the ship

Return type

tuple[list, list]

_get_ship_object(shipUUID: int) Ship

Returns the ship object associated with the given uuid

Parameters

shipUUID (int) – uuid of the ship

Raises

ShipDoesNotExistError – if the ship does not exist

Returns

ship object

Return type

Ship

add_ship(shipUUID: int, location: tuple, orientation: Literal['UP', 'DOWN', 'LEFT', 'RIGHT']) None

Adds a ship to the board

Parameters
  • shipUUID (int) – uuid of the ship to add

  • location (tuple) – location of the first square of the ship

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

Raises
remove_ship(shipUUID: int) None

Removes a ship from the board

Parameters

shipUUID (int) – uuid of the ship to remove

Raises
move_ship(shipUUID: int, location: tuple, orientation: Literal['UP', 'DOWN', 'LEFT', 'RIGHT']) None

Moves a ship to a new location. If the ship is not located it works like self.add_ship

Parameters
  • shipUUID (int) – uuid of the ship to move

  • location (tuple) – new location of the ship

Raises
cell(x: int, y: int) app.boards.Cell | None

Returns the cell at the given coordinates. If there is no cell it returns None

Parameters
  • x (int) – coordinate x

  • y (int) – coordinate y

Raises

IndexError – if the coordinates are out of range

Returns

value at the given coordinates

Return type

Cell | None

get_possible_locations(size: int, orientation: Literal['UP', 'DOWN', 'LEFT', 'RIGHT']) list

Returns a list of possible locations for a ship of the given size and orientation

Parameters
  • size (int) – size of the ship

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

Returns

list of (x, y) tuples

Return type

list

attack(x: int, y: int) AttackResult

Attacks the given location and returns AttackResult

Parameters
  • x (int) – x coordinate

  • y (int) – y coordinate

Raises
Returns

result of the attack

Return type

AttackResult

class PlayerBoard(player: Player)

Bases: Board