-
Notifications
You must be signed in to change notification settings - Fork 3
/
HRMove.py
47 lines (39 loc) · 1.27 KB
/
HRMove.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
from HRConsts import DIR_NAMES
from HRException import InvalidMove
class HRMove:
def __init__(self, piece, dir):
self.piece = piece
if isinstance(dir[0], tuple):
self.dir = dir[:]
else:
self.dir = [dir[:]]
self.tried = False
self.applied = False
def __repr__(self):
ret_str = '#{0}'.format(self.piece.id)
for d in self.dir:
ret_str += ' {0}'.format(DIR_NAMES[d])
return ret_str
#'#{0} {1}'.format(self.piece.id, DIR_NAMES[self.dir])
def apply(self):
if not self.tried:
for d in self.dir:
self.piece.x += d[0]
self.piece.y += d[1]
self.tried = True
self.applied = True
def attempt(self):
if self.tried and self.applied: raise InvalidMove
for d in self.dir:
self.piece.x += d[0]
self.piece.y += d[1]
self.tried = True
def cancel(self):
if self.applied or not self.tried: raise InvalidMove
for d in reversed(self.dir):
self.piece.x -= d[0]
self.piece.y -= d[1]
self.tried = False
def append(self, new_dir):
if self.applied or self.tried: raise InvalidMove
self.dir.append(new_dir)