/
definitions.py
153 lines (114 loc) · 3.8 KB
/
definitions.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from enum import Enum, auto
from collections import namedtuple
INTRO = '''
##########################
######## chess-py ########
##########################
#
###
####
#### ##
#### ####
## ###
####
##########
####
####
####
####
##############
##############
'''
# Starting position in FEN format
START_POSITION = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
# Board dimensions
DIM = 8
DIM_ZERO = 7
# Directions of movements
DIAGONAL = [(1, 1), (1, -1), (-1, -1), (-1, 1)]
HORIZONTAL_VERTICAL = [(1, 0), (-1, 0), (0, 1), (0, -1)]
ALL_DIRECTIONS = DIAGONAL + HORIZONTAL_VERTICAL
KNIGHT = [(1, 2), (-1, 2), (-1, -2), (1, -2), (2, 1), (-2, 1), (-2, -1), (2, -1)]
class BoardStatus(Enum):
Normal = auto(),
Check = auto(),
Checkmate = auto(),
Stalemate = auto()
class Colors(Enum):
Black = 1,
White = auto()
def other_color(self):
return Colors.Black if self == Colors.White else Colors.White
def __str__(self):
return "black" if self == Colors.Black else "white"
# Named tuple of coordinates
Coords = namedtuple("Coords", "r, c")
# Print Coords as regular tuples
def print_coords(self):
t = tuple(self)
return str(t)
Coords.__str__ = print_coords
Coords.__repr__ = Coords.__str__
# Check if coordinates on board
def on_board(coords):
for coord in coords:
if coord < 0 or coord > DIM_ZERO:
return False
return True
class Piece:
def __init__(self, color, text, moves_counter = 0):
self.color = color
self.text = text
self.moves_counter = moves_counter
self.moves = set()
self.threatens = set()
def __str__(self):
color = "w" if self.color == Colors.White else "b"
return color + self.text
__repr__ = __str__
class Pawn(Piece):
def __init__(self, color, moves_counter = 0):
super().__init__(color, "P", moves_counter = 0)
class Rook(Piece):
def __init__(self, color, moves_counter = 0):
super().__init__(color, "R", moves_counter = 0)
self.can_castle = True
class Knight(Piece):
def __init__(self, color, moves_counter = 0):
super().__init__(color, "N", moves_counter = 0)
class Bishop(Piece):
def __init__(self, color, moves_counter = 0):
super().__init__(color, "B", moves_counter = 0)
class Queen(Piece):
def __init__(self, color, moves_counter = 0):
super().__init__(color, "Q", moves_counter = 0)
class King(Piece):
def __init__(self, color, moves_counter = 0):
super().__init__(color, "K", moves_counter = 0)
self.can_castle = True
class Square:
def __init__(self, coords: Coords):
self.piece = None
self.coords = coords
def __str__(self):
return self.piece.__str__() if self.piece else " "
__repr__ = __str__
class Record:
def __init__(self, coords: Coords, piece):
self.coords = coords
self.piece_type = type(piece) if piece else None
self.color = piece.color if piece else None
self.moves_counter = piece.moves_counter if piece else None
def __str__(self) -> str:
return (" ".join(map(str, [self.coords,
self.piece_type,
self.color,
self.moves_counter]))
)
__repr__ = __str__
PROMOTE = [Queen, Rook, Bishop, Knight]
CHAR_PROMOTE = dict([(c, piece) for (c, piece) in zip("QRBN", PROMOTE)])
if __name__=="__main__":
print(Record(Coords(1,2), Pawn(Colors.Black)))
print(Record(Coords(1,2), None))
print(CHAR_PROMOTE)