63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
# position implementation
|
|
|
|
class TwoDUtils:
|
|
mappings = {
|
|
'a': 0,
|
|
'b': 1,
|
|
'c': 2,
|
|
'd': 3,
|
|
'e': 4,
|
|
'f': 5,
|
|
'g': 6,
|
|
'h': 7,
|
|
'i': 8,
|
|
}
|
|
reverse = {i[1]:i[0] for i in mappings.items()}
|
|
def __init__(self):
|
|
pass
|
|
|
|
class TwoDPos:
|
|
g = 0
|
|
l = 1
|
|
def __init__(self, mode, param):
|
|
if mode == TwoDPos.g:
|
|
try:
|
|
self.pos = (
|
|
TwoDUtils.mappings[param[0]],
|
|
int(param[1]) - 1,
|
|
)
|
|
except KeyError:
|
|
self.pos = (
|
|
int(param[0]) - 1,
|
|
int(param[1]) - 1,
|
|
)
|
|
|
|
elif mode == TwoDPos.l:
|
|
self.pos = (
|
|
param[0] * 3 + param[2],
|
|
param[1] * 3 + param[3],
|
|
)
|
|
|
|
def __repr__(self):
|
|
return ' '.join([self.glob(), str(self.local())])
|
|
|
|
def glob(self):
|
|
ret = []
|
|
ret.append(TwoDUtils.reverse[self.pos[0]])
|
|
ret.append(str(self.pos[1] + 1))
|
|
|
|
return ''.join(ret)
|
|
|
|
def local(self):
|
|
subboard = (
|
|
int(self.pos[0] / 3),
|
|
int(self.pos[1] / 3),
|
|
)
|
|
|
|
localpos = (
|
|
self.pos[0] % 3,
|
|
self.pos[1] % 3,
|
|
)
|
|
|
|
return (subboard[0], subboard[1], localpos[0], localpos[1])
|