-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_min_max.py
290 lines (267 loc) · 8.52 KB
/
best_min_max.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import math
import time
from operator import itemgetter
import threading
import chess_game as w
from chess_game import GameObject, GameInterface
import copy
import multiprocessing as mp
from multiprocessing import Pool
import random
class Moves(GameObject):
def __init__(self, board):
super(Moves, self).__init__()
self.temp_obj = GameObject()
self.board = board
def check_check(self, turn):
[rq, cq] = self.get_king_position(turn)
if self.checkByPawn(rq, cq, turn):
return True
elif self.checkByQueen(rq, cq, turn):
return True
elif self.checkByKnight(rq, cq, turn):
return True
elif self.checkByRook(rq, cq, turn):
return True
elif self.checkByBishop(rq, cq, turn):
return True
else:
return False
def knight_action(self, turn):
e = "Nb"
if turn:
e = "Nw"
p = []
m = self.knight_possible_moves(turn)
for i in range(0, len(m)):
x = m[i][0]
y = m[i][1]
if len(m[i]) > 2:
for j in range(0, len(m[i]) - 2, 2):
x1 = m[i][2 + j]
y1 = m[i][3 + j]
b = self.copyBoard()
self.temp_obj.undo(b)
self.temp_obj.move(e, x, y, x1, y1)
if self.temp_obj.check_check(turn):
pass
else:
c = self.temp_obj.cost_evaluation()
p.append([e, [x, y], [x1, y1], c])
return p
def action_list(self, e, m, turn):
p = []
for i in range(0, int(len(m) / 6)):
x = m[0 + i * 6]
y = m[1 + i * 6]
for j in range(2, 6):
l = m[j + 6 * i]
for k in range(0, len(l), 2):
x1 = l[0 + k]
y1 = l[1 + k]
b = self.copyBoard()
self.temp_obj.undo(b)
self.temp_obj.move(e, x, y, x1, y1)
if self.temp_obj.check_check(turn):
pass
else:
c = self.temp_obj.cost_evaluation()
p.append([e, [x, y], [x1, y1], c])
return p
def bishop_action(self, turn):
e = "Bb"
if turn:
e = "Bw"
m = self.bishop_possible_moves(e, turn)
p=self.action_list(e, m, turn)
return p
def rook_action(self, turn):
e = "Rb"
if turn:
e = "Rw"
m = self.rook_possible_moves(e, turn)
p = self.action_list(e, m, turn)
return p
def queen_action(self, turn):
e = "Qb"
if turn:
e = "Qw"
m = self.queen_possible_moves(turn)
p = []
for i in range(0, int(len(m) / 12)):
l = m[i * 12:12 + 12 * i]
p = p + self.action_list(e, l, turn)
return p
def pawn_action(self, turn):
e = "Pb"
if turn:
e = "Pw"
p = []
m = self.pawns_possible_moves(turn)
length = len(m)
for i in range(0, length):
x = m[i][0]
y = m[i][1]
for j in range(2, len(m[i]), 2):
x1 = m[i][j]
y1 = m[i][j + 1]
b = self.copyBoard()
self.temp_obj.undo(b)
self.temp_obj.move(e, x, y, x1, y1)
if self.temp_obj.check_check(turn):
pass
else:
c = self.temp_obj.cost_evaluation()
p.append([e, [x, y], [x1, y1], c])
return p
def king_action(self, turn):
e = "Kb"
if turn:
e = "Kw"
p = []
m = self.king_possible_moves(turn)
x = m[0][0]
y = m[0][1]
for i in range(1, len(m)):
x1 = m[i][0]
y1 = m[i][1]
b = self.copyBoard()
self.temp_obj.undo(b)
self.temp_obj.move(e, x, y, x1, y1)
if self.temp_obj.check_check(turn):
pass
else:
c = self.temp_obj.cost_evaluation()
p.append([e, [x, y], [x1, y1], c])
return p
def total_action(self, turn):
q = self.queen_action(turn)
n = self.knight_action(turn)
bs = self.bishop_action(turn)
r = self.rook_action(turn)
p = self.pawn_action(turn)
k = self.king_action(turn)
t = p + r + q + n + bs + k
return t
class AI_move():
def __init__(self):
self.temp = GameObject()
self.b = [] # board
self.movesObj = Moves(self.b)
self.turn = False
def set_board(self,board):
self.b = board
def best_min_max(self,a):
tempG = GameObject()
# depth = 1
tempo = copy.deepcopy(self.b)
tempG.set_board(tempo)
tempG.move(a[0], a[1][0], a[1][1], a[2][0], a[2][1], False)
value = -math.inf
move = [0,[],[],value]
b2 = tempG.copyBoard()
m = Moves(b2)
l2 = m.total_action(True)
try:
b = sorted(l2, key=itemgetter(3), reverse=False)[0] # F
except:
return move
#print("l2",l2)
tempo1 = tempG.copyBoard()
tempG.move(b[0], b[1][0], b[1][1], b[2][0], b[2][1], True)
b2 = tempG.copyBoard()
m = Moves(b2)
l3 = m.total_action(False)
if len(l3) == 0:
return move
try:
c = sorted(l3, key=itemgetter(3), reverse=True)[0]
except:
return move
#print("l3",l3)
tempo2 = tempG.copyBoard()
tempG.move(c[0], c[1][0], c[1][1], c[2][0], c[2][1], False)
b2 = tempG.copyBoard()
m = Moves(b2)
l4 = m.total_action(True)
try:
d = sorted(l4, key=itemgetter(3), reverse=False)[0] # F
except:
return move
#print("l4",l4)
tempo3 = tempG.copyBoard()
tempG.move(d[0], d[1][0], d[1][1], d[2][0], d[2][1], True)
b2 = tempG.copyBoard()
m = Moves(b2)
l5 = m.total_action(False)
try:
e = sorted(l5, key=itemgetter(3), reverse=True)[0] # T
except:
return move
tempo4 = tempG.copyBoard()
tempG.move(e[0], e[1][0], e[1][1], e[2][0], e[2][1], False)
b2 = tempG.copyBoard()
m = Moves(b2)
l6 = m.total_action(False)
try:
f = sorted(l6, key=itemgetter(3), reverse=False)[0] # F
except:
return move
tempo5 = tempG.copyBoard()
tempG.move(f[0], f[1][0], f[1][1], f[2][0], f[2][1], True)
b2 = tempG.copyBoard()
m = Moves(b2)
l7 = m.total_action(False)
try:
f = sorted(l7, key=itemgetter(3), reverse=True)[0] # T
except:
return move
#print("l5",l5)
tempG.undo(tempo5)
tempG.undo(tempo4)
tempG.undo(tempo3)
tempG.undo(tempo2)
tempG.undo(tempo1)
tempG.undo(tempo)
return f+a
if __name__ == '__main__':
g = GameInterface()
while True:
white_play = True
while white_play:
p = input('Enter the piece:')
xi = input('Enter piece initial position:')
xf = input('Enter piece final position:')
white_play = g.input(p,xi,xf)
if white_play:
print("Enter correct inputs")
########
b = g.deep_board()
m = Moves(b)
t = m.total_action(False)
print(t)
if len(t) == 0:
print("CHECKMATE")
break
else:
a = AI_move()
a.set_board(b)
start = time.time()
pool = Pool(mp.cpu_count())
outputs_async = pool.map_async(a.best_min_max, t)
outputs = outputs_async.get()
# print("Output: {}".format(outputs))
moves = sorted(outputs, key=itemgetter(3), reverse=True)
move_list = []
for x in moves:
if moves[0][3] == x[3]:
move_list.append(x)
random.shuffle(move_list)
move = move_list[0]
# print(outputs)
# print(move[0],move[1][0])
end = time.time()
print("time", end - start)
print('move',move)
# print(move[4], move[5][0], move[5][1], move[6][0], move[6][1])
g.inputAiMove(move[4], move[5][0], move[5][1], move[6][0], move[6][1])