-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
44 lines (39 loc) · 1.46 KB
/
Copy pathalgorithm.py
File metadata and controls
44 lines (39 loc) · 1.46 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
import time
import random
def display_matrix(matrix):
print('-' * (len(matrix[0]) + 2))
for row in matrix:
print('|' + ''.join(row) + '|')
print('-' * (len(matrix[0]) + 2))
def move_roaming_object(matrix, roaming_object_pos, direction):
x, y = roaming_object_pos
dx, dy = direction
if 0 <= x + dx < len(matrix) and 0 <= y + dy < len(matrix[0]) and matrix[x + dx][y + dy] != 'X':
matrix[x][y] = ' '
matrix[x + dx][y + dy] = 'O'
return (x + dx, y + dy), direction
else:
direction = (-dx, -dy)
return roaming_object_pos, direction
def main():
matrix = [[' ' for _ in range(20)] for _ in range(20)]
for i in range(10):
matrix[random.randint(0, 19)][random.randint(0, 19)] = 'X'
roaming_object_pos = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
for pos in roaming_object_pos:
x, y = pos
matrix[x][y] = 'O'
direction = [(1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]
while True:
display_matrix(matrix)
new_roaming_object_pos = []
new_direction = []
for i in range(len(roaming_object_pos)):
pos, dir = move_roaming_object(matrix, roaming_object_pos[i], direction[i])
new_roaming_object_pos.append(pos)
new_direction.append(dir)
roaming_object_pos = new_roaming_object_pos
direction = new_direction
time.sleep(1)
if __name__ == '__main__':
main()