//===== Constructor const Stack = function() { // This is the heart of the robot movement architecture. // Its elements are of the form { robotId, i, j } this.moves = []; document.addEventListener('L-arrow', this.msgArrow.bind(this)); document.addEventListener('L-submit', this.msgSubmit.bind(this)); document.addEventListener('L-undo', this.msgUndo.bind(this)); document.addEventListener('L-reset', this.msgReset.bind(this)); document.addEventListener('G-robots', this.msgRobots.bind(this)); }; Stack.prototype.getInitialPositions = function() { const moves = this.moves.reduce((acc, move) => { if (!acc[move.id]) { acc[move.id] = move; } return acc; }, {}); return Object.values(moves); }; Stack.prototype.msgRobots = function(evt) { this.moves = evt.detail.body.map(({ id, i, j }) => ({ id, i, j })); const evtStack = new CustomEvent('L-stack', { detail: this.moves }); document.dispatchEvent(evtStack); const evtShadows = new CustomEvent('L-shadows', { detail: this.moves }); document.dispatchEvent(evtShadows); }; Stack.prototype.msgArrow = function(evt) { this.moves.push(evt.detail); const evtStack = new CustomEvent('L-stack', { detail: this.moves }); document.dispatchEvent(evtStack); }; Stack.prototype.msgReset = function() { this.moves = this.getInitialPositions(); const evtStack = new CustomEvent('L-stack', { detail: this.moves }); document.dispatchEvent(evtStack); }; Stack.prototype.msgSubmit = function() { this.moves = this.getInitialPositions(); const evtSolve = new CustomEvent('L-solve', { detail: this.moves }); document.dispatchEvent(evtSolve); }; Stack.prototype.msgUndo = function() { this.moves.pop(); const evtStack = new CustomEvent('L-stack', { detail: this.moves }); document.dispatchEvent(evtStack); };