forked from naoyat/solitaire-solver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
freecell_solver.py
73 lines (56 loc) · 1.89 KB
/
freecell_solver.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
#!/usr/bin/env python
from __future__ import print_function
import os
import click
from freecell_image_loader import scan_freecell_image
from freecell_game_loader import parse, class_to_num, class_to_suite
from freecell_model import Model, flatten_2d
@click.group()
def cli():
pass
@cli.command()
@click.argument('image-path', type=click.Path(exists=True, readable=True))
@click.option('--short-form', is_flag=True, default=True)
def scan(image_path, short_form):
Xs = flatten_2d(scan_freecell_image(image_path, 2))
m = Model()
m.load()
pred = m.classify(Xs)
if short_form:
_pred = [
'%s%s' % (class_to_num[1 + num][-1], class_to_suite[1 + suite])
for num, suite in pred
]
else:
_pred = [
'%s%s' % (class_to_num[1 + num], class_to_suite[1 + suite])
for num, suite in pred
]
# save_txt_path = image_path.replace('.png', '.txt')
# with open(save_txt_path, 'w') as fp:
for r in range(7):
print(' '.join(_pred[r * 8 + c] for c in range(8 if r < 6 else 4)))
@cli.command()
@click.argument('game-path', type=click.Path(exists=True, readable=True))
def solve(game_path):
if game_path.endswith('.png'):
Xs = flatten_2d(scan_freecell_image(game_path, 2))
m = Model()
m.load()
pred = m.classify(Xs)
_pred = [
'%s%s' % (class_to_num[1 + num][-1], class_to_suite[1 + suite])
for num, suite in pred
]
save_txt_path = game_path.replace('.png', '.txt')
with open(save_txt_path, 'w') as fp:
for r in range(7):
row = ' '.join(_pred[r * 8 + c] for c in range(8 if r < 6 else 4))
print(row)
fp.write(row + '\n')
game_path = save_txt_path
else:
pass
os.system('FreeCellSolver %s' % game_path)
if __name__ == '__main__':
cli()