-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcell.py
80 lines (67 loc) · 2.09 KB
/
cell.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
class Cell:
'''
Class representing a cell.
Attributes:
-----------
i : int
Row index of the cell.
j : int
Column index of the cell.
walls : dict
Dictionary of the status of walls of the cell.
visited : bool
Whether the cell has been visited or not.
'''
def __init__(self, i, j):
'''Create a Cell object with indexes (i, j), four walls and marked unvisited'''
self.i = i
self.j = j
self.walls = {'N': True, 'W': True, 'S': True, 'E': True}
self.visited = False
def remove_walls(self, other):
'''Remove walls between two adjacent cells'''
dx = self.j - other.j
dy = self.i - other.i
# Check if removing walls is possible
if dx * dy != 0 or abs(dx + dy) != 1:
return -1
if dy == 1:
self.walls['N'] = False
other.walls['S'] = False
elif dy == -1:
self.walls['S'] = False
other.walls['N'] = False
if dx == 1:
self.walls['W'] = False
other.walls['E'] = False
elif dx == -1:
self.walls['E'] = False
other.walls['W'] = False
def add_walls(self, other):
'''Adding walls between two adjacent cells'''
dx = self.j - other.j
dy = self.i - other.i
# Check if adding walls is possible
if dx * dy != 0 or abs(dx + dy) != 1:
return -1
if dy == 1:
self.walls['N'] = True
other.walls['S'] = True
elif dy == -1:
self.walls['S'] = True
other.walls['N'] = True
if dx == 1:
self.walls['W'] = True
other.walls['E'] = True
elif dx == -1:
self.walls['E'] = True
other.walls['W'] = True
def set_visited(self):
'''Set a cell as visited'''
self.visited = True
def set_unvisited(self):
'''Set a cell as unvisited'''
self.visited = False
def is_visited(self):
'''Check if a cell is visited'''
return self.visited