Skip to content

Instantly share code, notes, and snippets.

@safhac
Created December 5, 2021 16:02
Show Gist options
  • Save safhac/5ca3bce38b68c721cec10f01b34a8d05 to your computer and use it in GitHub Desktop.
Save safhac/5ca3bce38b68c721cec10f01b34a8d05 to your computer and use it in GitHub Desktop.
chess pieces
class ChessBoard:
size: int = 7
class ChessPiece:
def __init__(self):
self.dead = False
class Horse(ChessPiece):
def __init__(self, x, y):
self.moves = [(1, -2), (1, 2), (2, 1), (2, -1), (-1, 2), (-1, -2), (-2, 1), (-2, -1)]
self.x = x
self.y = y
self.dead = False
def possible_moves(self):
return [(self.x + x, self.y + y) for x, y in self.moves]
def move(self, x, y):
if (x, y) in self.possible_moves() and all([self.x + x, self.y + y] in range(ChessBoard.size + 1)):
self.x = x
self.y = y
else:
# raise
return 'illegal move'
def player_move(p, x, y):
try:
p.move(x, y)
except BaseException as e:
print(e.args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment