-
Notifications
You must be signed in to change notification settings - Fork 0
/
mybot.py
539 lines (515 loc) · 15.7 KB
/
mybot.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
board = []
bot_is_white=0
def init_board():
for i in range(0, 8):
board.append([])
for j in range(0, 8):
board[i].append(-1)
def read_board():
global bot_is_white
global board
board_file = open("board_file", 'r')
line = board_file.readline()
bot_is_white=ord(line[0]) - 48
line = board_file.readline().split(' ')
while(line[0]!=''):
piece = line[0]
position = line[1]
i = 8 - (ord(position[1]) - 48)
j = ord(position[0]) - 97
board[i][j]=int(piece)
line = board_file.readline().split(' ')
# get moves
def possible_moves(i, j, piece):
code = get_code(piece)
col = get_col(piece)
if code == 5: # for pawn
return pawn_movelist(i, j, col)
elif code == 4:
return rook_movelist(i, j, col)
elif code == 3:
return knight_movelist(i, j, col)
elif code == 2:
return bishop_movelist(i, j, col)
elif code == 1:
return queen_movelist(i, j, col)
else:
return []
def pawn_movelist(i, j, col):
movelist = []
if col == 1:
if board[i - 1][j] == -1:
movelist.append(((i - 1, j),(i,j)))
if (i == 6 and board[i - 2][j] == -1):
movelist.append(((i - 2, j),(i,j)))
if (j > 0):
if get_col(board[i - 1][j - 1]) == 0:
movelist.append(((i - 1, j - 1),(i,j)))
if (j < 7):
if get_col(board[i - 1][j + 1]) == 0:
movelist.append(((i - 1, j + 1),(i,j)))
else:
if board[i + 1][j] == -1:
movelist.append(((i + 1, j),(i,j)))
if (i == 1 and board[i + 2][j] == -1):
movelist.append(((i + 2, j),(i,j)))
if (j > 0):
if get_col(board[i + 1][j - 1]) == 1:
movelist.append(((i + 1, j - 1),(i,j)))
if (j < 7):
if get_col(board[i + 1][j + 1]) == 1:
movelist.append(((i + 1, j + 1),(i,j)))
return movelist
def rook_movelist(i, j, col):
movelist = []
temp_j = j - 1
if col == 1:
col = 0
else:
col = 1
while (temp_j >= 0 and board[i][temp_j] == -1):
movelist.append(((i, temp_j),(i,j)))
temp_j -= 1
if (temp_j >= 0 and get_col(board[i][temp_j]) == col ): # kill
movelist.append(((i, temp_j),(i,j)))
# for right direction
temp_j = j + 1
while (temp_j < 8 and board[i][temp_j] == -1):
movelist.append(((i, temp_j),(i,j)))
temp_j += 1
if (temp_j < 8 and get_col(board[i][temp_j]) == col ): # kill
movelist.append(((i, temp_j),(i,j)))
# for down
tempi = i + 1
while tempi < 8 and board[tempi][j] == -1:
movelist.append(((tempi, j),(i,j)))
tempi += 1
if (tempi < 8 and get_col(board[tempi][j]) == col): # kill
movelist.append(((tempi, j),(i,j)))
# for up
tempi = i - 1
while tempi >= 0 and board[tempi][j] == -1:
movelist.append(((tempi, j),(i,j)))
tempi -= 1
if (tempi >= 0 and get_col(board[tempi][j]) == col ): # kill
movelist.append(((tempi, j),(i,j)))
return movelist
def bishop_movelist(i, j, col):
movelist = []
if col == 1:
col = 0
else:
col = 1
# for north east
tempi = i - 1
temp_j = j + 1
while (tempi >= 0 and temp_j < 8 and board[tempi][temp_j] == -1):
movelist.append(((tempi, temp_j),(i,j)))
tempi -= 1
temp_j += 1
if (tempi >= 0 and temp_j < 8 and get_col(board[tempi][temp_j]) == col): # kill
movelist.append(((tempi, temp_j),(i,j)))
# for north west
tempi = i - 1
temp_j = j - 1
while (tempi >= 0 and temp_j >= 0 and board[tempi][temp_j] == -1):
movelist.append(((tempi, temp_j),(i,j)))
tempi -= 1
temp_j -= 1
if (tempi >= 0 and temp_j >= 0 and get_col(board[tempi][temp_j]) == col): # kill
movelist.append(((tempi, temp_j),(i,j)))
# south east
tempi = i + 1
temp_j = j + 1
while (tempi < 8 and temp_j < 8 and board[tempi][temp_j] == -1):
movelist.append(((tempi, temp_j),(i,j)))
tempi += 1
temp_j += 1
if (tempi < 8 and temp_j < 8 and get_col(board[tempi][temp_j]) == col): # kill
movelist.append(((tempi, temp_j),(i,j)))
# south west
tempi = i + 1
temp_j = j - 1
while (tempi < 8 and temp_j >= 0 and board[tempi][temp_j] == -1):
movelist.append(((tempi, temp_j),(i,j)))
tempi += 1
temp_j -= 1
if (tempi < 8 and temp_j >= 0 and get_col(board[tempi][temp_j]) == col): # kill
movelist.append(((tempi, temp_j),(i,j)))
return movelist
def get_code(piece):
return piece % 10
def get_col(piece):
if piece == -1:
return -1
if piece > 5:
return 1
else:
return 0
def is_protected(move):
ans = 0
if bot_is_white:
ppiece = board[move[0][0]][move[0][1]]
board[move[0][0]][move[0][1]]=board[move[1][0]][move[1][1]]
board[move[1][0]][move[1][1]]=-1
next_black_movelist = black_movelist()
final_black_movelist = []
for x in next_black_movelist:
if board[x[1][0]][x[1][1]]==5:
curi=x[1][0]
curj=x[1][1]
fini=x[0][0]
finj=x[0][1]
if curj!=finj:
final_black_movelist.append(x[0])
else:
final_black_movelist.append(x[0])
if (move[0][0],move[0][1]) in final_black_movelist:
ans=1
board[move[1][0]][move[1][1]]=board[move[0][0]][move[0][1]]
board[move[0][0]][move[0][1]]=ppiece
else:
ppiece = board[move[0][0]][move[0][1]]
board[move[0][0]][move[0][1]]=board[move[1][0]][move[1][1]]
board[move[1][0]][move[1][1]]=-1
next_black_movelist = white_movelist()
final_black_movelist = []
for x in next_black_movelist:
if board[x[1][0]][x[1][1]]==15:
curi=x[1][0]
curj=x[1][1]
fini=x[0][0]
finj=x[0][1]
if curj!=finj:
final_black_movelist.append(x[0])
else:
final_black_movelist.append(x[0])
if (move[0][0],move[0][1]) in final_black_movelist:
ans=1
board[move[1][0]][move[1][1]]=board[move[0][0]][move[0][1]]
board[move[0][0]][move[0][1]]=ppiece
return ans
def white_movelist(): # working
movelist = []
for i in range(0, 8):
for j in range(0, 8):
if board[i][j]>9:
movelist.extend(possible_moves(i,j,board[i][j]))
return movelist
def black_movelist():
movelist = []
for i in range(0, 8):
for j in range(0, 8):
if board[i][j]<9:
movelist.extend(possible_moves(i,j,board[i][j]))
return movelist
def make_black_move():
olist = white_movelist() # returns opponent move, fromTowhere
mlist = black_movelist()
olist2 = [] # opponents attacking movelist
for move in olist:
if board[move[1][0]][move[1][1]]==15:
curi=move[1][0]
curj=move[1][1]
fini=move[0][0]
finj=move[0][1]
if curj!=finj:
olist2.append(move[0])
else:
olist2.append(move[0])
for i in range(0,8):
for j in range(0,8):
if board[i][j]==15:
if j<1:
olist2.append((i-1,j+1))
elif j>6:
olist2.append((i-1,j-1))
else:
olist2.append((i-1,j+1))
olist2.append((i-1,j-1))
#print("black olist2",olist2)
#print("black mlist",mlist)
# basic move prio , ( when a piece simply moves )
prio = []
for i in range(0,len(mlist)):
prio.append(0)
moveno=0
for move in mlist: #( add priority to basic moves )
if board[move[1][0]][move[1][1]]==5: # mover is a pawn
prio[moveno]+=20+(10*(move[0][0]))
if move[1][1]==1:
prio[moveno]+=20
curi=move[1][0]
curj=move[1][1]
fini=move[0][0]
finj=move[0][1]
if fini==7: # if pawn is promoted
prio[moveno]+=9999
if curj!=finj: # its diagonal move
if board[move[0][0]][move[0][1]]==12: # enemey bishop dies
if is_protected(move):
prio[moveno]+=200
else:
prio[moveno]+=200
elif board[move[0][0]][move[0][1]]==14: # enemy rook dies
prio[moveno]+=9999
elif board[move[0][0]][move[0][1]]==15: # enemy pawn dies
if is_protected(move):
prio[moveno]+=150
else:
prio[moveno]+=250
# if pawn threats
if move[0][0]<7:
next_movelist = possible_moves(move[0][0],move[0][1],board[move[1][0]][move[1][1]])
for nextmove in next_movelist:
curi=nextmove[1][0]
curj=nextmove[1][1]
fini=nextmove[0][0]
finj=nextmove[0][1]
if curj!=finj:
if board[nextmove[0][0]][nextmove[0][1]]==14: # enemy rook dies
prio[moveno]+=100 # pawn threats rook
# if pawn threats end
# if pawns move is under threat
if move[0] in olist2:
prio[moveno]-=150
# if pawns move is under threat ends
# if current position of pawn is under threat
if move[1] in olist2:
if move[0] not in olist2:
prio[moveno]+=140
# if current position of pawn is under threat ends
elif board[move[1][0]][move[1][1]]==4: # mover is a rook
prio[moveno]+=10
if board[move[0][0]][move[0][1]]>9: # its enemy piece ,, our rook can kill
if board[move[0][0]][move[0][1]]==12: # enemey bishop dies
if is_protected(move):
prio[moveno]-=350
else:
prio[moveno]+=350
elif board[move[0][0]][move[0][1]]==14: # enemy rook dies
prio[moveno]+=999
elif board[move[0][0]][move[0][1]]==15: # enemy pawn dies
if is_protected(move):
prio[moveno]-=999
else:
prio[moveno]+=150
# if rook threats in his next move
next_movelist = possible_moves(move[0][0],move[0][1],board[move[1][0]][move[1][1]])
for nextmove in next_movelist:
if board[nextmove[0][0]][nextmove[0][1]]==12: # rook will threat enemy bishop
prio[moveno]+=200
elif board[nextmove[0][0]][nextmove[0][1]]==14: # our rook will threat enemy rook ( do not move this)
prio[moveno]-=999
elif board[nextmove[0][0]][nextmove[0][1]]==15: # rook will threat enemy pawn
prio[moveno]+=150
# if rook threats end
# if rook move is under threat
if move[0] in olist2:
prio[moveno]-=900
# if rook move is under threat ends
# if current position of rook is under threat
if move[1] in olist2:
if move[0] not in olist2:
prio[moveno]+=350
# if current position of rook is under threat ends
elif board[move[1][0]][move[1][1]]==2: # mover is a bishop
prio[moveno]+=10
if board[move[0][0]][move[0][1]]==12: # enemy bishop dies
prio[moveno]+=999
elif board[move[0][0]][move[0][1]]==14: # enemy rook dies
prio[moveno]+=99999
elif board[move[0][0]][move[0][1]]==15: # enemy pawn dies
prio[moveno]+=150
#if bisohp threats in his next move
next_movelist = possible_moves(move[0][0],move[0][1],board[move[1][0]][move[1][1]])
for nextmove in next_movelist:
if board[nextmove[0][0]][nextmove[0][1]]==2: # bishop will threat enemy bishop
prio[moveno]-=100
elif board[nextmove[0][0]][nextmove[0][1]]==4: # our bishop will threat enemy rook ( do not move this)
prio[moveno]+=200
elif board[nextmove[0][0]][nextmove[0][1]]==5: # bishop will threat enemy pawn
prio[moveno]+=50
# if bhisop threats in his next move ends
# if bisho move is under threat
if move[0] in olist2:
prio[moveno]-=900
# if bishop move is under threat ends
# if current position of bishop is under threat
if move[1] in olist2:
if move[0] not in olist2:
prio[moveno]+=150
# if current position of bishiop is under threat ends
moveno+=1
moveno = 0
maxmoveno = 0
maxprio = -99999
for x in prio:
if x>maxprio:
maxprio=x
maxmoveno=moveno
moveno+=1
move = mlist[maxmoveno]
curi=move[1][0]
curj=move[1][1]
fini=move[0][0]
finj=move[0][1]
ans=chr(curj + 97)+chr(8 - curi + 48)
ans+=chr(finj+97)+chr(8-fini+48)
print(ans)
def make_white_move():
olist = black_movelist() # returns opponent move, fromTowhere
mlist = white_movelist()
olist2 = [] # opponents attacking movelist
for move in olist:
if board[move[1][0]][move[1][1]]==5:
curi=move[1][0]
curj=move[1][1]
fini=move[0][0]
finj=move[0][1]
if curj!=finj:
olist2.append(move[0])
else:
olist2.append(move[0])
for i in range(0,8):
for j in range(0,8):
if board[i][j]==5:
if j<1:
olist2.append((i+1,j+1))
elif j>6:
olist2.append((i+1,j-1))
else:
olist2.append((i+1,j+1))
olist2.append((i+1,j-1))
#print("whites olist2",olist2)
#print("whites mlist",mlist)
# basic move prio , ( when a piece simply moves )
prio = []
for i in range(0,len(mlist)):
prio.append(0)
moveno=0
for move in mlist: #( add priority to basic moves )
if board[move[1][0]][move[1][1]]==15: # mover is a pawn
prio[moveno]+=20+(10*(8-move[0][0]))
if move[1][1]==6:
prio[moveno]+=20
curi=move[1][0]
curj=move[1][1]
fini=move[0][0]
finj=move[0][1]
if fini==0: # if pawn is promoted
prio[moveno]+=9999
if curj!=finj: # its diagonal move
if board[move[0][0]][move[0][1]]==2: # enemey bishop dies
prio[moveno]+=200
elif board[move[0][0]][move[0][1]]==4: # enemy rook dies
prio[moveno]+=999
elif board[move[0][0]][move[0][1]]==5: # enemy pawn dies
prio[moveno]+=150
# if pawn threats
if move[0][0]>0:
next_movelist = possible_moves(move[0][0],move[0][1],board[move[1][0]][move[1][1]])
for nextmove in next_movelist:
curi=nextmove[1][0]
curj=nextmove[1][1]
fini=nextmove[0][0]
finj=nextmove[0][1]
if curj!=finj:
if board[nextmove[0][0]][nextmove[0][1]]==4: # enemy rook dies
prio[moveno]+=100 # pawn threats rook
# if pawn threats end
# if pawns move is under threat
if move[0] in olist2:
prio[moveno]-=150
# if pawns move is under threat ends
# if current position of pawn is under threat
if move[1] in olist2:
if move[0] not in olist2:
prio[moveno]+=140
# if current position of pawn is under threat ends
elif board[move[1][0]][move[1][1]]==14: # mover is a rook
prio[moveno]+=10
if board[move[0][0]][move[0][1]]<9: # its enemy piece ,, our rook can kill
if board[move[0][0]][move[0][1]]==2: # enemey bishop dies
if is_protected(move):
prio[moveno]-=550
else:
prio[moveno]+=200
elif board[move[0][0]][move[0][1]]==4: # enemy rook dies
prio[moveno]+=999
elif board[move[0][0]][move[0][1]]==5: # enemy pawn dies
if is_protected(move):
prio[moveno]-=250
else:
prio[moveno]+=350
# if rook threats in his next move
next_movelist = possible_moves(move[0][0],move[0][1],board[move[1][0]][move[1][1]])
for nextmove in next_movelist:
if board[nextmove[0][0]][nextmove[0][1]]==2: # rook will threat enemy bishop
prio[moveno]+=100
elif board[nextmove[0][0]][nextmove[0][1]]==4: # our rook will threat enemy rook ( do not move this)
prio[moveno]-=9999
elif board[nextmove[0][0]][nextmove[0][1]]==5: # rook will threat enemy pawn
prio[moveno]+=150
# if rook threats end
# if rook move is under threat
if move[0] in olist2:
prio[moveno]-=900
# if rook move is under threat ends
# if current position of rook is under threat
if move[1] in olist2:
if move[0] not in olist2:
prio[moveno]+=350
# if current position of rook is under threat ends
elif board[move[1][0]][move[1][1]]==12: # mover is a bishop
prio[moveno]+=10
if board[move[0][0]][move[0][1]]==2:
prio[moveno]+=40
elif board[move[0][0]][move[0][1]]==4: # enemy rook dies
prio[moveno]+=999
elif board[move[0][0]][move[0][1]]==5: # enemy pawn dies
prio[moveno]+=60
#if bisohp threats in his next move
next_movelist = possible_moves(move[0][0],move[0][1],board[move[1][0]][move[1][1]])
for nextmove in next_movelist:
if board[nextmove[0][0]][nextmove[0][1]]==2: # bishop will threat enemy bishop
prio[moveno]-=100
elif board[nextmove[0][0]][nextmove[0][1]]==4: # our bishop will threat enemy rook ( do not move this)
prio[moveno]+=100
elif board[nextmove[0][0]][nextmove[0][1]]==5: # bishop will threat enemy pawn
prio[moveno]+=50
# if bhisop threats in his next move ends
# if bisho move is under threat
if move[0] in olist2:
prio[moveno]-=9999
# if bishop move is under threat ends
# if current position of bishop is under threat
if move[1] in olist2:
if move[0] not in olist2:
prio[moveno]+=150
# if current position of bishiop is under threat ends
moveno+=1
#print("whites prio",prio)
moveno = 0
maxmoveno = 0
maxprio = -99999
for x in prio:
if x>maxprio:
maxprio=x
maxmoveno=moveno
moveno+=1
move = mlist[maxmoveno]
curi=move[1][0]
curj=move[1][1]
fini=move[0][0]
finj=move[0][1]
ans=chr(curj + 97)+chr(8 - curi + 48)
ans+=chr(finj+97)+chr(8-fini+48)
print(ans)
if __name__=="__main__":
init_board()
read_board()
if bot_is_white:
make_white_move()
else:
make_black_move()