Skip to content

Commit d2e02a4

Browse files
authored
Break circular dependency between Move and Chess (#550)
* Break circular dependency between Move and Chess * Remove defunct comment * Add corresponding method for NULL_MOVE flag
1 parent 837ae14 commit d2e02a4

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

etc/chess.js.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export const KNIGHT = "n";
249249
// @public (undocumented)
250250
export class Move {
251251
// Warning: (ae-forgotten-export) The symbol "InternalMove" needs to be exported by the entry point chess.d.ts
252-
constructor(chess: Chess, internal: InternalMove);
252+
constructor(internal: InternalMove, san: string, before: string, after: string);
253253
// (undocumented)
254254
after: string;
255255
// (undocumented)
@@ -271,6 +271,8 @@ export class Move {
271271
// (undocumented)
272272
isKingsideCastle(): boolean;
273273
// (undocumented)
274+
isNullMove(): boolean;
275+
// (undocumented)
274276
isPromotion(): boolean;
275277
// (undocumented)
276278
isQueensideCastle(): boolean;

src/chess.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

217216
const 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

Comments
 (0)