-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·142 lines (102 loc) · 3.95 KB
/
run
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
import sys
import logging
import argparse
from smash.board import Board, START_POSITION
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--fen', default=START_POSITION)
subparsers = parser.add_subparsers(
title='Commands', description='smash builtin commands')
engine_parser = subparsers.add_parser('engine', help='Run smash in engine mode')
engine_parser.add_argument('-d', '--depth', metavar='DEPTH', default=4, type=int)
engine_parser.add_argument('-l', '--log', metavar='FILENAME', default=None)
engine_parser.set_defaults(cmd=run_engine)
epd_parser = subparsers.add_parser('epd', help='Run an EPD test suite')
epd_parser.add_argument('-d', '--depth', metavar='DEPTH', default=4, type=int)
epd_parser.add_argument('file', metavar='FILENAME', type=argparse.FileType('r'))
epd_parser.set_defaults(cmd=run_epd)
shell_parser = subparsers.add_parser('shell', help='Run smash shell')
shell_parser.set_defaults(cmd=run_shell)
gentables_parser = subparsers.add_parser('gentables', help='Generate static tables')
gentables_parser.set_defaults(cmd=run_gentables)
perft_parser = subparsers.add_parser('perft', help='Run perft')
perft_parser.add_argument('depth', metavar='DEPTH', default=1, type=int,
nargs='?')
perft_parser.set_defaults(cmd=run_perft)
explore_parser = subparsers.add_parser(
'explore', help='Run perft after first move generation')
explore_parser.add_argument('depth', metavar='DEPTH', default=1, type=int,
nargs='?')
explore_parser.set_defaults(cmd=run_explore)
args = parser.parse_args()
args.cmd(args)
def run_gentables(args):
from smash.tools.gentables import gen_tables
print gen_tables()
def run_perft(args):
from smash.perft import perft
board = Board(args.fen)
print board
print perft(board, args.depth, 0)
def run_explore(args):
"""Generate the first level of moves and then run perft.
Useful for debugging if a perft result doesn't match.
"""
from smash.movegen import gen_moves
from smash.perft import perft
board = Board(args.fen)
for m in gen_moves(board):
board.move(m)
if board.is_legal():
r = perft(board, args.depth, 0)
print 'position fen %s' % board.fen()
print 'perft %s' % args.depth
print '# result: %s' % r
board.undo()
def run_shell(args):
"""Open a python shell with some useful commands"""
import code
from smash.shell.commands import ShellHelper
from smash.engine import Engine
board = Board(args.fen)
engine = Engine(depth=4)
shell_helper = ShellHelper(board, engine)
try:
import readline
except ImportError:
pass
else:
import rlcompleter
readline.set_completer(
rlcompleter.Completer(shell_helper.objects).complete)
readline.parse_and_bind('tab:complete')
banner = """
smash %s
Shell interpreter.
%s
%s
""" % ('0.2', str(board), board.fen())
code.interact(local=shell_helper.objects, banner=banner.strip())
def run_engine(args):
"""Runs the engine"""
import logging
from smash.proto import UCIProtocol
from smash.engine import Engine
from smash.board import Board
if args.log:
logging.basicConfig(filename=args.log, level=logging.DEBUG,
format='%(asctime)s - %(process)d [%(levelname)s] %(message)s')
else:
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s [%(levelname)s] %(message)s')
proto = UCIProtocol(Engine(depth=args.depth), Board)
proto.loop()
def run_epd(args):
from smash.engine import Engine
from smash.epd import EPDSuite
engine = Engine(depth=args.depth)
suite = EPDSuite(args.file)
suite.test_engine(engine)
if __name__ == '__main__':
sys.exit(main())