-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
115 lines (94 loc) · 3.46 KB
/
grid.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from math import sqrt
# from path_pygame import scrub
# from path_pygame import scramble
import time
def log_(log):
f = open("position_log.txt", "a")
f.write(log)
f.close()
class GridMatrix:
def __init__(self, width, height):
self.width = width
self.height = height
self.rects = []
self.current = {}
self.goal = {}
def create_rects(self):
dim_x = self.width // 20
dim_y = self.height // 20
for n in range(0, dim_x):
for m in range(0, dim_y):
self.rects.append({
'coord': [n, m],
'grid': [0 + (n * 20), 0 + (m * 20), 20, 20],
'screen': None,
'color': None,
'fill': 0.2,
'wall': 0,
'cost': 0
})
return self.rects
def find_nine(current_pos, rectal_dict, position_list):
current_list = []
goal = {}
scores = []
for rect in rectal_dict:
if rect['coord'] == position_list[1]:
goal = rect
def wall_check(wall_num):
if wall_num['wall'] != 1:
wall_num['cost'] = -1
return wall_num
else:
wall_num['cost'] = 1000
return wall_num
def get_current_recs(recs_list, pos, dest_list):
a, b = (pos[0] - 1, pos[1] - 1)
for box in recs_list:
for i in range(0, 3):
for j in range(0, 3):
if box['coord'] == [a + j, b + i]:
dest_list.append(box)
def score(recs_obj1, recs_obj2):
y, z = (recs_obj1['grid'][0], recs_obj1['grid'][1])
n, m = (recs_obj2['grid'][0], recs_obj2['grid'][1])
if sqrt((n - y) ** 2 + (m - z) ** 2) <= 20:
recs_obj1['cost'] = -1
return [[recs_obj1['grid'][0] // 20, recs_obj1['grid'][1] // 20],
recs_obj1['cost'] + (sqrt((n - y) ** 2 + (m - z) ** 2)/100)
]
get_current_recs(rectal_dict, current_pos, current_list)
checks = {"box_one": current_list[1],
"box_three": current_list[3],
"box_five": current_list[5],
"box_seven": current_list[-2]
}
for recs in current_list:
if recs:
wall_check(recs)
for _ in checks:
a, b = checks[_]['coord']
check_position = [(0, -1), (-1, 0), (0, 1), (1, 0)]
for coords in position_list:
if checks[_]['coord'] == coords:
for obj in rectal_dict:
for coord in check_position:
if obj['coord'] == [a + coord[0], b + coord[0]]:
if obj.get('wall') == 1:
increase = checks[_].get('cost') + 1
checks[_].update(cost=increase)
if obj['coord'] == checks[_]['coord']:
obj['cost'] += 0.1
scores.append(score(checks[_], goal))
winner = scores[0]
time.sleep(0.08)
for num in scores:
if num[1] <= winner[1]:
winner = num
log_(f"---- step ----\n"
f'Scores: {"".join([str(i) for i in scores])}\n'
f"Current: {position_list[0]}\n"
f"Goal: {position_list[1]}\n"
f"Win: {winner[0]}\n"
f"Win Score: {winner[1]}\n")
return winner[0]