-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.py
44 lines (28 loc) · 943 Bytes
/
common.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
from collections import namedtuple
X_TO_CHAR = {i : chr(ord('A') + i) for i in range(19)}
CHAR_TO_X = {chr(ord('A') + i): i for i in range(19)}
def repr_direction(dir_func):
dx, dy = dir_func(0, 0) # differentiate
return '({}, {})'.format(dy, dx)
class Point(namedtuple('Point', ['x', 'y'])):
@staticmethod
def from_name(name):
x = CHAR_TO_X[name.capitalize()[0]]
y = int(name[1:])-1
return Point(x, y)
@property
def name(self):
return X_TO_CHAR[self.x] + str(self.y+1)
# some memory optimization
__slots__ = ()
def __str__(self):
return '{} ({},{})'.format(self.name, self.x, self.y)
class Debugger:
def __init__(self, enable_log=False):
self.enable_log = enable_log
def log(self, str):
if self.enable_log:
print(str)
def stop(self):
if self.enable_log:
input('[Press any key to resume]')