예제 #1
0
파일: maze.py 프로젝트: glibesyck/Maze
 def find_path(self):
     """
     Attempts to solve the maze by finding a path from the starting cell
     to the exit. Returns True if a path is found and False otherwise.
     """
     stack_of_cells = Stack()
     stack_of_cells.push((self._start_cell.row, self._start_cell.col))
     self._mark_path(self._start_cell.row, self._start_cell.col)
     while not stack_of_cells.is_empty():
         current_cell = stack_of_cells.peek()
         valid_neighbor = False
         for direction in directions:
             if self._valid_move(current_cell[0] + direction[0],
                                 current_cell[1] + direction[1]):
                 stack_of_cells.push((current_cell[0] + direction[0],
                                      current_cell[1] + direction[1]))
                 valid_neighbor = True
                 self._mark_path(current_cell[0] + direction[0],
                                 current_cell[1] + direction[1])
                 if self._exit_found(current_cell[0] + direction[0],
                                     current_cell[1] + direction[1]):
                     return True
                 break  #we don't need 2 valid neighbor at the same time
         if not valid_neighbor:
             self._mark_tried(current_cell[0], current_cell[1])
             stack_of_cells.pop()
     return False
예제 #2
0
def is_validSource(srcfile):
    s = Stack()
    for line in srcfile:
        for token in line:
            if token in "{[(":
                s.push(token)
            elif token in '}])':
                if s.is_empty():
                    return False
                else:
                    left = s.pop()
                    if (token == '}' and left != '{') or \
                            (token == ']' and left != '[') or \
                            (token == '(' and left != ')'):
                        return False

    return s.is_empty()
예제 #3
0
    def find_path(self):
        """
        Attempts to solve the maze by finding a path from the starting cell
        to the exit. Returns True if a path is found and False otherwise.
        """
        maze_path = Stack()
        first_position = self._start_cell
        curr_row = first_position.row
        curr_col = first_position.col
        maze_path.push(_CellPosition(curr_row, curr_col))
        self._mark_path(curr_row, curr_col)

        while not self._exit_found(curr_row, curr_col):

            for direction in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
                new_row = curr_row + direction[0]
                new_col = curr_col + direction[1]
                if self._valid_move(new_row, new_col):
                    curr_row = new_row
                    curr_col = new_col
                    maze_path.push(_CellPosition(curr_row, curr_col))
                    self._mark_path(curr_row, curr_col)
                    break

                else:
                    continue

            else:
                self._mark_tried(curr_row, curr_col)
                maze_path.pop()
                if maze_path.is_empty():
                    return False
                previous_cell = maze_path.peek()
                curr_row = previous_cell.row
                curr_col = previous_cell.col

        return True
예제 #4
0
 def find_path(self):
     """
     Attempts to solve the maze by finding a path from the starting cell
     to the exit. Returns True if a path is found and False otherwise.
     """
     path = Stack()
     i = self._start_cell.row
     j = self._start_cell.col
     counter = 0
     while True:
         check = 0
         if self._exit_found(i, j):
             for _ in range(len(path)):
                 coord = path.pop()
                 self._maze_cells[coord[0], coord[1]] = "x"
             self._maze_cells[self._exit_cell.row,
                              self._exit_cell.col] = "x"
             return True
         if (self._valid_move(i - 1, j)
                 and self._maze_cells[i - 1, j] == None and check == 0):
             if path.is_empty() or path.peek() != (i - 1, j):
                 path.push((i, j))
                 counter = 0
                 check = 1
                 i -= 1
             elif counter == 1:
                 self._maze_cells[i, j] = "o"
                 try:
                     path.pop()
                 except AssertionError:
                     return False
                 counter = 0
                 check = 1
                 i -= 1
             else:
                 counter += 1
         if (self._valid_move(i, j + 1)
                 and self._maze_cells[i, j + 1] == None and check == 0):
             if path.is_empty() or path.peek() != (i, j + 1):
                 path.push((i, j))
                 counter = 0
                 check = 1
                 j += 1
             elif counter == 1:
                 self._maze_cells[i, j] = "o"
                 try:
                     path.pop()
                 except AssertionError:
                     return False
                 counter = 0
                 check = 1
                 j += 1
             else:
                 counter += 1
         if (self._valid_move(i + 1, j)
                 and self._maze_cells[i + 1, j] == None and check == 0):
             if path.is_empty() or path.peek() != (i + 1, j):
                 path.push((i, j))
                 counter = 0
                 check = 1
                 i += 1
             elif counter == 1:
                 self._maze_cells[i, j] = "o"
                 try:
                     path.pop()
                 except AssertionError:
                     return False
                 counter = 0
                 check = 1
                 i += 1
             else:
                 counter += 1
         if (self._valid_move(i, j - 1)
                 and self._maze_cells[i, j - 1] == None and check == 0):
             if path.is_empty() or path.peek() != (i, j - 1):
                 path.push((i, j))
                 counter = 0
                 check = 1
                 j -= 1
             elif counter == 1:
                 self._maze_cells[i, j] = "o"
                 try:
                     path.pop()
                 except AssertionError:
                     return False
                 counter = 0
                 check = 1
                 j -= 1
             else:
                 counter += 1
         if check == 0 and counter == 0:
             return False