-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
203 lines (160 loc) · 5.12 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from enum import Enum
from typing import List
class Color(Enum):
BLACK = 1
WHITE = 2
def __str__(self):
if self.value == 1:
return 'b'
else:
return 'w'
class Piece(object):
"""implements a base class for pieces in a chess game"""
PIECE_TYPE_MAP = {
'K': 'King',
'Q': 'Queen',
'r': 'Rock',
'k': 'Knight',
'b': 'Bishop',
'p': 'Pawn'
}
def __init__(self, color):
self.name = None
self.moves = None
if color in ['w', 'b']:
self.color = Color.WHITE if color == 'w' else Color.BLACK
else:
raise NameError('invalid color')
def validate_vector(self, vector: List[int]):
"""validate vector with allowed moves of a certain piece"""
if not self.moves:
raise NotImplementedError('base class')
else:
return True
def can_jump(self):
return self.moves['jump']
def get_long_name(self):
return self.PIECE_TYPE_MAP[self.name]
@staticmethod
def _abs_vector(vector: List[int]) -> List[int]:
return [abs(i) for i in vector]
@staticmethod
def _abs_vector_equal(vector: List[int]) -> bool:
_absvector = Piece._abs_vector(vector)
if all(i == _absvector[0] for i in _absvector):
return True
else:
return False
@staticmethod
def unify_vector(vector: List[int]) -> List[int]:
"""unify a provided vector"""
absvector = Piece._abs_vector(vector)
absmax = max(absvector)
if absmax <= 1:
return vector
# if all abs values are equal
if Piece._abs_vector_equal(vector):
return [int(i/absmax) for i in vector]
# handle special case for Knight
if 1 in absvector and 2 in absvector:
return vector
# handle queen and rock straight moves
if 0 in vector:
return [int(i/absmax) for i in vector]
def __str__(self):
if self.color == 1:
_color = 'w'
else:
_color = 'b'
_str = "%s%s" % (self.color, self.name)
return _str
class Knight(Piece):
"""Implement specialization for Knights"""
def __init__(self, color):
super(Knight, self).__init__(color)
self.name = 'k'
self.moves = {
"directions": [[-2, -1], [-2, 1],
[-1, -2], [-1, 2],
[1, -2], [1, 2],
[2, -1], [2, 1]],
"steps": 1,
"jump": True
}
def validate_vector(self, vector: List[int]):
_unified_vector = Piece.unify_vector(vector)
for move in self.moves['directions']:
if move == _unified_vector:
return True
return False
class King(Piece):
"""Implement specialization for a king"""
def __init__(self, color):
super(King, self).__init__(color)
self.name = 'K'
self.moves = {
"directions": [[-1, -1], [-1, 1], [-1, 0],
[1, -1], [1, 1], [1, 0],
[0, 1], [0, -1]],
"steps": None,
"jump": False
}
class Queen(Piece):
"""Implement specialization for a queen"""
def __init__(self, color):
super(Queen, self).__init__(color)
self.name = 'Q'
self.moves = {
"directions": [[-1, -1], [-1, 1], [-1, 0],
[1, -1], [1, 1], [1, 0],
[0, 1], [0, -1]],
"steps": None,
"jump": False
}
class Rock(Piece):
"""Implement specialization for a rock"""
def __init__(self, color):
super(Rock, self).__init__(color)
self.name = 'r'
self.moves = {
"directions": [[-1, 0], [0, 1], [1, 0], [0, -1]],
"steps": None,
"jump": False
}
def validate_vector(self, vector: List[int]):
_unified_vector = Piece.unify_vector(vector)
"""validate vector with allowed moves for a rock"""
if not self.moves:
raise NotImplementedError('base class')
else:
for move in self.moves['directions']:
if move == _unified_vector:
return True
return False
class Bishop(Piece):
"""Implement specialization for a bishop"""
def __init__(self, color):
super(Bishop, self).__init__(color)
self.name = 'b'
self.moves = {
"directions": [[-1, 1], [1, 1], [1, -1], [-1,-1]],
"steps": None,
"jump": False
}
class Pawn(Piece):
"""Implement specialization for a pawn"""
def __init__(self, color):
super(Pawn, self).__init__(color)
self.name = 'p'
self.moves = {
"directions": [[-1, -1], [-1, 1], [-1, 0],
[1, -1], [1, 1], [1, 0],
[0, 1], [0, -1]],
"steps": 1,
"steps_start": 2,
"jump": False
}
class NoPiece(object):
"""Implement non-piece"""
def __init__(self):
pass