-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirection.py
More file actions
58 lines (46 loc) · 1.55 KB
/
direction.py
File metadata and controls
58 lines (46 loc) · 1.55 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
53
54
55
56
57
58
from random import choice
class Direction:
def __init__(self, name, letter, vector=None, opposite=None):
self.vector = vector
self.letter = letter
self.name = name
if opposite is not None:
self.opposite = opposite
opposite.opposite = self
def __eq__(self, other):
try:
if self.name == other.name:
return True
else:
return False
except AttributeError:
return False
def __str__(self):
return self.name
north = Direction("north", "n", (0, 1))
south = Direction("south", "s", (0, -1), opposite=north)
east = Direction("east", "e", (1, 0))
west = Direction("west", "w", (-1, 0), opposite=east)
up = Direction("up", "u")
down = Direction("down", "d", opposite=up)
cardinal_directions = [north, south, east, west]
direction_list = [north, south, east, west, up, down]
def random(up_and_down=False):
if up_and_down:
return choice(direction_list)
else:
return choice(cardinal_directions)
letter_dict = {direct.letter: direct for direct in direction_list}
name_dict = {direct.name: direct for direct in direction_list}
# The good way: full_dict = {**letter_dict, **name_dict}
# This crappy implementation is for compatibility with the old python at work.
full_dict = {}
for i in letter_dict.keys():
full_dict[i] = letter_dict[i]
for i in name_dict.keys():
full_dict[i] = name_dict[i]
def from_string(string):
try:
return full_dict[string]
except KeyError:
return None