-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKnight.py
54 lines (44 loc) · 1.74 KB
/
Knight.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
from Piece import Piece
class Knight(Piece):
"""
Knight class inherit from Piece
"""
def canMove(self,board,x,y):
"""
param x : take the target x-axis coordinate
param y : take the target y-axis coordinate
"""
sourceX, sourceY, targetX, targetY = self.x, self.y, x, y
"""
since Knight can move only in L shape so that means it can be divide
into four coordinates |
++ | +-
-------------
-+ | --
|
from that we can understand and we can predict the next moves
"""
# -- side
if sourceX > targetX and sourceY > targetY:
if (targetX - sourceX) == -2 \
and (targetY - sourceY) == -1 \
or (targetX - sourceX) == -1 \
and (targetY - sourceY) == -2:
return True
# +- side
elif sourceX < targetX and sourceY > targetY:
if (targetX - sourceX) == 2 and (targetY - sourceY) == -1 \
or (targetX - sourceX) == 1 and (targetY - sourceY) == -2:
return True
# ++ side
elif sourceX < targetX and sourceY < targetY:
if (targetX - sourceX) == 2 and (targetY - sourceY) == 1 \
or (targetX - sourceX) == 1 and (targetY - sourceY) == 2:
return True
# -+ side
elif sourceX > targetX and sourceY < targetY:
if (targetX - sourceX) == -2 and (targetY - sourceY) == 1 \
or (targetX - sourceX) == -1 and (targetY - sourceY) == 2:
return True
else:
return False