-
Notifications
You must be signed in to change notification settings - Fork 0
/
play
executable file
·227 lines (215 loc) · 5.8 KB
/
play
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env coffee
require './rejection_handler'
readline = require 'readline'
{
Board
BLACK
WHITE
EMPTY
pos_from_str
square_to_char
pos_to_str
} = require './board'
{ PatternBoard } = require './pattern'
Player = require './player'
pattern_eval = require './pattern_eval'
{ format_eval } = require './util'
argv = require 'yargs'
.options
black:
alias: 'b'
desc: 'Black player'
choices: ['uct', 'human', 'minimax', 'simple', 'invert']
default: 'uct'
requiresArg: true
white:
alias: 'w'
desc: 'White player'
choices: ['uct', 'human', 'minimax', 'simple', 'invert']
default: 'minimax'
requiresArg: true
search:
alias: 's'
desc: 'Number of UCT search'
type: 'number'
default: require('./uct').defaults.max_search
requiresArg: true
book:
desc: 'Opening book'
choices: ['static', 'db', 'none']
default: 'static'
requiresArg: true
book_random:
alias: 'r'
desc: 'Randomness of opening book'
type: 'number'
default: .7
requiresArg: true
random:
alias: 'R'
desc: 'UCT randomness'
type: 'number'
default: 0
requiresArg: true
depth:
alias: 'd'
desc: 'Search depth of minimax'
type: 'number'
default: require('./minmax').defaults.max_depth
requiresArg: true
leafs:
alias: 'l'
desc: 'Maximum number of leafs to search of minimax'
type: 'number'
default: require('./minmax').defaults.max_leafs
requiresArg: true
wld:
alias: 'W'
desc: 'Depth of win-loss-draw endgame search'
type: 'number'
default: Player.defaults.solve_wld
requiresArg: true
full:
alias: 'f'
desc: 'Depth of ful endgame search'
type: 'number'
default: Player.defaults.solve_full
requiresArg: true
h:
alias: 'help'
.version false
.strict()
.argv
input = (prompt) -> new Promise (resolve) ->
rl = readline.createInterface process.stdin, process.stdout
rl.question prompt, (answer) ->
resolve answer
rl.close()
human = (board, turn) ->
prompt = square_to_char(turn) + '? '
while true
pos = await input(prompt)
if pos.toLowerCase() == 'undo'
return 'undo'
try
pos = pos_from_str(pos)
catch e
console.log 'invalid position'
continue
if board.can_move(turn, pos)
break
else
console.log 'illegal move'
{move:pos}
book =
if argv.book == 'static'
require('./static_book_player')(random: argv.book_random)
else if argv.book == 'db'
require('./db_book_player')(random: argv.book_random)
else
null
normal_eval = pattern_eval('weights.json')
logistic = normal_eval.logistic
simple = Player
book: book
strategy: require('./uct')
evaluate: require('./simple_eval')
max_search: argv.search
board_class: Board
random: argv.random
solve_wld: argv.wld
solve_full: argv.full
endgame_eval: normal_eval
minmax = Player
book: book
strategy: require('./minmax')
evaluate: normal_eval
max_depth: argv.depth
max_leafs: argv.leafs
shuffle: false
#cache_size: 0
solve_wld: argv.wld
solve_full: argv.full
endgame_eval: normal_eval
uct = Player
book: book
strategy: require('./uct')
evaluate: normal_eval
max_search: argv.search
random: argv.random
solve_wld: argv.wld
solve_full: argv.full
endgame_eval: normal_eval
invert = do->
do_invert = null
(board, turn, moves) ->
unless do_invert
invert_eval = pattern_eval('weights.json', true)
do_invert = Player
book: null
strategy: require('./uct')
evaluate: invert_eval
max_search: argv.search
inverted: true
random: argv.random
inverted: true
solve_wld: argv.wld
solve_full: argv.full
endgame_eval: invert_eval
do_invert(board, turn, moves)
time_tbl = {}
choose_player = (turn) ->
switch (if turn==BLACK then argv.black else argv.white)
when 'uct' then uct
when 'human' then human
when 'minimax' then minmax
when 'simple' then simple
when 'invert' then invert
do ->
board = new PatternBoard
turn = BLACK
pass = 0
undo_stack = []
while true
console.log board.dump true
console.log square_to_char(BLACK), board.count(BLACK), 'discs', board.list_moves(BLACK).length, 'moves'
console.log square_to_char(WHITE), board.count(WHITE), 'discs', board.list_moves(WHITE).length, 'moves'
console.log "#{board.count(EMPTY)} empty squares"
console.log square_to_char(turn), 'to move'
player = choose_player turn
if board.any_moves(turn)
while true
t = Date.now()
{move, value, solved} = await player(board, turn)
t = Date.now() - t
time_tbl[turn] = (time_tbl[turn] or 0) + t
if move == 'undo'
undo_done = false
while undo_stack.length
[t, move, flips]= undo_stack.pop()
board.undo t, move, flips
if t == turn
undo_done = true
break
if not undo_done
console.log 'cannot undo'
continue
console.log board.dump true
continue
break
flips = board.move turn, move
console.assert flips.length
undo_stack.push [turn, move, flips]
console.log "#{square_to_char turn} moved to #{pos_to_str move}"
if value?
console.log "estimated value #{format_eval(value, logistic, solved)}"
else
if board.any_moves(-turn)
console.log 'PASS'
if player == human
await input 'Press Enter'
else
break
turn = -turn
console.log "Time consumed by #{square_to_char(BLACK)}: #{time_tbl[BLACK]/1000} seconds"
console.log "Time consumed by #{square_to_char(WHITE)}: #{time_tbl[WHITE]/1000} seconds"