-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.py
More file actions
52 lines (40 loc) · 1.38 KB
/
Copy pathGrid.py
File metadata and controls
52 lines (40 loc) · 1.38 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
import random
class Grid(object):
def __init__(self, width, height):
self.WIDTH = width
self.HEIGHT = height
self.obstacles = set()
self.target = None
self.rectangle_controller = None
def restart(self):
self.add_target(None)
self.obstacles.clear()
def add_target(self, pos):
if not self.is_occupied(pos):
self.target = pos
self.rectangle_controller.set_target(self.target)
def add_obstacle(self, pos):
if not self.is_occupied(pos):
self.obstacles.add(pos)
def is_inside(self, pos):
x = pos[0]
y = pos[1]
if x < 0 or x >= self.WIDTH:
return False
if y < 0 or y >= self.HEIGHT:
return False
return True
def is_obstacle(self, pos):
return pos in self.obstacles
def is_target(self, pos):
return pos == self.target
def is_occupied(self, pos):
return pos == self.target or pos in self.obstacles
def random_obstacles(self, percentage):
self.obstacles.clear()
for w in range(self.WIDTH):
for h in range(self.HEIGHT):
pos = (w, h)
if random.randrange(1, 101) <= percentage:
if not self.is_occupied(pos) and not self.rectangle_controller.is_occupied(pos):
self.add_obstacle(pos)