-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.py
More file actions
46 lines (38 loc) · 1.19 KB
/
Knight.py
File metadata and controls
46 lines (38 loc) · 1.19 KB
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
from __future__ import annotations
from typing import TYPE_CHECKING, Iterator
from Coordinate import Coordinate as C
from Move import Move
from Piece import Piece
if TYPE_CHECKING:
from Board import Board
WHITE = True
BLACK = False
class Knight(Piece):
stringRep = 'N'
value = 3
def __init__(
self, board: Board, side: bool, position: C, movesMade: int = 0
) -> None:
super(Knight, self).__init__(board, side, position)
self.movesMade = movesMade
def getPossibleMoves(self) -> Iterator[Move]:
board = self.board
currentPos = self.position
movements = [
C(2, 1),
C(2, -1),
C(-2, 1),
C(-2, -1),
C(1, 2),
C(1, -2),
C(-1, -2),
C(-1, 2),
]
for movement in movements:
newPos = currentPos + movement
if board.isValidPos(newPos):
pieceAtNewPos = board.pieceAtPosition(newPos)
if pieceAtNewPos is None:
yield Move(self, newPos)
elif pieceAtNewPos.side != self.side:
yield Move(self, newPos, pieceToCapture=pieceAtNewPos)