chess-ai-py/vector.py
2022-06-17 19:30:09 -04:00

46 lines
1.5 KiB
Python

rows = '12345678'
cols = 'ABCDEFGH'
#class vec2:
# def __init__(self, x, y):
# self.x = int(x)
# self.y = int(y)
# def __add__(self, other):
# return vec2(self.x + other.x, self.y + other.y)
# def __sub__(self, other):
# return vec2(self.x - other.x, self.y - other.y)
# def __mul__(self, scalar):
# return vec2(self.x * scalar, self.y * scalar)
# def __repr__(self):
# return 'vec2({},{})'.format(self.x, self.y)
# def __eq__(self, other):
# return self.x == other.x and self.y == other.y
class vec2:
def __init__(self, x, y):
x, y = int(x), int(y)
# if not 0 <= x < 8:
# raise Exception('invalid chessvec x: {}'.format(repr(x)))
# if not 0 <= y < 8:
# raise Exception('invalid chessvec y: {}'.format(repr(y)))
self.x = x
self.y = y
def __add__(self, other): # Add vec2
return vec2(self.x + other.x, self.y + other.y)
def __sub__(self, other): # Subtract vec2
return vec2(self.x - other.x, self.y - other.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __hash__(self):
return hash(self.x) + hash(self.y)
def __repr__(self):
return 'vec2({},{})'.format(self.x, self.y)
def __str__(self):
return cols[self.x] + rows[self.y] if self.is_valid() else repr(self)
def is_valid(self):
return 0 <= self.x < 8 and 0 <= self.y < 8
def vec2_from_text(s):
return vec2(cols.index(s[0]), rows.index(s[1]))