@@ -31,7 +31,7 @@ export function initialState() {
31
31
id : "" ,
32
32
moves : [ ] ,
33
33
baseBoard : [ ] ,
34
- countBoard : [ ] ,
34
+ historyBoard : [ ] ,
35
35
viewPos : Number . NaN ,
36
36
dim : 0 ,
37
37
handicap : 0 ,
@@ -117,10 +117,10 @@ export function moveBack(baseState) {
117
117
let moves = baseState . moves
118
118
let move = moves [ viewPos ]
119
119
let baseBoard = unApply ( baseState . baseBoard , move )
120
- let countBoard = baseState . countBoard
120
+ let historyBoard = baseState . historyBoard
121
121
return produce ( baseState , ( draft ) => {
122
122
draft . baseBoard = baseBoard
123
- draft . board = cheapRehydrate ( baseBoard , countBoard )
123
+ draft . board = cheapRehydrate ( baseBoard , historyBoard )
124
124
draft . viewPos = viewPos
125
125
if ( viewPos ) {
126
126
let previous = moves [ viewPos - 1 ]
@@ -139,10 +139,10 @@ export function moveForward(baseState) {
139
139
let moves = baseState . moves
140
140
let move = moves [ viewPos ]
141
141
let [ , updated ] = updateBoard ( baseState . baseBoard , move )
142
- let countBoard = baseState . countBoard
142
+ let historyBoard = baseState . historyBoard
143
143
return produce ( baseState , ( draft ) => {
144
144
draft . baseBoard = updated
145
- draft . board = cheapRehydrate ( updated , countBoard )
145
+ draft . board = cheapRehydrate ( updated , historyBoard )
146
146
draft . viewPos = viewPos + 1
147
147
draft . lastMove = move . action === "pass" ? undefined : move
148
148
} )
@@ -152,21 +152,21 @@ function goToEnd(baseState) {
152
152
let moves = baseState . moves
153
153
let queueLength = baseState . queueLength
154
154
let baseBoard = baseState . baseBoard
155
- let countBoard = baseState . countBoard
155
+ let historyBoard = baseState . historyBoard
156
156
for ( let i = queueLength ; i < moves . length ; i ++ ) {
157
157
let move = moves [ i ]
158
158
let previousMove = getMove ( moves , i - 1 )
159
159
let [ , updated ] = updateBoardState ( baseBoard , previousMove , move , true )
160
160
baseBoard = updated
161
161
}
162
162
return produce ( baseState , ( draft ) => {
163
- draft . board = rehydrate ( baseBoard , countBoard )
163
+ draft . board = rehydrate ( baseBoard , historyBoard )
164
164
} )
165
165
}
166
166
167
167
export function addMove ( baseState , move ) {
168
168
let { action, n} = move
169
- let { moves, baseBoard, countBoard , counting, queueLength} = baseState
169
+ let { moves, baseBoard, historyBoard , counting, queueLength} = baseState
170
170
let previousMove = getMove ( moves , n - 1 )
171
171
if ( n < moves . length ) {
172
172
return baseState
@@ -191,9 +191,9 @@ export function addMove(baseState, move) {
191
191
draft . moves . push ( storedMove )
192
192
draft . lastMove = action === "pass" ? undefined : storedMove
193
193
draft . baseBoard = updated
194
- let updatedCountBoard = updateCountBoard ( countBoard , move )
195
- draft . countBoard = updatedCountBoard
196
- draft . board = rehydrate ( updated , updatedCountBoard )
194
+ let updatedFinalBoard = counting ? historyBoard : updateHistoryBoard ( historyBoard , move )
195
+ draft . historyBoard = updatedFinalBoard
196
+ draft . board = rehydrate ( updated , updatedFinalBoard )
197
197
draft . forbidden = forbidden
198
198
if ( action === "pass" && previousMove ?. action === "pass" ) {
199
199
draft . counting = true
@@ -203,10 +203,16 @@ export function addMove(baseState, move) {
203
203
204
204
export function createGameState ( game , auth ) {
205
205
let baseBoard = Array ( game . dim )
206
- let countBoard = Array ( game . dim )
206
+ let historyBoard = Array ( game . dim )
207
207
for ( let y = 0 ; y < game . dim ; y ++ ) {
208
208
baseBoard [ y ] = new Int32Array ( game . dim )
209
- countBoard [ y ] = new Int32Array ( game . dim )
209
+ historyBoard [ y ] = [ ]
210
+ for ( let x = 0 ; x < game . dim ; x ++ ) {
211
+ historyBoard [ y ] [ x ] = {
212
+ n : - 1 ,
213
+ color : 0 ,
214
+ }
215
+ }
210
216
}
211
217
let moves = [ ]
212
218
let forbidden = [ - 1 , - 1 ]
@@ -223,15 +229,17 @@ export function createGameState(game, auth) {
223
229
if ( passes ) {
224
230
counting = true
225
231
queueLength = move . n
226
- } else {
227
- passes = 1
228
232
}
233
+ passes = 1
229
234
} else {
230
235
passes = 0
231
236
}
232
237
let previousMove = getMove ( moves , i - 1 )
233
238
let [ storedMove , updated , newForbidden ] = updateBoardState ( baseBoard , previousMove , move , counting )
234
- countBoard [ move . y ] [ move . x ] = move . n
239
+ historyBoard [ move . y ] [ move . x ] = {
240
+ n : move . n ,
241
+ color : 0 ,
242
+ }
235
243
moves . push ( storedMove )
236
244
forbidden = newForbidden
237
245
baseBoard = updated
@@ -244,9 +252,9 @@ export function createGameState(game, auth) {
244
252
handicap : game . handicap ,
245
253
counting : counting ,
246
254
baseBoard : baseBoard ,
247
- countBoard : countBoard ,
255
+ historyBoard : historyBoard ,
248
256
moves : moves ,
249
- board : rehydrate ( baseBoard , countBoard ) ,
257
+ board : rehydrate ( baseBoard , historyBoard ) ,
250
258
forbidden : forbidden ,
251
259
viewPos : queueLength || moves . length ,
252
260
queueLength : queueLength || moves . length ,
@@ -313,20 +321,20 @@ function unApply(board, move) {
313
321
return result
314
322
}
315
323
316
- function cheapRehydrate ( board , countBoard ) {
317
- let dim = board . length
324
+ function cheapRehydrate ( baseBoard , historyBoard ) {
325
+ let dim = baseBoard . length
318
326
let result = Array ( dim )
319
- for ( let i = 0 ; i < board . length ; i ++ ) {
327
+ for ( let i = 0 ; i < baseBoard . length ; i ++ ) {
320
328
result [ i ] = Array ( dim )
321
329
}
322
- for ( let y = 0 ; y < board . length ; y ++ ) {
323
- for ( let x = 0 ; x < board [ y ] . length ; x ++ ) {
330
+ for ( let y = 0 ; y < baseBoard . length ; y ++ ) {
331
+ for ( let x = 0 ; x < baseBoard [ y ] . length ; x ++ ) {
324
332
result [ y ] [ x ] = {
325
- n : countBoard [ y ] [ x ] ,
333
+ historyEntry : historyBoard [ y ] [ x ] ,
326
334
x : x ,
327
335
y : y ,
328
- color : board [ y ] [ x ] ,
329
- hasStone : hasStone ( board [ y ] [ x ] ) ,
336
+ color : baseBoard [ y ] [ x ] ,
337
+ hasStone : hasStone ( baseBoard [ y ] [ x ] ) ,
330
338
isForbidden : ( ) => false ,
331
339
liberties : 0 ,
332
340
has : ( ) => false ,
@@ -347,9 +355,17 @@ function getMove(moves, i) {
347
355
return moves [ i ]
348
356
}
349
357
350
- function updateCountBoard ( countBoard , { x, y, n} ) {
351
- let updated = countBoard . slice ( )
352
- updated [ y ] = countBoard [ y ] . slice ( )
353
- updated [ y ] [ x ] = n
358
+ function updateHistoryBoard ( historyBoard , move ) {
359
+ let { x, y, n, color, action} = move
360
+ if ( action ) {
361
+ return historyBoard
362
+ }
363
+ if ( ! color ) {
364
+ return historyBoard
365
+ }
366
+ let updated = historyBoard . slice ( )
367
+ updated [ y ] = historyBoard [ y ] . slice ( )
368
+ let oldColor = updated [ y ] [ x ] . color
369
+ updated [ y ] [ x ] = { n, color : color || oldColor }
354
370
return updated
355
371
}
0 commit comments