forked from hazelduvall/othello_tourney
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ai.py
More file actions
52 lines (43 loc) · 1.54 KB
/
run_ai.py
File metadata and controls
52 lines (43 loc) · 1.54 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
import os
import sys
if sys.platform == 'win32' or sys.platform == 'cygwin':
import ctypes
import importlib
from multiprocessing import Process, Value
import time
import socket
from othello_admin import *
sys.path = [sys.path[0]]+sys.path
class LocalAI:
def __init__(self, name, possible_names, timelimit):
self.name = name
self.timelimit = timelimit
if name in possible_names:
old_path = os.getcwd()
old_sys = sys.path[1:]
path = old_path + '/private/Students/'+name
os.chdir(path)
sys.path = [os.getcwd()] + old_sys
self.strat = importlib.import_module('private.Students.'+name+'.strategy').Strategy().best_strategy
os.chdir(old_path)
#sys.path = old_sys
def get_move(self, board, player):
best_shared = Value("i", -1)
best_shared.value = 11
running = Value("i", 1)
p = Process(target=self.strat, args=(list(board), player, best_shared, running))
p.start()
t1 = time.time()
p.join(self.timelimit)
running.value = 0
time.sleep(0.01)
p.terminate()
time.sleep(0.01)
if sys.platform == 'win32' or sys.platform == 'cygwin':
handle = ctypes.windll.kernel32.OpenProcess(1, False, p.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
else:
if p.is_alive(): os.kill(p.pid, 9)
move = best_shared.value
return move