This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.py
159 lines (110 loc) · 4.61 KB
/
position.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
__author__ = 'tenpaMk2'
# FIXME 方角が文字の場合と単語の場合とで別々に生成するのは良くなさそう。一括で管理できないかな。
# FIXME とりあえず創っちゃったけど、後で整理しないとな。
class DIRECTION(object):
north = 0
east = 1
south = 2
west = 3
NUM_TO_DIRECTION = dict(enumerate([
'n',
'e',
's',
'w']))
DIRECTION_TO_NUM = dict(zip(NUM_TO_DIRECTION.values(), NUM_TO_DIRECTION.keys()))
DIRECTION_WORD = ['North', 'East', 'South', 'West']
DIRECTION_MOVE = ((-1, 0), (0, 1), (1, 0), (0, -1))
def get_north_pos_of(pos: list) -> list:
return get_direction_pos_of(pos, DIRECTION.north)
def get_east_pos_of(pos: list) -> list:
return get_direction_pos_of(pos, DIRECTION.east)
def get_south_pos_of(pos: list) -> list:
return get_direction_pos_of(pos, DIRECTION.south)
def get_west_pos_of(pos: list) -> list:
return get_direction_pos_of(pos, DIRECTION.west)
def get_direction_pos_of(pos: list, direction) -> list:
return pos[0] + DIRECTION_MOVE[direction][0], pos[1] + DIRECTION_MOVE[direction][1]
def get_direction_poses_of(pos: list) -> list:
return (get_direction_pos_of(pos, direction) for direction in (DIRECTION.north,
DIRECTION.east,
DIRECTION.south,
DIRECTION.west))
class PositionAndDirectionFactory(object):
@staticmethod
def make_north_pos_and_dir(position):
return PositionAndDirection(position, DIRECTION_TO_NUM['n'])
@staticmethod
def make_east_pos_and_dir(position):
return PositionAndDirection(position, DIRECTION_TO_NUM['e'])
@staticmethod
def make_south_pos_and_dir(position):
return PositionAndDirection(position, DIRECTION_TO_NUM['s'])
@staticmethod
def make_west_pos_and_dir(position):
return PositionAndDirection(position, DIRECTION_TO_NUM['w'])
class PositionAndDirection(object):
def __init__(self, position, direction=DIRECTION_TO_NUM['n']):
self.position = position
self.direction = direction
def _move_towards(self, direction):
self.direction = direction
self.position[0] += DIRECTION_MOVE[direction][0]
self.position[1] += DIRECTION_MOVE[direction][1]
def move_north(self):
self._move_towards(DIRECTION_TO_NUM['n'])
def move_east(self):
self._move_towards(DIRECTION_TO_NUM['e'])
def move_south(self):
self._move_towards(DIRECTION_TO_NUM['s'])
def move_west(self):
self._move_towards(DIRECTION_TO_NUM['w'])
def turn_north(self):
self.direction = DIRECTION_TO_NUM['n']
def turn_east(self):
self.direction = DIRECTION_TO_NUM['e']
def turn_south(self):
self.direction = DIRECTION_TO_NUM['s']
def turn_west(self):
self.direction = DIRECTION_TO_NUM['w']
def run(self):
self._move_towards(self.direction)
def set_direction(self, direction):
self.direction = direction
def get_front_position(self):
front_y = self.position[0] + DIRECTION_MOVE[self.direction][0]
front_x = self.position[1] + DIRECTION_MOVE[self.direction][1]
return (front_y, front_x)
def get_position(self):
# list関数によってdeepcopyしないと、呼出し元でpositionを別の変数hogeに読みこんだとき、
# hogeが常にリアルタイムなpositionの値を参照してしまう。
return tuple(self.position)
def get_north_position(self):
return get_north_pos_of(self.position)
def get_east_position(self):
return get_east_pos_of(self.position)
def get_south_position(self):
return get_south_pos_of(self.position)
def get_west_position(self):
return get_west_pos_of(self.position)
@property
def position(self):
return self._position
@position.setter
def position(self, position):
if not len(position) == 2:
raise ValueError("value must have 2 elements")
self._position = list(position)
@property
def direction(self):
return self._direction
@direction.setter
def direction(self, direction):
if not 0 <= direction <= 3:
raise ValueError("direction must be [0, 3]")
self._direction = direction
def get_direction_by_character(self):
return NUM_TO_DIRECTION[self.direction]
def get_direction_by_word(self):
return DIRECTION_WORD[self.direction]