@@ -209,35 +209,37 @@ export function calculate(state: GameType, from: number | Color | undefined | nu
209209 return { state } ;
210210 }
211211 } else {
212- const fromInt = parseInt ( from )
212+ if ( from === undefined || from === null ) return { state } ;
213+ if ( typeof from === 'string' )
214+ from = parseInt ( from )
213215 // TODO: indexToPoint needs a color, but we don't have it for local games
214- const offense = nextGame . board [ fromInt ] ;
216+ const offense = nextGame . board [ from ] ;
215217 const defense = nextGame . board [ to ] ;
218+ const player = Math . sign ( defense ) === 1 ? Color . White : Color . Black ;
216219 if ( defense === undefined ) {
217220 // bear off
218221 moveLabel = `${ from } /off` ;
219222 if ( offense > 0 ) {
220223 // White
221- usedDie = HOME_INDEXES . white [ 1 ] - fromInt + 1 ;
224+ usedDie = HOME_INDEXES . white [ 1 ] - from + 1 ;
222225 nextGame . home . white ++ ;
223226 } else {
224227 // Black
225- usedDie = HOME_INDEXES . black [ 1 ] - fromInt + 1 ;
228+ usedDie = HOME_INDEXES . black [ 1 ] - from + 1 ;
226229 nextGame . home . black ++ ;
227230 }
228231 } else if ( ! defense || Math . sign ( defense ) === Math . sign ( offense ) ) {
229232 // move
230- moveLabel = ` ${ fromInt } / ${ to } ` ;
231- const player = state . color ;
233+
234+ moveLabel = ` ${ from } / ${ to } ` ;
232235 const dice = [ ...state . dice ] ;
233- usedDie = dice . find ( die => destination ( player , fromInt , die ) === to ) ;
236+ usedDie = dice . find ( die => destination ( player , from , die ) === to ) ;
234237 nextGame . board [ to ] += Math . sign ( offense ) ;
235238 } else if ( Math . abs ( defense ) === 1 ) {
236239 // hit
237- moveLabel = `${ fromInt } /${ to } *` ;
238- const player = state . color ;
240+ moveLabel = `${ from } /${ to } *` ;
239241 const dice = [ ...state . dice ] ;
240- usedDie = dice . find ( die => destination ( player , fromInt , die ) === to ) ;
242+ usedDie = dice . find ( die => destination ( player , from , die ) === to ) ;
241243 nextGame . board [ to ] = - Math . sign ( defense ) ;
242244 if ( offense > 0 ) nextGame . prison . black ++ ;
243245 else nextGame . prison . white ++ ;
@@ -246,7 +248,7 @@ export function calculate(state: GameType, from: number | Color | undefined | nu
246248 return { state } ;
247249 }
248250 // remove from previous position
249- nextGame . board [ fromInt ] -= Math . sign ( nextGame . board [ fromInt ] ) ;
251+ nextGame . board [ from ] -= Math . sign ( nextGame . board [ from ] ) ;
250252 if ( nextGame . home . white === 15 || nextGame . home . black === 15 )
251253 nextGame . status = Status . GameOver ;
252254 }
0 commit comments