forked from ryanwilsonperkin/rushhour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.py
63 lines (51 loc) · 1.94 KB
/
vehicle.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
CAR_IDS = {'X', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'}
TRUCK_IDS = {'O', 'P', 'Q', 'R'}
class Vehicle(object):
"""A configuration of a single vehicle."""
def __init__(self, id, x, y, orientation):
"""Create a new vehicle.
Arguments:
id: a valid car or truck id character
x: the x coordinate of the top left corner of the vehicle (0-5)
y: the y coordinate of the top left corner of the vehicle (0-5)
orientation: either the vehicle is vertical (V) or horizontal (H)
Exceptions:
ValueError: on invalid id, x, y, or orientation
"""
if id in CAR_IDS:
self.id = id
self.length = 2
elif id in TRUCK_IDS:
self.id = id
self.length = 3
else:
raise ValueError('Invalid id {0}'.format(id))
if 0 <= x <= 5:
self.x = x
else:
raise ValueError('Invalid x {0}'.format(x))
if 0 <= y <= 5:
self.y = y
else:
raise ValueError('Invalid y {0}'.format(y))
if orientation == 'H':
self.orientation = orientation
x_end = self.x + (self.length - 1)
y_end = self.y
elif orientation == 'V':
self.orientation = orientation
x_end = self.x
y_end = self.y + (self.length - 1)
else:
raise ValueError('Invalid orientation {0}'.format(orientation))
if x_end > 5 or y_end > 5:
raise ValueError('Invalid configuration')
def __hash__(self):
return hash(self.__repr__())
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "Vehicle({0}, {1}, {2}, {3})".format(self.id, self.x, self.y,
self.orientation)