Skip to content

Instantly share code, notes, and snippets.

@Benhgift
Last active October 27, 2016 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benhgift/35d0b6daf02ee6ba58f7497f550b15bb to your computer and use it in GitHub Desktop.
Save Benhgift/35d0b6daf02ee6ba58f7497f550b15bb to your computer and use it in GitHub Desktop.
map_solver
from clint.textui import colored
from time import sleep
sleep_time = .2
array = [
[2, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 1, 1, 1, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 0],
]
def print_array(array):
print()
for line in array:
for i, elem in enumerate(line):
text = str(elem)
if elem == 1:
text = colored.green(text)
if elem == 2:
text = colored.red(text)
print(text, end=' ')
print()
s()
def s(time=None):
if time:
sleep(time)
else:
sleep(sleep_time)
def already_seen_or_wall(array, point):
target = array[point[1]][point[0]]
return target == 2 or target == 1
def mark_as_seen(array, point):
array_point = array[point[1]][point[0]]
if array_point != 1:
array[point[1]][point[0]] = 2
def handle_new_point(array, new_points, x, y):
new_point = [x, y]
if not already_seen_or_wall(array, new_point):
new_points += [new_point]
mark_as_seen(array, new_point)
return new_points
def crawl(array, point):
new_points = []
x = point[0]
y = point[1]
length = len(array) - 1
if x < length:
new_points = handle_new_point(array, new_points, x+1, y)
if x > 0:
new_points = handle_new_point(array, new_points, x-1, y)
if y < length:
new_points = handle_new_point(array, new_points, x, y+1)
if y > 0:
new_points = handle_new_point(array, new_points, x, y-1)
return new_points
def check_if_hit_end(q, array):
target = len(array) - 1
for x in q:
if x[0] == target and x[1] == target:
return True
return False
def main():
print_array(array)
end_point = [len(array), len(array)]
q = [[0,0]]
while q:
s()
new_points = []
for x in q:
new_points += crawl(array, x)
q = new_points[:]
if check_if_hit_end(q, array):
print_array(array)
s()
print("\nwinner")
s(1)
return 1
print_array(array)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment