2 import sys
3 import random
6 class PathFinder:
7         def __init__(self):
8                 self.visited = []
10                 pass
12         def pathFind(self, start, end, board):
13                 if start[0] == end[0] and start[1] == end[1]:
14                         #sys.stderr.write("Got to destination!\n")
15                         return []
17                 if self.visited.count(start) > 0:
18                         #sys.stderr.write("Back track!!\n")
19                         return False
20                 if start[0] < 0 or start[0] >= len(board) or start[1] < 0 or start[1] >= len(board[start[0]]):
21                         #sys.stderr.write("Out of bounds!\n")
22                         return False
23                 if len(self.visited) > 0 and board[start[0]][start[1]] != None:
24                         #sys.stderr.write("Full position!\n")
25                         return False
29                 self.visited.append(start)
30                 left = (start[0]-1, start[1])
31                 right = (start[0]+1, start[1])
32                 up = (start[0], start[1]-1)
33                 down = (start[0], start[1]+1)
34                 choices = [left, right, up, down]
35                 choices.sort(key = lambda e : (e[0] - end[0])**2.0 + (e[1] - end[1])**2.0 )
36                 options = []
37                 for point in choices:
38                         option = [point, self.pathFind(point,end,board)]
39                         if option[1] != False:
40                                 options.append(option)
42                 options.sort(key = lambda e : len(e[1]))
43                 if len(options) == 0:
44                         #sys.stderr.write("NO options!\n")
45                         return False
46                 else:
47                         if options[0][0] == left:
48                                 options[0][1].insert(0,"LEFT")
49                         elif options[0][0] == right:
50                                 options[0][1].insert(0,"RIGHT")
51                         elif options[0][0] == up:
52                                 options[0][1].insert(0,"UP")
53                         elif options[0][0] == down:
54                                 options[0][1].insert(0,"DOWN")
55                 #sys.stderr.write("PathFind got path " + str(options[0]) + "\n")
56                 return options[0][1]

UCC git Repository :: git.ucc.asn.au