-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.py
99 lines (80 loc) · 2.62 KB
/
Solver.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
# -*- coding: utf-8 -*-
from Board import *
from Car import *
from collections import *
from copy import deepcopy
Position = namedtuple('Position', 'y x')
class BoardSolver():
"""
Creates and displays the grid
"""
def __init__(self, input):
self.win = None
self.et = set()
self.etats = deque()
self.etats.append(Board(input))
self.et.add(str(self.etats[0]))
def solve(self):
"""
Rush hour solver
:Return: None
"""
while self.etats:
state = self.etats.popleft()
if state.cars['r'].get_new_pos(1) == state.finish:
state.moves[-1].direction += 1
self.win = state
return
c_s = state.get_child_states()
for cs in c_s:
it = str(cs)
if it not in self.et:
self.et.add(it)
self.etats.append(cs)
def display(self):
"""
Display possible solution if game is unsovable print : unsolvable grid
:return: None
"""
if self.win is None:
print ("Unsolvable game.")
else:
for step in self.win.moves:
print(step)
def winner_car_positions_as_dict(self):
"""
Returns dict of type {'car name': (Position(y, x),size,orientation)}
:return: dict of positions for cars
:rtype: dict
"""
d=self.win.cars
if self.win is None:
return "Game is unsolvable"
else:
for el in d:
d[el]=(d[el].pos,d[el].size,d[el].is_horizontal)
return d
def winner_car_matrix(self):
"""
Put solution into the matrix
:Return: list of lists with cars placed in final state
:rtype: list
"""
matrix=[[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ']]
pos=self.winner_car_positions_as_dict()
for car in pos:
matrix[pos[car][0][0]][pos[car][0][1]]=car
orientation = pos[car][2]
size=pos[car][1]
if orientation==True:
for i in range(size):
matrix[pos[car][0][0]][pos[car][0][1]+i]=car
else:
for i in range(size):
matrix[pos[car][0][0]+i][pos[car][0][1]]=car
return matrix