- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessCanMoveToPos.py
More file actions
Latest commit
64 lines (40 loc) · 1.68 KB
/
Copy pathchessCanMoveToPos.py
File metadata and controls
64 lines (40 loc) · 1.68 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
alphaToNum= {'a': 1, 'b': 2, 'c': 3,
'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8}
whiteHome=2
blackHome=7
defrook(pos1, pos2):
returnpos1[0] ==pos2[0] orpos1[1] ==pos2[1]
defbishop(pos1, pos2):
returnabs(alphaToNum[pos1[0]]-alphaToNum[pos2[0]]) ==abs(int(pos1[1])-int(pos2[1]))
defknight(pos1, pos2):
return (abs(int(pos1[1])-int(pos2[1])), abs(alphaToNum[pos1[0]]-alphaToNum[pos2[0]])) in [(1, 2), (2, 1)]
defqueen(pos1, pos2):
returnbishop(pos1, pos2) orrook(pos1, pos2)
defking(pos1, pos2):
return (abs(alphaToNum[pos1[0]]-alphaToNum[pos2[0]]), abs(int(pos1[1])-int(pos2[1]))) in [(0, 1), (1, 0), (1, 1)]
defpawn(pos1, pos2):
returnint(pos2[1])-int(pos1[1]) ==1andpos1[0] ==pos2[0]
defpawnWhite(pos1, pos2):
if (int(pos1[1]), int(pos2[1])) == (whiteHome, whiteHome+2) andpos1[0] ==pos2[0]:
returnTrue
returnpawn(pos1, pos2)
defpawnBlack(pos1, pos2):
if (int(pos1[1]), int(pos2[1])) == (blackHome, blackHome-2) andpos1[0] ==pos2[0]:
returnTrue
returnpawn(pos2, pos1)
defisPosInsideBoard(pos):
returnpos[0] inalphaToNumandint(pos[1]) inalphaToNum.values()
pieceFunctions= {'k': king, 'q': queen,
'n': knight, 'r': rook, 'b': bishop}
defisMoveValid(piece, pos1, pos2):
ifpos1==pos2ornot(len(pos1) ==len(pos2) ==2):
returnFalse
ifnotisPosInsideBoard(pos1) ornotisPosInsideBoard(pos2):
returnFalse
ifpiece=='p':
returnpawnBlack(pos1, pos2)
ifpiece=='P':
returnpawnWhite(pos1, pos2)
returnpieceFunctions[piece.lower()](pos1, pos2)
if__name__=="__main__":
print(isMoveValid('P', 'd2', 'd4'))