generated from dgomes/iia-ia-sokoban
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deadlock.py
83 lines (75 loc) · 2.61 KB
/
deadlock.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
73
74
75
76
77
78
79
80
81
82
83
todos_cantos = set()
#deadlock de canto
def deadlock_corner(box,obstacles):
#calculate the neighbors of the next state box
global todos_cantos
up = (box[0] ,box[1] - 1)
down = (box[0], box[1] + 1)
left = (box[0] - 1 , box[1])
right = (box[0] + 1, box[1])
if(up in obstacles and left in obstacles):
todos_cantos.add(box)
#print(up,left)
return True
elif(left in obstacles and down in obstacles):
todos_cantos.add(box)
#print(left,down)
return True
elif(down in obstacles and right in obstacles):
todos_cantos.add(box)
#print(down,right)
return True
elif(right in obstacles and up in obstacles):
todos_cantos.add(box)
#print(right,up)
return True
else:
return False
#deadlock de duas caixas sem conseguir mexer
def deadlock_box(box,allboxes,obstacles):
up = (box[0] ,box[1] - 1)
down = (box[0], box[1] + 1)
left = (box[0] - 1 , box[1])
right = (box[0] + 1, box[1])
for boxes in allboxes:
if(up in obstacles and left in boxes or right in boxes):
return True
elif(down in obstacles and left in boxes or right in boxes):
return True
elif(left in obstacles and up in boxes or down in boxes):
return True
elif(right in obstacles and up in boxes or down in boxes):
return True
else:
return False
def isWall(position,x,y,corner,obstacles):
new_position = position
up = (new_position[0] ,new_position[1] - 1)
down = (new_position[0], new_position[1] + 1)
left = (new_position[0] - 1 , new_position[1])
right = (new_position[0] + 1, new_position[1])
#Enquanto nova posição não é corner
while new_position != corner:
if down in obstacles and x>0 and y>0:
new_position = (new_position[0]+x, new_position[1]+y)
else:
return False
return True
def deadlock_boxnotgoal(box,goals,obstacles):
up = (box[0] ,box[1] - 1)
down = (box[0], box[1] + 1)
left = (box[0] - 1 , box[1])
right = (box[0] + 1, box[1])
#se em baixo da caixa está parede
if down in obstacles:
# para cada canto existente no mapa
for goal in goals:
for corner in todos_cantos:
# se o canto tem o mesmo y que a box estão ao mesmo nível
if corner[1] == box[1] and goal[1] != box[1]:
isWall(box,1,0,corner,obstacles)
isWall(box,-1,0,corner,obstacles)
else:
return None
else:
return False