-
Notifications
You must be signed in to change notification settings - Fork 1
/
atomic_engine.py
90 lines (70 loc) · 1.8 KB
/
atomic_engine.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
import chess
import time
from evaluation.evaluation import *
from opening_book import Book
import pickle
import random
import chess.variant
inf = float('inf')
poscount = 0
DEPTH = 2
def search(node, color, variant, depth):
moves = list(node.legal_moves)
if not moves:
print('Game over.')
return
move = negamax(node, -inf, inf, color, variant, depth)[1]
if not move:
return random.choice(moves)
else:
return move
def negamax(node, a, b, color, variant, depth=DEPTH):
global poscount
if (depth == 0):
return (evaluate(node, color, variant) * color, None)
moves = list(node.legal_moves)
best_move = None
best_value = -inf
for move in moves:
poscount+=1
node.push(move)
result = negamax(node, -b, -a, -color, variant, depth-1)
value = -result[0]
node.pop()
if value > best_value:
best_value = value
best_move = move
a = max(a, value)
if a >= b:
break
return (best_value, best_move)
if __name__ == "__main__":
board = chess.variant.AtomicBoard()
moves = []
book = None
#pickle.dump(book, open("penguinbook.p", "wb"))
c = 0
while not board.is_variant_end():
if c%2==0:
move = input("move: \n\n")
move = chess.Move.from_uci(move)
if not move in board.legal_moves:
continue
else:
start_time = time.time()
#book_move = book.get_moves(moves)
book_move = None
if book_move:
move = random.choice(book_move)
move = chess.Move.from_uci(move)
else:
move = search(board, color=-1, variant="atomic", depth=DEPTH)
elapsed = time.time() - start_time
print("--- %s moves ---" % (len(list(board.legal_moves))))
print("--- number of nodes: %s --" % poscount)
print("--- %s seconds ---" % (elapsed))
print("--- nodes per second: %s ---" % str(poscount / elapsed))
print(move)
moves.append(str(move))
board.push(move)
c+=1