-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.py
58 lines (51 loc) · 1.31 KB
/
main.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
import json
import os.path
from cube import RubiksCube
from solver import IDA_star, build_heuristic_db
MAX_MOVES = 5
NEW_HEURISTICS = False
HEURISTIC_FILE = 'heuristic.json'
#--------------------------------
cube = RubiksCube(n=3)
cube.show()
print('-----------')
#--------------------------------
if os.path.exists(HEURISTIC_FILE):
with open(HEURISTIC_FILE) as f:
h_db = json.load(f)
else:
h_db = None
if h_db is None or NEW_HEURISTICS is True:
actions = [(r, n, d) for r in ['h', 'v', 's'] for d in [0, 1] for n in range(cube.n)]
h_db = build_heuristic_db(
cube.stringify(),
actions,
max_moves = MAX_MOVES,
heuristic = h_db
)
with open(HEURISTIC_FILE, 'w', encoding='utf-8') as f:
json.dump(
h_db,
f,
ensure_ascii=False,
indent=4
)
#--------------------------------
cube.shuffle(
l_rot = MAX_MOVES if MAX_MOVES < 5 else 5,
u_rot = MAX_MOVES
)
cube.show()
print('----------')
#--------------------------------
solver = IDA_star(h_db)
moves = solver.run(cube.stringify())
print(moves)
for m in moves:
if m[0] == 'h':
cube.horizontal_twist(m[1], m[2])
elif m[0] == 'v':
cube.vertical_twist(m[1], m[2])
elif m[0] == 's':
cube.side_twist(m[1], m[2])
cube.show()