-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
179 lines (137 loc) · 4.22 KB
/
util.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
def checkTile(tiles, x, y, pieceColor, occupied=True, empty=True):
if not (0 <= x < 8 and 0 <= y < 8):
return False
tile = tiles[x][y]
if not tile.piece.code and empty:
return True
if tile.piece.code and tile.piece.color != pieceColor and occupied:
return True
return False
def getBlackPawnMoves(x, y, piece):
possibleMoves = []
color = piece.color
tiles = piece.parent().parent().tiles
if checkTile(tiles, x, y+1, color, occupied=False):
possibleMoves.append((x, y+1))
if checkTile(tiles, x, y+2, color, occupied=False):
if y == 1:
possibleMoves.append((x, y+2))
if checkTile(tiles, x+1, y+1, color, occupied=True, empty=False):
possibleMoves.append((x+1, y+1))
if checkTile(tiles, x-1, y+1, color, occupied=True, empty=False):
possibleMoves.append((x-1, y+1))
return possibleMoves
def getWhitePawnMoves(x, y, piece):
possibleMoves = []
color = piece.color
tiles = piece.parent().parent().tiles
if checkTile(tiles, x, y-1, color, occupied=False):
possibleMoves.append((x, y-1))
if checkTile(tiles, x, y-2, color, occupied=False):
if y == 6:
possibleMoves.append((x, y-2))
if checkTile(tiles, x+1, y-1, color, occupied=True, empty=False):
possibleMoves.append((x+1, y-1))
if checkTile(tiles, x-1, y-1, color, occupied=True, empty=False):
possibleMoves.append((x-1, y-1))
return possibleMoves
def getBishopMoves(x, y, piece):
possibleMoves = []
color = piece.color
tiles = piece.parent().parent().tiles
for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
for i in range(1, 9):
nx, ny = x+dx*i, y+dy*i
if not (0 <= nx < 8) or not (0 <= ny < 8):
break
if checkTile(tiles, nx, ny, color):
possibleMoves.append((nx, ny))
if tiles[nx][ny].piece.code:
break
return possibleMoves
def getRookMoves(x, y, piece):
possibleMoves = []
color = piece.color
tiles = piece.parent().parent().tiles
for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
for i in range(1, 9):
nx, ny = x+dx*i, y+dy*i
if not (0 <= nx < 8) or not (0 <= ny < 8):
break
if checkTile(tiles, nx, ny, color):
possibleMoves.append((nx, ny))
if tiles[nx][ny].piece.code:
break
return possibleMoves
def getKnightMoves(x, y, piece):
possibleMoves = []
color = piece.color
tiles = piece.parent().parent().tiles
for dx, dy in [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)]:
nx, ny = x+dx, y+dy
if checkTile(tiles, nx, ny, color):
possibleMoves.append((nx, ny))
return possibleMoves
def getKingMoves(x, y, piece):
possibleMoves = []
color = piece.color
tiles = piece.parent().parent().tiles
for dx, dy in [(1, 1), (1, -1), (-1, 1), (-1, -1), (1, 0), (0, 1), (-1, 0), (0, -1)]:
nx, ny = x+dx, y+dy
if checkTile(tiles, nx, ny, color):
possibleMoves.append((nx, ny))
return possibleMoves
def getQueenMoves(x, y, piece):
return getBishopMoves(x, y, piece) + getRookMoves(x, y, piece)
startingPieces = {
'a1': 'rd',
'b1': 'nd',
'c1': 'bd',
'd1': 'qd',
'e1': 'kd',
'f1': 'bd',
'g1': 'nd',
'h1': 'rd',
'a2': 'pd',
'b2': 'pd',
'c2': 'pd',
'd2': 'pd',
'e2': 'pd',
'f2': 'pd',
'g2': 'pd',
'h2': 'pd',
'a8': 'rl',
'b8': 'nl',
'c8': 'bl',
'd8': 'ql',
'e8': 'kl',
'f8': 'bl',
'g8': 'nl',
'h8': 'rl',
'a7': 'pl',
'b7': 'pl',
'c7': 'pl',
'd7': 'pl',
'e7': 'pl',
'f7': 'pl',
'g7': 'pl',
'h7': 'pl',
}
possibleMoves = {
'pd': getBlackPawnMoves,
'bd': getBishopMoves,
'rd': getRookMoves,
'nd': getKnightMoves,
'kd': getKingMoves,
'qd': getQueenMoves,
'pl': getWhitePawnMoves,
'bl': getBishopMoves,
'rl': getRookMoves,
'nl': getKnightMoves,
'kl': getKingMoves,
'ql': getQueenMoves,
None: lambda x, y, z: []
}
def rgb2hex(color):
r, g, b = color.r, color.g, color.b
return f"#{r:02x}{g:02x}{b:02x}"