-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
242 lines (206 loc) · 7.15 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#------------------------------------------------------------------
# created by iliya faramrzi (telegram:iliyawww | gmail:iliyafaramarzi1384@gmail.com | linkedin:iliyafaramrzi | github: iliyafaramarzi)
# some notes that help you to know code better:
# 0 means way
# 1 mean don't way(wall)
# 2 start point
# 3 finish point
# '_' means this way is checked befor
#------------------------------------------------------------------
import numpy as np
from PIL import Image
# print list 10 * 10
def printli(li):
count = 0
for i in li:
if count == 9:
print(i)
count = 0
else:
print(i, end=' ')
count += 1
#find start point(2)
def find_start(li):
return li.index(2)
#find the finish point(3)
def find_finish(li):
return li.index(3)
# return up point of the location
def up(pos):
return pos - 10
# return down point of the location
def down(pos):
return pos + 10
# return right point of the location
def right(pos):
return pos + 1
# return left point of the location
def left(pos):
return pos - 1
#reset list with a source list(keep '_')
def reset(change, source):
for i in change:
index = change.index(i)
if not i == '_':
change[index] = source[index]
return change
#reset all the numbers in a list with a source list
def reset_all(change, source):
for i in change:
index = change.index(i)
change[index] = source[index]
return change
#list that work on it
li = [1,2,1,1,3,1,0,0,0,0,
1,0,1,1,0,1,0,0,0,0,
1,0,1,1,0,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,1,1,1,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,1,1,0,0]
#source list and don't change it at program
LI = [1,2,1,1,3,1,0,0,0,0,
1,0,1,1,0,1,0,0,0,0,
1,0,1,1,0,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,1,1,1,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,
0,0,0,0,0,1,1,1,0,0]
loc = find_start(li)
intersection = False
path = []
while 3 in li:
if find_finish(li) < loc:
if up(loc) == 0 and left(loc) == 0: # if we have more one way it is change the 'intersection' to True
intersection = True
if li[up(loc)] == 0 or li[up(loc)] == 3 and not li[up(loc)] == '_': # if te up point is 0 or 3 and it is not _ it work
loc = up(loc) # change location
li[loc] = 9 # change pointer
if not li[loc + 10] == '_': # check if don't gone this way befor do codes
li[loc + 10] = '*'
path.append(loc + 10)
if intersection == True: # check if 'intersection' is True make a sign that if this way is not true don't test it again
li[loc] == '_'
intersection = False
path.append(loc)
elif li[left(loc)] == 0 or li[left(loc)] == 3 and not li[left(loc)] == '_':
loc = left(loc)
li[loc] = 9
if not li[loc + 1] == '_':
li[loc + 1] = '*'
path.append(loc + 1)
if intersection == True:
li[loc] == '_'
intersection = False
path.append(loc)
#----------------------------------------
elif li[down(loc)] == 0 or li[down(loc)] == 3 and not li[down(loc)] == '_':
loc = down(loc)
li[loc] = 9
if not li[loc -10] == '_':
li[loc - 10] = '*'
path.append(loc - 10)
if intersection == True:
li[loc] = '_'
intersection = False
path.append(loc)
elif li[right(loc)] == 0 or li[right(loc)] == 3 and not li[right(loc)] == '_':
loc = right(loc)
li[loc] = 9
if not li[loc -1] == '_':
li[loc - 1] = '*'
path.append(loc - 1)
if intersection == True:
li[loc] == '_'
intersection = False
path.append(loc)
#----------------------------------------
else:
li = reset(li, LI)
loc = find_start(li)
path.clear()
elif find_finish(li) > loc:
if li[down(loc)] == 0 and li[right(loc)] == 0:
intersection = True
if li[down(loc)] == 0 or li[down(loc)] == 3 and not li[down(loc)] == '_':
loc = down(loc)
li[loc] = 9
if not li[loc -10] == '_':
li[loc - 10] = '*'
path.append(loc - 10)
if intersection == True:
li[loc] = '_'
intersection = False
path.append(loc)
elif li[right(loc)] == 0 or li[right(loc)] == 3 and not li[right(loc)] == '_':
loc = right(loc)
li[loc] = 9
if not li[loc -1] == '_':
li[loc - 1] = '*'
path.append(loc - 1)
if intersection == True:
li[loc] == '_'
intersection = False
path.append(loc)
#-----------------------------------------
elif li[up(loc)] == 0 or li[up(loc)] == 3 and not li[up(loc)] == '_':
loc = up(loc)
li[loc] = 9
if not li[loc + 10] == '_':
li[loc + 10] = '*'
path.append(loc + 10)
if intersection == True:
li[loc] == '_'
intersection = False
path.append(loc)
elif li[left(loc)] == 0 or li[left(loc)] == 3 and not li[left(loc)] == '_':
loc = left(loc)
li[loc] = 9
if not li[loc + 1] == '_':
li[loc + 1] = '*'
path.append(loc + 1)
if intersection == True:
li[loc] == '_'
intersection = False
path.append(loc)
#-----------------------------------------
else:
li = reset(li, LI)
loc = find_start(li)
path.clear()
#reset list for show the path
li = reset_all(li, LI)
for i in path:
li[i] = '*'
# Change numbers to RGB color
b = []
for i in li:
if i == 1:
b.append(tuple((255,255,255)))
elif i == 0:
b.append(tuple((0,0,0)))
elif i == 3:
b.append(tuple((249,0,25)))
elif i == 2:
b.append(tuple((252,243,32)))
elif i == '*':
b.append(tuple((213,237,237)))
finall = []
counter = 0
# Change list t osomething that pillow can show image
for j in range(1, 11):
counter += 1
finall.insert(j-1, b[(j-1) * 10: j * 10])
#Generate and show image
x = np.asarray(finall, dtype=np.uint8)
image = Image.fromarray(x)
image = image.resize((500,500))
image.show()
# Print Solved maze (* is the way)
printli(li)