-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_engine.py
More file actions
813 lines (664 loc) · 28.6 KB
/
old_engine.py
File metadata and controls
813 lines (664 loc) · 28.6 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import random
import pprint
import time
import sys
from itertools import chain
import itertools
import pickle
#!/usr/bin/python
# -*- coding: utf-8 -*-
tic = time.clock()
CHECKLIST_ONE = [24, 35, 108, 119]
CHECKLIST_TWO = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
36, 48, 60, 72, 84, 96, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 95, 83, 71, 59,
47]
#The board representation
posiii = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #0-11
0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, #12-23
0, 0, 'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r', 0, 0, #24-35 #26-33
0, 0, 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 0, 0, #36-47 #38-45
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #48-59 #50-57
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #60-71 #62-69
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #72-83 #74-81
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #84-95 #86-93
0, 0, 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 0, 0, #96-107 #98-105
0, 0, 'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R', 0, 0, #108-119 #110-117
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #120-131
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 #132-143
]
#The board for testing moves and functions of different pieces.
#Modify at will
posi = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #0-11
0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, #12-23
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #24-35 #26-33
0, 0, '-', '-', '-', 'q', '-', '-', '-', 'k', 0, 0, #36-47 #38-45
0, 0, '-', '-', '-', '-', '-', 'N', '-', '-', 0, 0, #48-59 #50-57
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #60-71 #62-69
0, 0, '-', 'R', '-', '-', 'q', '-', 'R', '-', 0, 0, #72-83 #74-81
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #84-95 #86-93
0, 0, '-', '-', 'K', '-', '-', 'N', 'B', '-', 0, 0, #96-107 #98-105
0, 0, '-', '-', '-', '-', '-', '-', '-', '-', 0, 0, #108-119 #110-117
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #120-131
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 #132-143
]
#Used for printing the board
def board_view(list):
view = [list[i:i + 12] for i in xrange(0, 11, 12)]
o = '\n'.join(map(str, view))
view_2 = [list[i:i + 12] for i in xrange(12, 23, 12)]
o_2 = '\n'.join(map(str, view_2))
view_3 = [list[i:i + 12] for i in xrange(24, 117, 12)]
o_3 = '\n'.join(map(str, view_3))
view_4 = [list[i:i + 12] for i in xrange(120, 131, 12)]
o_4 = '\n'.join(map(str, view_4))
view_5 = [list[i:i + 12] for i in xrange(132, 143, 12)]
o_5 = '\n'.join(map(str, view_5))
good_board = '\t%s\n\t%s\n%s\n\t%s\n\t%s\n' % (o, o_2, o_3, o_4, o_5)
return good_board
#Illegal positions for pieces
ZEROS = [i for i, x in enumerate(posi) if x == 0]
X, Y = 1, 12
moves = {
"rook" : [X, Y, -X, -Y],
"knight" : [Y+Y-X, Y+Y+X, X+X+Y, X+X-Y, -Y-Y+X, -Y-Y-X, -X-X-Y, -X-X+Y],
"bishop" : [X+Y, X-Y, -X+Y, -X-Y],
"queen" : [X, Y, -X, -Y, X+Y, X-Y, -X+Y, -X-Y],
"king" : [X, Y, -X, -Y, X+Y, X-Y, -X+Y, -X-Y],
"pawn" : [-Y, -Y+X, -Y-X]
}
#The function for rotating the board; used when a move is made
#and it is other player's move.
def rotate(BOARD):
for SQUARE in range(0, 8):
BOARD[26+SQUARE], BOARD[117-SQUARE] \
= BOARD[117-SQUARE], BOARD[26+SQUARE]
for SQUARE in range(0, 8):
BOARD[38+SQUARE], BOARD[105-SQUARE] \
= BOARD[105-SQUARE], BOARD[38+SQUARE]
for SQUARE in range(0, 8):
BOARD[50+SQUARE], BOARD[93-SQUARE] \
= BOARD[93-SQUARE], BOARD[50+SQUARE]
for SQUARE in range(0, 8):
BOARD[62+SQUARE], BOARD[81-SQUARE] \
= BOARD[81-SQUARE], BOARD[62+SQUARE]
return BOARD
white_pieces = {
"king": 'K', "rook": 'R', "pawn": 'P',
"knight": 'N', "queen": 'Q', "bishop": 'B'
}
def lower_case(d):
new = dict((k.lower(), v.lower()) for k,v in d.iteritems())
return new
def upper_case(d):
new = dict((k, v.upper()) for k,v in d.iteritems())
return new
def upper_keys(d):
new = dict((k.upper(), v) for k,v in d.iteritems())
return new
#Maybe will be used later
def low(d):
new = dict((k.lower(), v) for k,v in d.iteritems())
return new
#Two lists for positions and lists of moves for White and Black
newlist = {'K': [], 'R': [], 'P': [], 'N': [], 'Q': [], 'B': []} #White
position = {'K': [], 'R': [], 'P': [], 'N': [], 'Q': [], 'B': []} #White
newlist_B = {'k': [], 'r': [], 'p': [], 'n': [], 'q': [], 'b': []} #Black
position_B = {'k': [], 'r': [], 'p': [], 'n': [], 'q': [], 'b': []} #Black
RANGE_SQUARE_NUMBERS = []
RANGE_SQUARES = []
enemy = []
enemy_w = []
ene = []
PIECE_POSITIONS = []
class Dictionaries(object):
pass
# These will be implemented to prevent castling
#kingmove = False
#rookmove = False
CHECKING_SQUARE_LIST = []
class Whitem(Dictionaries):
def __init__(self, kingmove, rookmove, white_to_move, newlist, newlist_B):
self.kingmove = kingmove
self.rookmove = rookmove
self.white_to_move = white_to_move
self.newlist = newlist
self.newlist_B = newlist
def move_gen(self, posi):
# Links necessary lists to enumerate moves and positions.
global white_pieces
del RANGE_SQUARE_NUMBERS[:]
del RANGE_SQUARES[:]
del ene[:]
del enemy[:]
del enemy_w[:]
del PIECE_POSITIONS[:]
#kings.pieces_attacking = []
#kings.seek_threats(posi)
kings.check = False
SIZE = 0
for i in self.newlist: #empties the list
self.newlist[i] = []
for i in self.newlist_B: #empties the list
self.newlist_B[i] = []
if self.white_to_move == False:
kings.CURRENT_PIECE = [i for i, x in enumerate(posi) if x == 'k'][0]
self.newlist_B = upper_keys(self.newlist_B)
self.newlist = low(self.newlist)
#rotate(self.posi)
white_pieces = lower_case(white_pieces)
else:
kings.CURRENT_PIECE = [i for i, x in enumerate(posi) if x == 'K'][0]
self.newlist_B = low(self.newlist_B)
self.newlist = upper_keys(self.newlist)
white_pieces = upper_case(white_pieces)
print "PIECE IS HERE", kings.CURRENT_PIECE
kings.seek_threats(posi, kings.CURRENT_PIECE)
print "WOO"
#kings.pieces_attacking = []
print kings.piece
#print "KINGSSS", kings.kingSafety()
LOOK_ALL = 1
while len(kings.kingSafety()[0]) + LOOK_ALL >= 2:
kings.kingSafety()
LOOK_ALL -= 1
#this_piece_to_capture =
#capture_that_piece = kings.kingSafety()[0]
#print capture_that_piece
#if capture_that_piece != []:
# pick_a_piece = random.choice(capture_that_piece)
# print pick_a_piece
# posi[pick_a_piece], posi[attack_this] = \
# posi[attack_this], posi[pick_a_piece]
# posi[attack_this] = '-'
# return posi
#Whitem.move_gen(self, posi)
print "CHECK TRUE?", kings.check
print "WHO ATTACKS?", kings.pieces_attacking
kings.pieces_attacking = []
#Searches all white's pieces' positions
for j in self.newlist.keys():
T = [i for i, x in enumerate(posi) if x == j]
PIECE_POSITIONS.append(T)
PIECE_UNNESTED = list(itertools.chain.from_iterable(PIECE_POSITIONS))
RANDOM_SQUARE = random.choice(PIECE_UNNESTED)
PIECE = posi[RANDOM_SQUARE]
print "RANDOM_SQ", RANDOM_SQUARE
#print "SAFE?", kings.kingSafety()
if kings.seek_threats(posi, kings.CURRENT_PIECE) != []:
print kings.pieces_attacking
attack_this = random.choice(kings.pieces_attacking)
kings.pieces_attacking = []
print kings.pieces_attacking
print attack_this
kings.seek_threats(posi, attack_this)
print "ATT", kings.pieces_attacking
kings.check = True
if kings.pieces_attacking != []:
while kings.check:
attack_that = random.choice(kings.pieces_attacking)
previous_piece = attack_that
posi[attack_this], posi[attack_that] = \
posi[attack_that], posi[attack_this]
posi[attack_that] = "-"
kings.pieces_attacking = []
print "JOU"
if kings.seek_threats(posi, kings.CURRENT_PIECE) != []:
print "HERE"
attack_that = previous_piece
posi[attack_that], posi[attack_this] = \
posi[attack_this], posi[attack_that]
continue
else:
return posi
if self.white_to_move == True:
print "TRUE"
PIECE = 'K'
RANDOM_SQUARE = [i for i, y in enumerate(posi) if y == 'K']
SIZE += 1
RANDOM_SQUARE = RANDOM_SQUARE[0]
elif self.white_to_move == False:
print "TRUE???"
PIECE = 'k'
RANDOM_SQUARE = [i for i, y in enumerate(posi) if y == 'k']
SIZE += 1
RANDOM_SQUARE = RANDOM_SQUARE[0]
print "what is random now huhuhuh?", RANDOM_SQUARE
#else:
# RANDOM_SQUARE = kings.CURRENT_PIECE
# PIECE = posi[RANDOM_SQUARE]
print "what is random now?", RANDOM_SQUARE
#kings.check = False
print "PIECE woot", PIECE
#print "posiiii", RANDOM_SQUARE
PIECE_STRING = white_pieces.keys()[white_pieces.values().index(PIECE)]
F = [x.lower() for x in list(self.newlist)]
for MOVE in moves[PIECE_STRING]:
H = MOVE + RANDOM_SQUARE
P = posi[MOVE + RANDOM_SQUARE]
self.newlist[PIECE].append(H)
ene.append(P)
for ENEMY_PIECE in self.newlist_B:
# Checks are there enemy pieces in ene list
SQUARES = [i for i, y in enumerate(ene[1:3]) if y == ENEMY_PIECE]
for SQUARE in SQUARES:
# enemy_w is for king, pawn and knight. They are not
# ranged pieces
enemy.append(SQUARE)
#newlist is a list of possible moves of randomly selected piece
RND_MOVE = random.choice(self.newlist[PIECE])
print "RND", RND_MOVE
# RND_MOVE is a random move of PIECE
# Pawns' initial squares
INITIAL_PAWN_SQUARE = any(x in [RANDOM_SQUARE] \
for x in list(range(98, 105)))
print "WHAT RAND", RANDOM_SQUARE
print "IS THIS KING", PIECE
# posi[RND_MOVE] == '-' is a condition for square to be empty
if PIECE in ['B', 'R', 'Q', 'b', 'r', 'q'] \
and posi[RND_MOVE] == '-':
n = 8
for RANGE in range(1, 8):
while (any(x in list(self.newlist) \
for x in RANGE_SQUARES) == False):
try:
M = RANDOM_SQUARE - (RANDOM_SQUARE-RND_MOVE)*RANGE
B = posi[RANDOM_SQUARE - \
(RANDOM_SQUARE-RND_MOVE)*RANGE]
# Not moving to a square which is zero
# No jumping over Black pieces
# No jumping over White pieces
# Stay inside board, no 'floor' to 'roof' action
if any(x in ZEROS for x in RANGE_SQUARE_NUMBERS) \
== False \
and any(x in list(self.newlist_B) for x in \
RANGE_SQUARES) == False \
and any(x in list(self.newlist) for x in \
RANGE_SQUARES) == False \
and ((RANDOM_SQUARE \
- (RANDOM_SQUARE-RND_MOVE)*RANGE) - (RANDOM_SQUARE \
- (RANDOM_SQUARE-RND_MOVE)*(RANGE-1))) < 14:
RANGE_SQUARE_NUMBERS.append(M)
RANGE_SQUARES.append(B)
break
except IndexError:
break
if PIECE == 'R' or 'r':
self.rookmove = True
else:
pass
for ENEMY_PIECE in self.newlist_B:
SQUARES = [i for i, y in enumerate(RANGE_SQUARES) \
if y == ENEMY_PIECE]
for SQUARE in SQUARES:
enemy.append(SQUARE)
if enemy != []: #list of opponents pieces for capture
ENEMY_INDEX = min(s for s in enemy)
CAPTURE = RANGE_SQUARE_NUMBERS[ENEMY_INDEX]
if posi[CAPTURE] not in self.newlist.keys() + ['k', 'K', 0]:
# Below piece of code means that a move is made
posi[RANDOM_SQUARE], posi[CAPTURE] \
= posi[CAPTURE], posi[RANDOM_SQUARE]
posi[RANDOM_SQUARE] = '-'
return posi
else:
pass
else:
pass
INDICES = [i for i, y in enumerate(RANGE_SQUARES) if y == '-']
FREE_SQUARE_INDEX = random.choice(INDICES)
FREE_SQUARE = RANGE_SQUARE_NUMBERS[FREE_SQUARE_INDEX]
if posi[FREE_SQUARE] != 0:
posi[RANDOM_SQUARE], posi[FREE_SQUARE] = \
posi[FREE_SQUARE], posi[RANDOM_SQUARE]
return posi
else:
Whitem.move_gen(self, posi)
else:
if (PIECE == 'P' or PIECE == 'p') and posi[RANDOM_SQUARE - \
(RANDOM_SQUARE-self.newlist[PIECE][0])] == '-':
if enemy != []:
ENEMY_INDEX = random.choice(enemy)
CAPTURE = self.newlist[PIECE][ENEMY_INDEX + 1]
if posi[CAPTURE] not in self.newlist.keys() \
+ ['k', 'K']:
posi[RANDOM_SQUARE], posi[CAPTURE] = \
posi[CAPTURE], posi[RANDOM_SQUARE]
posi[RANDOM_SQUARE] = '-'
return posi
else:
pass
elif INITIAL_PAWN_SQUARE == True and (PIECE == 'P' or PIECE \
== 'p') and posi[self.newlist[PIECE][0] - 12] == '-' and \
posi[self.newlist[PIECE][0] - 24] == '-':
# Pawn moves randomly one or two squares depending on
# z value.
z = random.choice(range(1, 3))
posi[RANDOM_SQUARE], posi[RANDOM_SQUARE - \
(RANDOM_SQUARE-self.newlist[PIECE][0])*z] = \
posi[RANDOM_SQUARE - (RANDOM_SQUARE \
- self.newlist[PIECE][0])*z], posi[RANDOM_SQUARE]
return posi
else:
posi[RANDOM_SQUARE], \
posi[self.newlist[PIECE][0]] = \
posi[self.newlist[PIECE][0]], posi[RANDOM_SQUARE]
return posi
if RANDOM_SQUARE in CHECKLIST_ONE:
SIZE = 3
elif RANDOM_SQUARE in CHECKLIST_TWO:
SIZE = 5
else:
SIZE = 8
if len(CHECKING_SQUARE_LIST) == SIZE:
print "Checkmate!"
exit(1)
# Normal king move or captures enemy piece
elif (PIECE == 'K' or PIECE == 'k') and (posi[RANDOM_SQUARE \
- (RANDOM_SQUARE-RND_MOVE)*2] not in ['K', 'k']):
noppa = random.choice(range(0, 2))
if enemy != []:
print "eat enemy!!!!!"
print "SEEK", kings.seek_threats(posi, RANDOM_SQUARE)
ENEMY_INDEX = random.choice(enemy)
CAPTURE = self.newlist[PIECE][ENEMY_INDEX + 1]
# No capturing enemy King or own pieces
if posi[CAPTURE] not in self.newlist.keys() \
+ ['k', 'K']:
X_ = posi[CAPTURE]
posi[RANDOM_SQUARE], posi[CAPTURE] \
= posi[CAPTURE], posi[RANDOM_SQUARE]
posi[RANDOM_SQUARE] = '-'
print kings.check
if kings.check == True:
if CAPTURE not in CHECKING_SQUARE_LIST:
CHECKING_SQUARE_LIST.append(CAPTURE)
posi[CAPTURE] = X_
posi[RANDOM_SQUARE] = PIECE
print "back to normal"
Whitem.move_gen(self, posi)
del CHECKING_SQUARE_LIST[:]
return posi
else:
pass
# Check if castling is a legal move
elif noppa == 0 and self.kingmove == False and \
self.rookmove == False and (posi[117] == 'R' or 'r') and \
posi[115] == posi[116] == '-' \
and posi[114] in ['K', 'k']:
posi[RANDOM_SQUARE], posi[115], posi[116], \
posi[117] = posi[115], posi[117], \
posi[RANDOM_SQUARE], posi[116]
# Check if castling is a legal move
elif noppa == 1 and self.kingmove == False and \
self.rookmove == False and \
posi[110] in ['R', 'r'] \
and all(x=='-' for x in posi[111:113]) \
and posi[114] in ['K', 'k']:
posi[110], posi[111], posi[112], \
posi[113], posi[RANDOM_SQUARE] \
= posi[113], posi[112], \
posi[RANDOM_SQUARE], posi[110], posi[111]
self.kingmove = True
self.rookmove = True
else:
if posi[RANDOM_SQUARE - (RANDOM_SQUARE-RND_MOVE)] == '-':
self.kingmove = True
posi[RANDOM_SQUARE], posi[RND_MOVE] \
= posi[RND_MOVE], posi[RANDOM_SQUARE]
for i in moves["king"]:
if posi[RND_MOVE + i] in ['K', 'k']:
print "kunkku close"
posi[RND_MOVE], posi[RANDOM_SQUARE] \
= posi[RANDOM_SQUARE], posi[RND_MOVE]
Whitem.move_gen(self, posi)
kings.seek_threats(posi, RANDOM_SQUARE)
print "howmany??", kings.pieces_attacking
print kings.check
if kings.seek_threats(posi, RANDOM_SQUARE) != []:
if RND_MOVE not in CHECKING_SQUARE_LIST:
CHECKING_SQUARE_LIST.append(RND_MOVE)
print "back to normaaal"
posi[RND_MOVE], posi[RANDOM_SQUARE] \
= posi[RANDOM_SQUARE], posi[RND_MOVE]
Whitem.move_gen(self, posi)
del CHECKING_SQUARE_LIST[:]
return posi
else:
Whitem.move_gen(self, posi)
# Knight captures a piece which is not the king or moves
# to a different square.
elif (PIECE == 'N' or PIECE == 'n') and posi[RANDOM_SQUARE \
- (RANDOM_SQUARE-RND_MOVE)] == '-':
if enemy != []:
ENEMY_INDEX = random.choice(enemy)
CAPTURE = self.newlist[PIECE][ENEMY_INDEX + 1]
if posi[CAPTURE] not in (self.newlist.keys() \
+ ['k', 'K']):
posi[RANDOM_SQUARE], posi[CAPTURE] \
= posi[CAPTURE], posi[RANDOM_SQUARE]
posi[RANDOM_SQUARE] = '-'
return posi
else:
pass
else:
posi[RANDOM_SQUARE], posi[RANDOM_SQUARE - \
(RANDOM_SQUARE-RND_MOVE)] = posi[RANDOM_SQUARE - \
(RANDOM_SQUARE-RND_MOVE)], posi[RANDOM_SQUARE]
return posi
elif posi[RANDOM_SQUARE - (RANDOM_SQUARE-RND_MOVE)] == 0:
Whitem.move_gen(self, posi)
else:
Whitem.move_gen(self, posi)
def save_list(target_list):
with open('target_list', 'wb') as f:
pickle.dump(target_list, f)
def open_list(target_list):
with open('target_list', 'rb') as f:
target_list = pickle.load(f)
class Evaluate(object):
def __init__(self, position):
self.position = position
def evaluation_function(self):
piece_values = {
'B': 310, 'N': 300, 'P': 100, 'R': 500, 'Q': 900, 'K': 10000
}
sum = 0
sum_1 = 0
value_list = []
for i in piece_values.keys():
value_list.append(self.position.count(i)*piece_values[i])
for i in value_list:
sum += i
del value_list [:]
piece_values = low(piece_values)
for i in piece_values.keys():
value_list.append(self.position.count(i)*piece_values[i])
for i in value_list:
sum_1 += i
black_pieces = False
return sum - sum_1
class board_object(object):
def __init__(self, posi):
self.posi = posi
self.posi = list(self.posi)
def chess_engine(): #This will be used for the evaluation function
BOARD_LIST = []
EVALUATIONS = []
depth = 0
#first_save = True
#eval_1 = Evaluate(posi, False, 0, 0)
BOARD_LIST.append(board_object(posi))
EVALUATIONS.append(Evaluate(posi).evaluation_function())
LIMIT = 50
while depth < LIMIT:
a_game.move_gen(posi)
#print board_view(posi)
#if first_save:
# this_board = board_object(posi)
#BOARD_LIST[1] = board_object(posi)
BOARD_LIST.append(board_object(posi))
# aaa = this_board.posi
#with open('posi', 'wb') as f:
# pickle.dump(posi, f)
#eval_1.evaluation_function()
EVALUATIONS.append(Evaluate(posi).evaluation_function())
# first_save = False
#else:
# pass
a_game.white_to_move = False
rotate(posi)
a_game.move_gen(posi)
a_game.white_to_move = True
rotate(posi)
BOARD_LIST.append(board_object(posi))
EVALUATIONS.append(Evaluate(posi).evaluation_function())
depth += 1
#eval_2 = Evaluate(posi, False, 0, 0)
#eval_2.evaluation_function()
#print board_view(posi)
#BOARD_LIST.append(board_object(posi))
#print board_view(this_board.posi)
#rotate(this_board.posi)
#a_game.white_to_move = False
#a_game.move_gen(this_board.posi)
#rotate(this_board.posi)
#second_board = board_object(this_board.posi)
#print board_view(this_board.posi)
#for i in range(0, LIMIT):
for i in range(0, LIMIT):
print EVALUATIONS[i]
print board_view(BOARD_LIST[LIMIT].posi)
#Boards could be keys and evaluations values in dictionary
# This works!!
#if this_board.posi == BOARD_LIST[1].posi:
# print "hurraa!"
#print eval_1.sum
#print eval_1.sum_1
#print eval_2.sum
#print eval_2.sum_1
#check_these_pieces = []
class Threat(object):
def __init__(self, check, piece, CURRENT_PIECE):
self.check = check
self.piece = piece
self.CURRENT_PIECE = CURRENT_PIECE
self.pieces_attacking = []
def seek_threats(self, posit, initial_piece_square):
#posi = test
#See if the king is in check
initial_piece = posit[initial_piece_square]
#print "INITIAL", initial_piece
own_pieces = white_pieces.values()
opponent_pieces = white_pieces
if initial_piece.isupper():
opponent_pieces = lower_case(white_pieces)
#opponent_pieces = map(lambda x:x.lower(),opponent_pieces)
else:
own_pieces = map(lambda x:x.lower(),own_pieces)
#print opponent_pieces
#initial_piece_square = [i for i, x in enumerate(posit) if x == initial_piece]
#initial_piece_square = random.choice(initial_piece_square)
#or x == 'k']
#print "initial_piece_square:", initial_piece_square
#print opponent_pieces.values()
for index, name in enumerate(white_pieces.keys()):
#print(index, name)
#print moves[name]
if index == 2:
moves[name] = moves[name][1:]
for i in moves[name]:
if index in [1, 4, 5]:
j = 1
while posit[initial_piece_square + i * j] == '-' or \
posit[initial_piece_square + i * j] == opponent_pieces[name]:
if posit[initial_piece_square + i * j] == opponent_pieces[name]:
#print "PIECE", name
self.pieces_attacking.append(initial_piece_square + i * j)
j += 1
if index == 2:
if posit[initial_piece_square + i] == opponent_pieces[name]:
#print "PIECE", name
self.pieces_attacking.append(initial_piece_square + i)
if index in [0, 3]:
if posit[initial_piece_square + i] == opponent_pieces[name]:
#print "PIECE", name
self.pieces_attacking.append(initial_piece_square + i)
# if posit[initial_piece_square + i] == opponent_pieces[name]
#print self.pieces_attacking
#print "SELF", self.CURRENT_PIECE
return self.pieces_attacking
def kingSafety(self):
if posi[self.CURRENT_PIECE] in ['K', 'k'] and self.pieces_attacking != []:
self.check = True
self.piece = random.choice(self.pieces_attacking)
self.pieces_attacking = []
#print "HESSU"
self.seek_threats(posi, self.piece)
return (self.pieces_attacking, self.piece)
#return
else:
self.check = False
return (self.pieces_attacking, self.CURRENT_PIECE)
def capture_piece():
if kings.check:
PIECE = kings.piece
#print board_view(test)
game = Dictionaries()
a_game = Whitem(False, False, True, newlist, newlist_B)
#a_game.posi = this_board
#b_game = Blackm()
kings = Threat(False, None, None)
#kings.CURRENT_PIECE = 32
#kings.seek_threats(test, 32)
#a = kings.kingSafety()
#print "aaaa", a[0]
#print kings.check
#print kings.check
#if kings.pieces_attacking == []:
# print "empty"
#print board_view(test)
#print kings.pieces_attacking
def moving_randomly():
turn()
#turn_test()
#chess_engine()
#with open('posi', 'rb') as f:
# posi = pickle.load(f)
#print board_view(posi)
def turn():
while True:
print board_view(posi)
print "Press enter to play"
m = raw_input()
if m == "":
a_game.move_gen(posi)
print board_view(posi)
print "Press enter to play"
rotate(posi)
a_game.white_to_move = False
nassu = raw_input()
if nassu == "":
print a_game.white_to_move
a_game.move_gen(posi)
a_game.white_to_move = True
rotate(posi)
print board_view(posi)
else:
continue
else:
continue
def turn_test():
while True:
a_game.move_gen()
print board_view(posi)
time.sleep(0.7)
rotate(posi)
b_game.move_gen()
rotate(posi)
print board_view(posi)
moving_randomly()
toc = time.clock()
print "time spent:", toc - tic