-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.py
181 lines (152 loc) · 4.8 KB
/
algorithm.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
############################################################
###algorithm.py Chess Algorithm Engine ###
###Written by Nicholas Maselli ###
### ###
###Purpose: The Algorithm class creates a minimax tree ###
###that utilizies alpha beta pruning and iterative ###
###deepening to search through the tree quickly. Piece ###
###square tables are used to obtain good value functions ###
###for chess board evaluation. ###
### ###
###Version: 1.0 ###
###Date: 6-30-17 ###
############################################################
from chess import Chess
import random
import collections
import time
###############################
#####MinimaxGameTree Class#####
###############################
class MinimaxGameTree():
def __init__(self, chess, color, depth):
self.chess = chess
self.player = color
self.depth = depth
#Time
self.fulltime = 0
#Continuously Iterate search depth to obtain better move ordering
def iterativeDeepening(self):
alpha = -40000
beta = 40000
pv = []
for depth in range(1, self.depth+1):
data = self.dfsMax(alpha, beta, depth, pv)
pv = data[1]
best_value = data[0]
move_list = data[1]
best_move = move_list[self.depth-1]
return(best_move)
#Minimax algorithm with alpha-beta pruning, max function
def dfsMax(self, alpha, beta, depth, pv):
if (depth == 0):
value = self.evaluate_board(self.player)
return((value, []))
#Start with the principal value move
move_list = []
best_move = None
if (pv != []):
move = pv.pop()
self.next_position(move)
data = self.dfsMin(alpha, beta, depth-1, pv)
self.previous_position()
value = data[0]
if (value >= beta):
move_list = data[1]
move_list.append(best_move)
return((beta, move_list))
if (value > alpha):
alpha = value
best_move = move
move_list = data[1]
for move in self.chess.legal_moves():
self.next_position(move)
data = self.dfsMin(alpha, beta, depth-1, pv)
self.previous_position()
value = data[0]
if (value >= beta):
move_list = data[1]
move_list.append(best_move)
return((beta, move_list))
if (value > alpha):
alpha = value
best_move = move
move_list = data[1]
#If you are in checkmate
if (best_move == None):
alpha = -20000
move_list.append(best_move)
return((alpha, move_list))
#Minimax algorithm with alpha-beta pruning, min function
def dfsMin(self, alpha, beta, depth, pv):
if (depth == 0):
value = self.evaluate_board(self.player)
return((value, []))
#Start with the principal value move
move_list = []
best_move = None
if (pv != []):
move = pv.pop()
self.next_position(move)
data = self.dfsMax(alpha, beta, depth-1, pv)
self.previous_position()
value = data[0]
if (value <= alpha):
move_list = data[1]
move_list.append(best_move)
return((alpha, move_list))
if (value < beta):
beta = value
best_move = move
move_list = data[1]
for move in self.chess.legal_moves():
self.next_position(move)
data = self.dfsMax(alpha, beta, depth-1, pv)
self.previous_position()
value = data[0]
if (value <= alpha):
move_list = data[1]
move_list.append(best_move)
return((alpha, move_list))
if (value < beta):
beta = value
best_move = move
move_list = data[1]
#If opponent is in checkmate
if (best_move == None):
beta = 20000
move_list.append(best_move)
return((beta, move_list))
#Evaluate the current board and state from color's perspective
def evaluate_board(self, color):
if (color == 'white'):
value = self.chess.state.value
if (color == 'black'):
value = -self.chess.state.value
return(value)
#Move to the next position in the chess board
def next_position(self, move):
self.chess.move_piece(move)
#Move to previous position in the chessboard
def previous_position(self):
self.chess.undo_move()
#########################
#####Algorithm Class#####
#########################
class Algorithm():
#Initialize values
def __init__(self, chess, player, depth):
self.chess = chess
self.player = player
self.depth = depth
self.fulltime = 0
#Choose next move using algorithm
def best_move(self):
self.tree = MinimaxGameTree(self.chess, self.player, self.depth)
#Comments here for timing purposes
#start_time = time.time()
move = self.tree.iterativeDeepening()
#end_time = time.time()
#print("Searching the tree: {}".format(end_time - start_time))
notation = self.chess.coordinate_to_notation(move)
return(notation)