-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_openings
executable file
·92 lines (87 loc) · 2.46 KB
/
mk_openings
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
#!/usr/bin/env coffee
require './rejection_handler'
fs = require 'fs'
Book = require './book'
{ Board, BLACK, WHITE, pos_from_str, pos_to_str } = require './board'
{ unique_moves } = require './util'
{ defaults } = require './db_book_player'
argv = require 'yargs'
.options
M:
alias: 'max_moves'
desc: 'Minimum number of moves'
default: 40
type: 'number'
requiresArg: true
d:
alias: 'max_diff'
desc: 'Minimum difference of move value from best move'
default: Infinity
type: 'number'
requiresArg: true
m:
alias: 'min_visits'
desc: 'Minimum number of visits to nodes'
default: defaults.min_visits
type: 'number'
requiresArg: true
o:
alias: 'output'
desc: 'Output file'
default: 'openings.json'
type: 'string'
requiresArg: true
b:
alias: 'book'
desc: 'Book database'
default: 'book.db'
type: 'string'
requiresArg: true
v:
alias: 'verbose'
type: 'boolean'
h:
alias: 'help'
.version false
.strict()
.argv
nodes = {}
book = new Book argv.book, readonly: true
board = new Board
add_nodes = (turn, depth, movestr) ->
return if depth > argv.max_moves
moves = []
for move in unique_moves(board, turn)
flips = board.move turn, move
#console.assert flips.length
data = book.get_op_node(board)
if data and not nodes[data.code]? and not data.is_leaf and data.n_visited
moves.push {
move
value: data.pub_value * turn
n: data.n_visited
code: data.code
}
board.undo turn, move, flips
return unless moves.length
moves.sort (a, b) -> b.value - a.value
best_value = moves[0].value
for move in moves
if depth == 1 or (best_value - move.value <= argv.max_diff and
move.n >= argv.min_visits)
nodes[move.code] = { value: move.value * turn, n: move.n }
new_movestr = movestr + pos_to_str(move.move, turn)
console.log new_movestr, nodes[move.code] if argv.verbose
flips = board.move turn, move.move
#console.assert flips.length
if board.any_moves(-turn)
add_nodes -turn, depth+1, new_movestr
else if board.any_moves(turn)
add_nodes turn, depth+1, new_movestr
board.undo turn, move.move, flips
do ->
book.init()
board.move BLACK, pos_from_str('F5')
add_nodes WHITE, 1, 'F5'
console.log "#{Object.keys(nodes).length} nodes"
fs.writeFileSync argv.output, JSON.stringify(nodes)