robolab/src/planet.py

60 lines
2.0 KiB
Python

#!/usr/bin/env python3
from enum import IntEnum, unique
from typing import List, Optional, Tuple, Dict
# IMPORTANT NOTE: DO NOT IMPORT THE ev3dev.ev3 MODULE IN THIS FILE
@unique
class Direction(IntEnum):
""" Directions in degrees """
NORTH = 0
EAST = 90
SOUTH = 180
WEST = 270
# simple alias, no magic here
Weight = int
"""
Weight of a given path (received from the server)
value: -1 if broken path
>0 for all other paths
never 0
"""
'''
https://www.python.org/dev/peps/pep-0289/
Node checking:
((0,0),Direction.NORTH) in planetmap[0]
Simplify this for node search:
next(x for (x, y), direction in planetmap[0])
next(y for x, y in planetmap[0] if x == (0, 0))
planetmap[0][next((x, y) for x, y in planetmap[0] if y == 'North')]
'''
#Contains the representation of the map and provides certain functions to manipulate it according to the specifications
class Planet:
def __init__(self):
""" Initializes the data structure """
self._planetmap = {}
self.target = None
#Adds a bidirectional path defined between the start and end coordinates to the map and assigns the weight to it.
def add_path(self, start: Tuple[Tuple[int, int], Direction], target: Tuple[Tuple[int, int], Direction], weight: int):
if(start[0] not in self._planetmap):
self._planetmap[start[0]] = {}
if(start[1] not in self._planetmap[start[0]]):
self._planetmap[start[0]][start[1]] = (target[0], target[1], weight)
if(target[0] not in self._planetmap):
self._planetmap[target[0]] = {}
if(target[1] not in self._planetmap[target[0]]):
self._planetmap[target[0]][target[1]] = (start[0], start[1], weight)
def get_paths(self) -> Dict[Tuple[int, int], Dict[Direction, Tuple[Tuple[int, int], Direction, Weight]]]:
return self._planetmap
#Returns a shortest path between two nodes
def shortest_path(self, start: Tuple[int, int], target: Tuple[int, int]) -> Optional[List[Tuple[Tuple[int, int], Direction]]]:
pass