-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.py
154 lines (123 loc) · 3.36 KB
/
pieces.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
from typing import List, Tuple, Set
from typing_chess import Square, Board
class Piece:
position: Tuple[int, int] | None
def __init__(self, position: Tuple[int, int]):
self.position = position
def getValidMoves(self, board: Board, position: Square) -> List[Square]:
print("Piece Not Specified")
return []
@staticmethod
def isInBoard(board: Board, position: List[int]):
[x, y] = position
if not (
x >= 0 and x <= board.size[0] - 1 and y >= 0 and y <= board.size[1] - 1
):
return False
return True
@staticmethod
def getValidOperation(
board: List[List[Square]], position: Square, operation: Tuple[int, int]
) -> Tuple[int, int] | None:
current_position = [position.position[0], position.position[1]]
current_position[0] += operation[0]
current_position[1] += operation[1]
if (
not Piece.isInBoard(board, current_position)
or board.board[current_position[0]][current_position[1]].piece
):
return None
return tuple(current_position)
@staticmethod
def getAllValidMovesForOperations(
board: Board,
position: Square,
operations: List[Tuple[int, int]],
getOne: bool = False,
) -> Set[Tuple[int, int]]:
valid_moves = set()
i = 1
trackings = [True] * len(operations)
while True:
for k in range(len(operations)):
if not trackings[k]:
continue
op = Piece.ValidOperation(
board, position, (operations[k][0] * i, operations[k][1] * i)
)
if op:
valid_moves.add(op)
else:
trackings[k] = False
for tracking in trackings:
if tracking:
break
else:
break
i += 1
if getOne:
break
return valid_moves
def getValidMoves(self, board: Board, position: Square) -> Set[Tuple[int, int]]:
return Piece.getAllValidMovesForOperations(
board, position, self.operations, self.oneMove
)
def __str__(self):
return f"-"
class Pawn(Piece):
operations = [(1, 0)]
oneMove = True
def __str__(self):
return f"P"
class Knight(Piece):
operations = [
(2, 1),
(2, -1),
(-2, 1),
(-2, -1),
(1, 2),
(-1, 2),
(1, -2),
(-1, -2),
]
oneMove = True
def __str__(self):
return f"N"
class Rook(Piece):
operations = [(0, 1), (0, -1), (1, 0), (-1, 0)]
oneMove = False
def __str__(self):
return f"R"
class Bishop(Piece):
operations = [(1, 1), (-1, -1), (1, -1), (-1, 1)]
oneMove = False
def __str__(self):
return f"B"
class Queen(Piece):
operations = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1),
(0, 1),
(0, -1),
(1, 0),
(-1, 0),
]
oneMove = False
def __str__(self):
return f"Q"
class King(Piece):
operations = [
(1, 1),
(-1, -1),
(1, -1),
(-1, 1),
(0, 1),
(0, -1),
(1, 0),
(-1, 0),
]
oneMove = True
def __str__(self):
return f"K"