This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmazebot.py
101 lines (68 loc) · 1.93 KB
/
mazebot.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
import requests
import json
from os import system
from msvcrt import getch
from time import sleep
from math import sqrt
# gets a specified amount of lines
def get_maze():
request = requests.get(f'https://api.noopschallenge.com/mazebot/random?maxSize=20&minSize=20')
json_obj = json.loads(request.content)
grid = json_obj["map"]
x, y = json_obj["startingPosition"]
targx, targy = json_obj["endingPosition"]
return grid, x, y, targx, targy
def clear():
system('cls')
def draw(grid, x, y, steps):
clear()
screen = ''
for i in range(len(grid)):
screen += '\n'
for j in range(len(grid[0])):
if [j, i] != [x, y]:
screen += grid[i][j] + ' '
else:
screen += '□ '
print(screen.replace('X', '●'))
print(f'Steps taken {steps}')
print('''
Press for:
W to move up
A to move left
S to move down
D to move right
R to reset
P to quit
''')
def incSteps():
global steps
steps += 1
grid, x, y, targx, targy = get_maze()
savex = x
savey = y
global steps
steps = 0
while True:
if x == targx and y == targy:
clear()
print(f'You have found the exit in {steps} steps!')
sleep(1)
steps = 0
grid, x, y, targx, targy = get_maze()
savex = x
savey = y
draw(grid, x, y, steps)
key = getch()
if key == b'w' and grid[y - 1][x] != 'X' and y > 0 : y -= 1 ; incSteps()
if key == b'a' and grid[y][x - 1] != 'X' and x > 0: x -= 1 ; incSteps()
try:
if key == b's' and grid[y + 1][x] != 'X' : y += 1 ; incSteps()
if key == b'd' and grid[y][x + 1] != 'X' : x += 1 ; incSteps()
except IndexError:
pass
if key == b'r':
steps = 0
x = savex
y = savey
if key == b'p' : quit()