Exemplo n.º 1
0
def bfs(start_tile, end_tile):
    """
    Breadth-first search algorithm
    :param start_tile: Tile object, start tile of board
    :param end_tile: Tile object, end tile of board
    :return:
    """
    queue = Queue()
    queue.put(start_tile)
    came_from = {}
    came_from[start_tile] = None
    has_been_next_tile = []

    while not queue.empty():
        current_tile = queue.get()
        current_tile.visit()

        if current_tile == end_tile:
            break

        for next_tile in current_tile.neighbours:

            if next_tile not in has_been_next_tile:
                has_been_next_tile.append(next_tile)

            if next_tile not in came_from:
                queue.put(next_tile)
                came_from[next_tile] = current_tile
                current_tile.visit()

    return came_from, has_been_next_tile
Exemplo n.º 2
0
def bfs(start, end):
    """
    Breadth first search. Takes a start tile and end tile, and uses
    their neighbour list to traverse.
    Uses the LIFO queue in queues.py.
    :param start: Tile
    :param end: Tile
    :return: came_from, dictionary with all tiles, and where we came from (parent).
             success, True or False. If the algorithm found the end tile or not.
             has_been_next, list over tiles that has been considered as the next tile.
    """
    frontier = Queue()
    frontier.add(start)
    came_from = {start: None}
    success = False
    has_been_next = []

    while not frontier.empty():
        current = frontier.pop()
        current.visit()
        if current == end:
            print("Breadth First Search, successful.")
            success = True
            break

        for next_tile in current.neighbours:
            if next_tile not in has_been_next:
                has_been_next.append(next_tile)
            if next_tile not in came_from:
                frontier.add(next_tile)
                came_from[next_tile] = current

    return came_from, success, has_been_next
Exemplo n.º 3
0
class NumpadBase:

    KEYS = '0123456789xy'

    # this signals a need to stop user interaction and re-look at ux stack
    ABORT_KEY = '\xff'

    def __init__(self):
        # once pressed, and released; keys show up in this queue
        self._changes = Queue(24)
        self.key_pressed = ''

        self.debug = 0  # 0..2

        self.last_event_time = utime.ticks_ms()

    async def get(self):
        # Get keypad events. Single-character strings.
        return await self._changes.get()

    def get_nowait(self):
        # Poll if anything ready: not async!
        return self._changes.get_nowait()

    def empty(self):
        return self._changes.empty()

    def abort_ux(self):
        # pretend a key was pressed, in order to unblock things
        self.inject(self.ABORT_KEY)

    def inject(self, key):
        # fake a key press and release
        if not self._changes.full():
            self.key_pressed = ''
            self._changes.put_nowait(key)
            self._changes.put_nowait('')

    def _key_event(self, key):
        if key != self.key_pressed:
            # annouce change
            self.key_pressed = key

            if self._changes.full():
                # no space, but do a "all up" and the new event
                print('Q overflow')
                self._changes.get_nowait()
                self._changes.get_nowait()
                if key != '':
                    self._changes.put_nowait('')

            self._changes.put_nowait(key)

            self.last_event_time = utime.ticks_ms()