- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
Latest commit
81 lines (67 loc) · 2.88 KB
/
Copy pathcode.py
File metadata and controls
81 lines (67 loc) · 2.88 KB
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
importtime
#╔═══╗ ╔╗ ╔╗ ╔═══╗ ╔╗
#║╔═╗║ ╔╝╚╗║║ ║╔══╝ ║║
#║╚═╝║╔══╗ ╚╗╔╝║╚═╗ ║╚══╗╔╗╔═╗ ╔═╝║╔══╗╔═╗
#║╔══╝╚ ╗║ ║║ ║╔╗║ ║╔══╝╠╣║╔╗╗║╔╗║║╔╗║║╔╝
#║║ ║╚╝╚╗ ║╚╗║║║║╔╝╚╗ ║║║║║║║╚╝║║║═╣║║
#╚╝ ╚═══╝ ╚═╝╚╝╚╝╚══╝ ╚╝╚╝╚╝╚══╝╚══╝╚╝
defnewline():
print("------------------------------------")
defprint_board(board):
forrowinboard:
forcellinrow:
print(cell, end='')
print()
deffind_path(board, start, end):
directions= [(0, 1), (0, -1), (1, 0), (-1, 0)]
queue= [(start, [])]
visited=set()
whilequeue:
(x, y), path=queue.pop(0)
if (x, y) ==end:
returnpath
fordx, dyindirections:
nx, ny=x+dx, y+dy
if0<=nx<len(board) and0<=ny<len(board[0]) andboard[nx][ny] !='🔲'and (nx, ny) notinvisited:
queue.append(((nx, ny), path+ [(nx, ny)]))
visited.add((nx, ny))
returnNone
defmain():
input_board= [
['🚩', '🔲', '🔲', '🏁', '🔲', '💠', '💠', '💠', '💠', '💠'],
['💠', '🔲', '🔲', '💠', '🔲', '💠', '💠', '💠', '💠', '💠'],
['💠', '🔲', '🔲', '💠', '🔲', '🔲', '🔲', '💠', '💠', '💠'],
['💠', '💠', '💠', '💠', '🔲', '💠', '🔲', '💠', '💠', '💠'],
['🔲', '🔲', '🔲', '🔲', '🔲', '🔲', '🔲', '🔲', '🔲', '🔲']
]
end_pos=None
foriinrange(len(input_board)):
forjinrange(len(input_board[0])):
ifinput_board[i][j] =='🏁':
end_pos= (i, j)
break
ifend_posisNone:
print("You need to place atleast 1 end position")
return
start_pos=None
foriinrange(len(input_board)):
forjinrange(len(input_board[0])):
ifinput_board[i][j] =='🚩':
start_pos= (i, j)
break
ifstart_posisNone:
print("You need to place a start position")
return
path=find_path(input_board, start_pos, end_pos)
ifpathisNone:
print("Error! This map is unbeatable, or you have placed too much elements")
else:
print("Path calculated")
forstepinpath:
board_copy= [row[:] forrowininput_board]
board_copy[step[0]][step[1]] ='🤖'
newline()
print_board(board_copy)
time.sleep(0.5) # Adjust the delay time as needed
if__name__=="__main__":
main()