forked from HpT2/Bloxorg-222
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_star.py
73 lines (51 loc) · 1.63 KB
/
A_star.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import math
from queue import PriorityQueue
class Node:
def __init__(self,block, goal, parent= None):
self.block = block
self.parent = parent
self.g_val = self.g(parent)
self.f_val = self.g_val + self.h(goal)
def h(self ,goal):
if self.block.rotation != "SPLIT":
return (math.dist([self.block.y,self.block.x],goal))
return ((math.dist([self.block.y,self.block.x],goal)) + math.dist([self.block.y,self.block.x],goal)) / 2
def g(self, parent):
if parent == None:
return 0
return parent.g_val + 1
def __lt__(self, other) -> bool:
return self.f_val < other.f_val
def solve(block,goal):
appended = 0
root = Node(block,goal)
queue = PriorityQueue()
queue.put(root)
close = []
traversed = 0
while not(queue.empty()):
exam_node = queue.get()
exam_block = exam_node.block
traversed += 1
if exam_block.isGoal():
print("SUCCESS")
print("APPENDED {} NODE".format(appended))
print("TRAVERSED", traversed, "NODE")
return exam_block
if not(exam_block.isValidBlock()):
continue
if exam_block in close:
continue
close.append(exam_block)
if exam_block.rotation != "SPLIT":
newBlocks = [exam_block.UP(), exam_block.DOWN(), exam_block.LEFT(), exam_block.RIGHT()]
appended += 4
else:
newBlocks = [exam_block.SPLIT_UP(), exam_block.SPLIT_DOWN(),
exam_block.SPLIT_LEFT(), exam_block.SPLIT_RIGHT(),
exam_block.SPLIT_UP_1(), exam_block.SPLIT_DOWN_1(),
exam_block.SPLIT_LEFT_1(), exam_block.SPLIT_RIGHT_1()]
appended += 8
new_nodes = [Node(newBlock, goal, exam_node) for newBlock in newBlocks]
for node in new_nodes:
queue.put(node)