Repository navigation
Expand file tree
/
Copy paththreads.py
More file actions
47 lines (40 loc) · 1.75 KB
/
Copy paththreads.py
File metadata and controls
47 lines (40 loc) · 1.75 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
from ai.move import Move
import threading
import copy
class GetMovesThread (threading.Thread):
def run (self):
self.moves = []
for move in self.piece.get_moves(self.chess,self.chess.get_king(self.piece.get_color())):
self.moves.append(Move(self.piece.get_row(),self.piece.get_col(),move[0],move[1],self.chess.move_eval((self.piece.get_row(),self.piece.get_col()),(move[0],move[1]))))
def __init__ (self,name,piece,chess):
threading.Thread.__init__(self)
self.name = name
self.piece = piece
self.chess = chess
class MinimaxThread (threading.Thread):
def run (self):
self.result = self.minimax(self.depth,self.chess,self.move,self.color)
def minimax (self,depth,chess,move,color):
temp_chess = copy.deepcopy(chess)
temp_chess.move(move.move_from[0],move.move_from[1],move.move_to[0],move.move_to[1])
if (depth == 0): return move.val
else:
if (color == 'b'):
moves = temp_chess.get_possible_moves('b')
if (len(moves) == 0):
return move.val
moves_values = map(self.minimax,[depth-1]*len(moves),[temp_chess]*len(moves),moves,['w']*len(moves))
return max(moves_values)
else:
moves = temp_chess.get_possible_moves('w')
if (len(moves) == 0):
return move.val
moves_values = map(self.minimax,[depth-1]*len(moves),[temp_chess]*len(moves),moves,['w']*len(moves))
return min(moves_values)
def __init__ (self,name,chess,move,color,depth):
threading.Thread.__init__(self)
self.name = name
self.chess = chess
self.move = move
self.color = color
self.depth = depth