-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyss_move.py
More file actions
70 lines (54 loc) · 2.13 KB
/
pyss_move.py
File metadata and controls
70 lines (54 loc) · 2.13 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
"""choose a move to play"""
import time
# multiprocess is faster than multiprocessing (idk why, im probably doing something wrong)
import multiprocess as mp # pylint: disable=import-error
from pyss_minimax import minimax
def process_function(board, move, depth, queue):
"""function to be run in multiple processes simultaneously"""
board.push(move)
score = minimax(board, depth - 1, float("-inf"), float("inf"))
queue.put((move, score))
def get_move(board, depth, max_time):
"""get the highest scoring move by running an evaluation on all legal moves"""
# start timer
start_time = time.time()
# set default worst moves
best_move = None
best_score = float("-inf")
# create an array and queue for processes
processes = []
queue = mp.Queue() # pylint: disable=no-member
# iterate through all legal moves
for move in board.legal_moves:
# copy the board
board_copy = board.copy()
# create a process for each move
process = mp.Process(target=process_function, args=(board_copy, move, depth, queue)) # pylint: disable=not-callable
processes.append(process)
# start all processes
for process in processes:
process.start()
# wait for all processes to finish
for process in processes:
# if the time limit has been reached, kill all processes
if time.time() - start_time > max_time:
for process in processes:
process.terminate()
print("Max Time Reached")
break
process.join()
# get the best move from the queue
# the queue empty out as we .get move/score pairs
while not queue.empty():
move, score = queue.get()
# ensure that we get the best move as black as well
if not board.turn:
score *= -1
if score > best_score:
best_move = move
best_score = score
# if the score is equal, I prefer to always play the same move deterministically
# this is helpful for debugging
elif score == best_score and str(move) < str(best_move):
best_move = move
return best_move