/
reversi.js
364 lines (324 loc) · 6.95 KB
/
reversi.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { RLEnvironmentBase } from './base.js'
const EMPTY = 1
const BLACK = 2
const WHITE = 3
const flipPiece = p => {
if (p === BLACK) {
return WHITE
} else if (p === WHITE) {
return BLACK
}
return EMPTY
}
/**
* Reversi environment
*/
export default class ReversiRLEnvironment extends RLEnvironmentBase {
constructor() {
super()
this._size = [8, 8]
this._board = new ReversiBoard(this._size, this._evaluation)
this._turn = BLACK
this._reward = {
win: 1,
lose: -1,
step: 0,
}
}
static BLACK = BLACK
static WHITE = WHITE
static EMPTY = EMPTY
static OWN = 2
static OTHER = 3
get actions() {
const a = [EMPTY]
for (let i = 0; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++) {
a.push(`${String.fromCharCode('a'.charCodeAt(0) + i)}${i + 1}`)
}
}
return [a]
}
get states() {
const s = [[BLACK, WHITE]]
for (let i = 0; i < this._size[0] * this._size[1]; i++) {
s.push([EMPTY, ReversiRLEnvironment.OWN, ReversiRLEnvironment.OTHER])
}
return s
}
set evaluation(func) {
if (func) {
this._board._evaluator = this._evaluation = (board, turn) => {
return func(this._makeState(board, turn, this._turn))
}
} else {
this._board._evaluator = this._evaluation = null
}
}
_makeState(board, agentturn, gameturn) {
const s = [gameturn]
for (let i = 0; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++) {
const p = board.at([i, j])
s.push(p === EMPTY ? p : p === agentturn ? ReversiRLEnvironment.OWN : ReversiRLEnvironment.OTHER)
}
}
return s
}
_state2board(state, turn) {
const board = new ReversiBoard(this._size, this._evaluation)
const opturn = flipPiece(turn)
for (let i = 0, p = 1; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++, p++) {
if (state[p] === EMPTY) {
board._board[i][j] = EMPTY
} else if (state[p] === ReversiRLEnvironment.OWN) {
board._board[i][j] = turn
} else {
board._board[i][j] = opturn
}
}
}
return board
}
_checkAgent(agent) {
if (!this._agents) {
throw new Error('Agent does not exist. Call reset to set agents.')
}
if (agent !== BLACK && agent !== WHITE) {
throw new Error('Unknown agent.')
}
}
reset() {
super.reset()
this._agents = [BLACK, WHITE]
this._board.reset()
this._turn = BLACK
return this.state(BLACK)
}
state(agent) {
if (!agent) {
agent = this._turn
}
this._checkAgent(agent)
return this._makeState(this._board, agent, this._turn)
}
setState(state, agent) {
this._turn = state[0]
this._board = this._state2board(state, agent)
}
step(action, agent) {
if (!agent) {
agent = this._turn
}
return super.step(action, agent)
}
test(state, action, agent) {
if (!agent) {
agent = this._turn
}
this._checkAgent(agent)
const gameturn = state[0]
const getreward = done =>
!done ? this._reward.step : board.winner === agent ? this._reward.win : this._reward.lose
const board = this._state2board(state, agent)
if (agent !== gameturn) {
const done = board.finish
return {
state,
reward: getreward(done),
done,
invalid: true,
}
}
if (action[0] === EMPTY) {
const choices = board.choices(agent)
const done = board.finish
const invalid = choices.length > 0
return {
state: invalid ? state : this._makeState(board, agent, flipPiece(gameturn)),
reward: getreward(done),
done,
invalid,
}
}
const changed = board.set(action[0], agent)
const done = board.finish
if (!changed) {
return {
state,
reward: getreward(done),
done,
invalid: true,
}
}
return {
state: this._makeState(board, agent, flipPiece(gameturn)),
reward: getreward(done),
done,
}
}
}
class ReversiBoard {
constructor(size, evaluator) {
this._evaluator = evaluator
this._size = size
this.reset()
}
get size() {
return this._size
}
get count() {
let b = 0
let w = 0
for (let i = 0; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++) {
if (this._board[i][j] === WHITE) {
w++
} else if (this._board[i][j] === BLACK) {
b++
}
}
}
return {
black: b,
white: w,
}
}
get finish() {
return this.choices(BLACK).length + this.choices(WHITE).length === 0
}
get winner() {
if (!this.finish) {
return null
}
const count = this.count
if (count.black > count.white) {
return BLACK
} else if (count.black < count.white) {
return WHITE
}
return null
}
toString() {
let buf = ''
for (let i = 0; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++) {
if (j > 0) {
buf += ' '
}
if (this._board[i][j] === BLACK) {
buf += 'x'
} else if (this._board[i][j] === WHITE) {
buf += 'o'
} else {
buf += '-'
}
}
buf += '\n'
}
return buf
}
nextTurn(turn) {
return flipPiece(turn)
}
copy() {
const cp = new ReversiBoard(this._size, this._evaluator)
for (let i = 0; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++) {
cp._board[i][j] = this._board[i][j]
}
}
return cp
}
score(turn) {
if (this._evaluator) {
return this._evaluator(this, turn)
}
const count = this.count
if (turn === BLACK) {
return count.black - count.white
} else {
return count.white - count.black
}
}
at(p) {
if (typeof p === 'string') {
p = [p[1] - 1, p.charCodeAt(0) - 'a'.charCodeAt(0)]
}
return this._board[p[0]][p[1]]
}
set(p, turn) {
if (typeof p === 'string') {
p = [p[1] - 1, p.charCodeAt(0) - 'a'.charCodeAt(0)]
}
const flips = this.flipPositions(p[0], p[1], turn)
if (flips.length === 0) {
return false
}
this._board[p[0]][p[1]] = turn
for (const [ti, tj] of flips) {
this._board[ti][tj] = turn
}
return true
}
reset() {
this._board = []
for (let i = 0; i < this._size[0]; i++) {
this._board[i] = Array(this._size[1]).fill(EMPTY)
}
const cx = Math.floor(this._size[0] / 2)
const cy = Math.floor(this._size[1] / 2)
this._board[cx - 1][cy - 1] = WHITE
this._board[cx - 1][cy] = BLACK
this._board[cx][cy - 1] = BLACK
this._board[cx][cy] = WHITE
}
choices(turn) {
const c = []
for (let i = 0; i < this._size[0]; i++) {
for (let j = 0; j < this._size[1]; j++) {
if (this.flipPositions(i, j, turn).length > 0) {
c.push([i, j])
}
}
}
return c
}
flipPositions(i, j, turn) {
if (i < 0 || j < 0 || this._size[0] <= i || this._size[1] <= j) {
return []
} else if (turn === EMPTY || this._board[i][j] !== EMPTY) {
return []
}
const p = []
for (const [di, dj] of [
[1, 1],
[1, 0],
[1, -1],
[0, -1],
[-1, -1],
[-1, 0],
[-1, 1],
[0, 1],
]) {
let ti = i
let tj = j
const tmp = []
while (true) {
ti += di
tj += dj
if (ti < 0 || tj < 0 || this._size[0] <= ti || this._size[1] <= tj) {
break
} else if (this._board[ti][tj] === turn) {
p.push(...tmp)
break
} else if (this._board[ti][tj] === EMPTY) {
break
}
tmp.push([ti, tj])
}
}
return p
}
}