Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. from reference import build_cave, shortest_path
  2. data = {'size': 4,'entrance': (0, 0),'exit': (2, 1),'dragon': (0, 2),'sword': (3, 3),'treasure': [(1, 3)],'walls': [(1, 1), (1, 2), (2, 2), (2, 3)]}
  3. data2 = {'size': 5,'entrance': (0, 2),'exit': (0, 4),'sword': (0, 0),'dragon': (2, 1),'treasure': [(0, 3), (4, 1)]}
  4. '''
  5. node = start state
  6. unexplored = a priority queue containing node as the only element
  7.  
  8. repeat:
  9.    if unexplored is empty:
  10.        return failure (there is no valid path)
  11.    node = node in unexplored with the lowest cost
  12.    if node == end state:
  13.        return solution
  14.    for each valid move from node:
  15.        child = result of executing move
  16.        if child is not in unexplored:
  17.            add child to unexplored, together with its cost
  18.        else if child is in unexplored and the new cost is less than
  19.                    current cost:
  20.            replace the current cost with new cost
  21. '''
  22.  
  23. def generate_moves(data, node, has_sword, explored,
  24.                    collected_treasure):
  25.     possible_moves = []
  26.        
  27.     if has_sword is False:
  28.         if 'sword' in data:
  29.             sword = (shortest_path(data, node[1], data['sword'], False), data['sword'], node[1])
  30.             possible_moves.append(sword)
  31.            
  32.     if 'treasure' in data:
  33.         treasure = data['treasure']
  34.         treasure_one = (shortest_path(data, node[1], data['treasure'][0], has_sword), data['treasure'][0], node[1])
  35.         if treasure_one[1] != node[1]:
  36.             possible_moves.append(treasure_one)
  37.         required_treasure = 1
  38.         if len(treasure) == 2:
  39.             treasure_two = (shortest_path(data, node[1], data['treasure'][1], has_sword), data['treasure'][1], node[1])
  40.             if treasure_two[1] != node[1]:
  41.                 possible_moves.append(treasure_two)
  42.             required_treasure = 2
  43.         if len(treasure) == 3:
  44.             treasure_three = (shortest_path(data, node[1], data['treasure'][2], has_sword), data['treasure'][2], node[1])
  45.             if treasure_three[1] != node[1]:
  46.                 possible_moves.append(treasure_three)
  47.             required_treasure = 3
  48.            
  49.     if collected_treasure == required_treasure:
  50.         end = data['exit']
  51.         find_exit = (shortest_path(data, node[1], end, has_sword), end, node[1])
  52.         possible_moves = []
  53.         possible_moves.append(find_exit)
  54.        
  55.        
  56.     for move in possible_moves:
  57.         if move[0] == None:
  58.             possible_moves.remove(move)
  59.        
  60.            
  61.     return sorted(possible_moves)
  62.  
  63. def count(start, end, path, path_count):
  64.    
  65.     count = 0
  66.     total_path = [end]
  67.     while total_path[-1] != start:
  68.         total_path.append(path[total_path[-1]])
  69.        
  70.     for node in total_path:
  71.         count += path_count[node]
  72.                  
  73.     return count    
  74.    
  75.  
  76. def optimal_path(data):
  77.     '''
  78.    '''
  79.     start = data['entrance']
  80.     node = (0, start, start)
  81.     explored = set()
  82.     unexplored = [node]
  83.     end = data['exit']
  84.     path = {}
  85.    
  86.     path_count = {}
  87.     collected_treasure = 0
  88.     has_sword = False
  89.     if 'sword' in data:
  90.         sword = data['sword']
  91.    
  92.     while sorted(unexplored):
  93.         node = unexplored.pop(0)
  94.         explored.add(node[1])
  95.            
  96.         if node[1] == end:
  97.             path_count[node[1]] = node[0]
  98.            
  99.             return (count(start, end, path, path_count))
  100.         if node[1] == sword:
  101.             has_sword = True
  102.         if 'treasure' in data:
  103.             if node[1] in data['treasure']:
  104.                 collected_treasure += 1
  105.                
  106.         possible_moves = generate_moves(data, node, has_sword, explored,
  107.                                         collected_treasure)
  108.         for move in sorted(possible_moves):
  109.             unexplored.append(move)
  110.             path[move[1]] = move[2]  
  111.             path_count[node[1]] = node[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement