ultimate/pos.py

63 lines
1.4 KiB
Python
Raw Permalink Normal View History

2022-07-06 23:55:40 -05:00
# 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:
2022-09-14 19:56:11 -05:00
try:
self.pos = (
TwoDUtils.mappings[param[0]],
int(param[1]) - 1,
)
except KeyError:
self.pos = (
int(param[0]) - 1,
int(param[1]) - 1,
)
2022-07-06 23:55:40 -05:00
elif mode == TwoDPos.l:
self.pos = (
param[0] * 3 + param[2],
param[1] * 3 + param[3],
2022-07-06 23:55:40 -05:00
)
def __repr__(self):
return ' '.join([self.glob(), str(self.local())])
2022-07-06 23:55:40 -05:00
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])