-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplateau.py
72 lines (56 loc) · 2.24 KB
/
plateau.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
#Implements a plateau with a grid system
from object import Object
class Plateau:
class Error(Exception):
"Base class for other exceptions"
pass
class OutOfLimitsError(Error):
"Raised when a instruction moves a object to off limits"
pass
class CollisionDetectedError(Error):
"Raised when a instruction causes a collision between two objects"
pass
#Implements the size of the grid in a separate class for a better readability
class Limits:
#Limits class constructor
def __init__(self, x: int, y: int) -> None:
self._x = x
self._y = y
@property
def x(self) -> int:
return self._x
@x.setter
def x(self, value: int) -> None:
if value > 0:
self.x = value
else:
raise ValueError(f"The limit has to be greater than zero")
@property
def y(self) -> int:
return self._y
@y.setter
def y(self, value: int) -> None:
if value > 0:
self.y = value
else:
raise ValueError(f"The limit has to be greater than zero")
#Plateau class constructor
def __init__(self, xLimit: int, yLimit: int) -> None:
Plateau.Limits = Plateau.Limits(xLimit, yLimit)
#False represents a empty space in the grid
self.grid = [[False for x in range(xLimit+1)] for y in range(yLimit+1)]
#insert a object in the grid
def insert(self, obj: Object) -> None:
self.grid[obj.x][obj.y] = obj
#Removes a object from the grid
def remove(self, obj: Object) -> None:
if obj is self.grid[obj.x][obj.y]:
self.grid[obj.x][obj.y] = False
else:
raise Exception(f"The object {object.name} {object.id} does not exist in [{object.x},{object.y}]")
#Checks if the selected point is available to deploy a object
def checkPt(self, x, y) -> None:
if self.grid[x][y] is not False:
raise Plateau.CollisionDetectedError(f"The point [{x},{y}] is already occupied")
if x > self.Limits.x or y > self.Limits.y or x < 0 or y < 0:
raise Plateau.OutOfLimitsError(f"The point [{x},{y}] is beyond limits")