-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.js
35 lines (35 loc) · 870 Bytes
/
history.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
class History {
constructor(historyInit) {
this.movesHistory = historyInit || [];
}
lastMove() {
return this.movesHistory.length > 0
? this.movesHistory[this.movesHistory.length - 1]
: undefined;
}
storeMove(move) {
this.movesHistory.push(move);
}
lastMoveToUndo() {
const lastMove = this.lastMove();
return lastMove && lastMove.undoMove ? lastMove : undefined;
}
undoLastMove() {
const lastMove = this.lastMoveToUndo();
return lastMove ? this.movesHistory.pop() : undefined;
}
hasMoved(piece) {
return this.movesHistory.find((x) => x.piece === piece) != undefined;
}
hasMovedFromIndex(piece, index) {
return (
this.movesHistory.find((x) => x.piece === piece && x.from === index) !=
undefined
);
}
}
if (typeof module !== "undefined") {
module.exports = {
History,
};
}