forked from nneonneo/threes-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirwatch_assistant.py
54 lines (44 loc) · 1.48 KB
/
dirwatch_assistant.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
''' Help the user achieve a high score in a real game of threes by using a move searcher.
This assistant watches a directory of screenshot files, and makes a move when it detects a new screenshot. '''
import time
import os
from ocr import OCR
from base_assistant import run_assistant
def watchdir(d, sleeptime=0.1):
base = set(os.listdir(d))
while 1:
time.sleep(sleeptime)
new = set(os.listdir(d))
for v in sorted(new - base):
if not v.startswith('.'):
yield os.path.join(d, v)
base = new
def gen_board(ocr, d, startpoint=None):
if startpoint is not None:
for fn in os.listdir(d):
if fn >= startpoint and not fn.startswith('.'):
print fn
fn = os.path.join(d, fn)
board, tileset = ocr.ocr(fn)
yield board, tileset, True
for fn in watchdir(d):
board, tileset = ocr.ocr(fn)
print os.path.basename(fn)
yield board, tileset, False
def make_move(move):
print "Recommended move:", move
os.system('say ' + move)
def rungame(args):
model = args.pop(0)
d = args.pop(0)
if args:
startpoint = os.path.basename(args[0])
else:
startpoint = None
run_assistant(gen_board(OCR(model), d, startpoint), make_move)
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print "Usage:", sys.argv[0], "model", "directory", "[startpoint]"
exit()
rungame(sys.argv[1:])