-
Notifications
You must be signed in to change notification settings - Fork 1
/
piece.py
43 lines (31 loc) · 1.25 KB
/
piece.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 typing import Any
from color import Color
letter_memo = {}
def to_letter(coordinate: int) -> str:
"""Returns the i coordinate as a letter"""
if coordinate not in letter_memo:
letters = "a b c d e f g h".split(" ")
letter_memo[coordinate] = letters[coordinate - 1]
return letter_memo[coordinate]
class Piece:
"""Represents a piece on the board"""
def __init__(self, color: Color, name: str, i: int, j: int, moved: bool = False, en_passant: bool = False):
self.color = color
self.name = name
self.i = i
self.j = j
self.moved = moved
self.en_passant = en_passant
def __eq__(self, obj: Any) -> bool:
return (
isinstance(obj, Piece)
and obj.i == self.i
and obj.j == self.j
)
def __hash__(self) -> int:
return hash((self.i, self.j))
def __str__(self) -> str:
return f'{self.name}{"B" if self.color == Color.BLACK else "W"}: {to_letter(self.i)}{self.j}'
def copy_piece_edit_en_passant(original: Piece, en_passant: bool) -> Piece:
"""Copy the given piece modifying the en_passant value"""
return Piece(original.color, original.name, original.i, original.j, original.moved, en_passant)