@@ -145,7 +145,12 @@ export class Move {
145145 before : string
146146 after : string
147147
148- constructor ( chess : Chess , internal : InternalMove ) {
148+ constructor (
149+ internal : InternalMove ,
150+ san : string ,
151+ before : string ,
152+ after : string ,
153+ ) {
149154 const { color, piece, from, to, flags, captured, promotion } = internal
150155
151156 const fromAlgebraic = algebraic ( from )
@@ -156,20 +161,10 @@ export class Move {
156161 this . from = fromAlgebraic
157162 this . to = toAlgebraic
158163
159- /*
160- * HACK: The chess['_method']() calls below invoke private methods in the
161- * Chess class to generate SAN and FEN. It's a bit of a hack, but makes the
162- * code cleaner elsewhere.
163- */
164-
165- this . san = chess [ '_moveToSan' ] ( internal , chess [ '_moves' ] ( { legal : true } ) )
164+ this . san = san
166165 this . lan = fromAlgebraic + toAlgebraic
167- this . before = chess . fen ( )
168-
169- // Generate the FEN for the 'after' key
170- chess [ '_makeMove' ] ( internal )
171- this . after = chess . fen ( )
172- chess [ '_undoMove' ] ( )
166+ this . before = before
167+ this . after = after
173168
174169 // Build the text representation of the move flags
175170 this . flags = ''
@@ -212,6 +207,10 @@ export class Move {
212207 isBigPawn ( ) {
213208 return this . flags . indexOf ( FLAGS [ 'BIG_PAWN' ] ) > - 1
214209 }
210+
211+ isNullMove ( ) {
212+ return this . flags . indexOf ( FLAGS [ 'NULL_MOVE' ] ) > - 1
213+ }
215214}
216215
217216const EMPTY = - 1
@@ -1402,6 +1401,17 @@ export class Chess {
14021401 )
14031402 }
14041403
1404+ private _createMove ( internal : InternalMove ) {
1405+ const san = this . _moveToSan ( internal , this . _moves ( { legal : true } ) )
1406+ const before = this . fen ( )
1407+
1408+ this . _makeMove ( internal )
1409+ const after = this . fen ( )
1410+ this . _undoMove ( )
1411+
1412+ return new Move ( internal , san , before , after )
1413+ }
1414+
14051415 moves ( ) : string [ ]
14061416 moves ( { square } : { square : Square } ) : string [ ]
14071417 moves ( { piece } : { piece : PieceSymbol } ) : string [ ]
@@ -1466,7 +1476,7 @@ export class Chess {
14661476 const moves = this . _moves ( { square, piece } )
14671477
14681478 if ( verbose ) {
1469- return moves . map ( ( move ) => new Move ( this , move ) )
1479+ return moves . map ( ( move ) => this . _createMove ( move ) )
14701480 } else {
14711481 return moves . map ( ( move ) => this . _moveToSan ( move , moves ) )
14721482 }
@@ -1726,7 +1736,7 @@ export class Chess {
17261736 * need to make a copy of move because we can't generate SAN after the move
17271737 * is made
17281738 */
1729- const prettyMove = new Move ( this , moveObj )
1739+ const prettyMove = this . _createMove ( moveObj )
17301740
17311741 this . _makeMove ( moveObj )
17321742 this . _incPositionCount ( )
@@ -1894,7 +1904,7 @@ export class Chess {
18941904 const hash = this . _hash
18951905 const move = this . _undoMove ( )
18961906 if ( move ) {
1897- const prettyMove = new Move ( this , move )
1907+ const prettyMove = this . _createMove ( move )
18981908 this . _decPositionCount ( hash )
18991909 return prettyMove
19001910 }
@@ -2581,7 +2591,7 @@ export class Chess {
25812591 }
25822592
25832593 if ( verbose ) {
2584- moveHistory . push ( new Move ( this , move ) )
2594+ moveHistory . push ( this . _createMove ( move ) )
25852595 } else {
25862596 moveHistory . push ( this . _moveToSan ( move , this . _moves ( ) ) )
25872597 }
0 commit comments