-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckmate.py
225 lines (184 loc) · 6.69 KB
/
Checkmate.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import numpy as np
from PIL import Image
from PIL import ImageChops
from PIL import ImageStat
import os
import time
def read_corner_fast(image):
h, w, _ = image.shape
for i in range(h):
if sum(sum(image[i]))!=0:
for j in range(w):
if sum(image[i][j])!=0:
break
else:
continue
break
return i, j
def table_state(chess, white_set, black_set):
figure_names = {'pawn': "P", 'knight':"N", 'bishop':"B", "rook" :"R", "queen": "Q", 'king':"K" }
sl = int((chess.shape)[0]/8)
string = ''
white_positions ={}
black_positions = {}
for i in range(8):
row = chess[i*sl: (i+1)*sl, :, :]
z = 0
for j in range(8):
square = row[:, j*sl:(j+1)*sl, :]
results = np.all(square == square[0])
if results == True:
z+=1
else:
if z !=0:
string = string + '%s'%z
#figure = match_pattern(square)
im = Image.fromarray(square).convert('1')
statistics_black = {}
bs = black_set
for key in bs:
diff1 = ImageChops.difference(im, bs[key][0])
diff2 = ImageChops.difference(im, bs[key][1])
stat = ImageStat.Stat(diff1)
diff1_ratio = sum(stat.mean) / (len(stat.mean) * 255)
stat = ImageStat.Stat(diff2)
diff2_ratio = sum(stat.mean) / (len(stat.mean) * 255)
statistics_black[key] = min(diff1_ratio, diff2_ratio)
bs = white_set
statistics_white = {}
for key in bs:
diff1 = ImageChops.difference(im, bs[key][0])
diff2 = ImageChops.difference(im, bs[key][1])
stat = ImageStat.Stat(diff1)
diff1_ratio = sum(stat.mean) / (len(stat.mean) * 255)
stat = ImageStat.Stat(diff2)
diff2_ratio = sum(stat.mean) / (len(stat.mean) * 255)
statistics_white[key] = min(diff1_ratio, diff2_ratio)
wm = min(statistics_white.items(), key=lambda x: x[1])
bm = min(statistics_black.items(), key=lambda x: x[1])
if wm[1] < bm[1]:
F = figure_names[wm[0]]
current = white_positions.get(wm[0], [])
current.append((i,j))
white_positions[wm[0]] = current
else:
F = figure_names[bm[0]].lower()
current = black_positions.get(bm[0], [])
current.append((i,j))
black_positions[bm[0]] = current
string = string + str(F)#'F'
z=0
if z!=0:
string = string + '%s'%z
if i!=7:
string = string+'/'
return string, white_positions, black_positions
def merge_figures( figure, sl):
black_tile_path = os.path.join(folder_path, 'tiles/black.png')
white_tile_path = os.path.join(folder_path, 'tiles/white.png')
black_tile = Image.open(black_tile_path).convert("RGBA")
white_tile = Image.open(white_tile_path).convert("RGBA")
black_tile.paste(figure, (0,0), figure)
black_tile = black_tile.resize((sl, sl)).convert('1')
white_tile.paste(figure, (0,0), figure)
white_tile = white_tile.resize((sl, sl)).convert('1')
return black_tile, white_tile
def set_figures(figures_path, sl):
player = {}
for figure in ['king', 'bishop', 'knight', 'pawn', 'queen', 'rook']:
fig_path = os.path.join(figures_path, '%s.png'%figure)
fig = Image.open(fig_path).convert('RGBA')
k1, k2 = merge_figures(fig, sl)
player[figure] = [k1, k2]
return player
def check_king(fig, color, I,J, i, j):
if fig=='king':
f = lambda I,J,i,j: (abs(I-i) == 1 and J==j) or (I==i and abs(J-j)==1)
if f(I,J,i,j):return 1
else: return 0
if fig=='rook':
f = lambda I, J, i, j: (I==i) or (J==j)
if f(I,J,i,j):
return 1
else:
return 0
if fig=='bishop':
f = lambda I, J, i, j: abs(I-i) == abs(J-j)
if f(I,J,i,j):
return 1
else:
return 0
if fig=='queen':
f = lambda I, J, i, j: (abs(I-i) == abs(J-j)) or (I==i) or (J==j)
if f(I,J,i,j):
return 1
else:
return 0
if fig=='knight':
f = lambda I, J, i, j: ((abs(I-i)==2) and (abs(J-j)==1)) or ((abs(I-i)==1) and (abs(J-j)==2))
if f(I,J,i,j):
return 1
else:
return 0
if fig=='pawn':
if color == 'black':
f = lambda I, J, i, j: (( I-i) == 1) and (abs(J-j) == 1)
if f(I,J,i,j):
return 1
else:
return 0
if color == 'white':
f = lambda I, J, i, j: (( I-i) == -1) and (abs(J-j) == 1)
if f(I,J,i,j):
return 1
else:
return 0
if __name__ == "__main__":
folder_path = input()
test= os.path.split(folder_path)[-1]
image_path = os.path.join(folder_path, '%s.png'%test)
image_file = Image.open(image_path)
image = np.array(image_file)
H, W = read_corner_fast(image)
print('%s,%s'%(H,W))
h, w, _ = image.shape
row = image[H]
for l in range(W, w):
if (sum(row[l]))==0:
break
else:
continue
lenght = l-W
chess_x = image[ H:H+lenght, :, :]
chess = chess_x[:, W: W+lenght, :]
sl = int((chess.shape)[0]/8)
figures_path1 = os.path.join(folder_path, 'pieces', 'white')
white_set = set_figures(figures_path1, sl)
figures_path2 = os.path.join(folder_path, 'pieces', 'black')
black_set = set_figures(figures_path2, sl)
states, wpos, bpos = table_state(chess, white_set, black_set)
print(states)
black_checks = 0
color = 'black'
if 'king' in wpos.keys():
I, J = wpos['king'][0]
for key in bpos:
for nF in bpos[key]:
i, j = nF
black_checks += check_king(key, color, I, J, i,j)
white_checks =0
color = 'white'
if 'king' in bpos.keys():
I, J = bpos['king'][0]
for key in wpos:
for nF in wpos[key]:
i, j = nF
white_checks += check_king(key, color, I, J, i,j)
if white_checks == 0 and black_checks==0:
print('-')
else:
if white_checks>0:
print('W')
else:
if black_checks>0:
print('B')