diff --git a/source/Move.js b/source/Move.js new file mode 100644 index 0000000..c4c032d --- /dev/null +++ b/source/Move.js @@ -0,0 +1,14 @@ +export default class Move { + constructor(human, robot) { + this._human = human ? 1 : 0; + this._robot = robot ? 1 : 0; + } + + get human() { + return this._human; + } + + get robot() { + return this._robot; + } +} diff --git a/source/Movements.js b/source/Movements.js index 05d9984..29bb22a 100644 --- a/source/Movements.js +++ b/source/Movements.js @@ -1,23 +1,22 @@ +import Move from './Move'; + export default class Movements { - humanMovements = []; - robotMovements = []; + moves = []; - constructor(human = [], robot = []) { - this.humanMovements = human; - this.robotMovements = robot; + constructor(moves = []) { + this.moves = moves; } - makeHumanMove(value) { - this.humanMovements.push(value === 1 ? value : 0); - } - - makeRobotMove(value) { - this.robotMovements.push(value === 1 ? value : 0); + makeMove(human, robot) { + this.moves.push(new Move(human, robot)); } getLastMovements(humanCount, robotCount) { - const humanSlice = this.humanMovements.slice(-humanCount); - const robotSlice = this.robotMovements.slice(-robotCount); - return [].concat(robotSlice, humanSlice); + const humanMoves = this.moves.map(m => m.human); + const robotMoves = this.moves.map(m => m.robot); + return [].concat( + robotMoves.slice(-robotCount), + humanMoves.slice(-humanCount) + ); } } diff --git a/tests/DaemonTest.js b/tests/DaemonTest.js index c90feed..1e95e62 100644 --- a/tests/DaemonTest.js +++ b/tests/DaemonTest.js @@ -47,8 +47,7 @@ test('Daemon 1-1', function() { // expect(d._getStepSlice(m)).toEqual(step_slice(steps)); expect(robot).toEqual(step.robot); d.adjust(m, step.human, index + 1); - m.makeHumanMove(step.human); - m.makeRobotMove(step.robot); + m.makeMove(step.human, step.robot); console.log('Step', index + 1, d); }); }); diff --git a/tests/MovementsTest.js b/tests/MovementsTest.js index e152044..6499a76 100644 --- a/tests/MovementsTest.js +++ b/tests/MovementsTest.js @@ -1,4 +1,5 @@ import Movements from '../source/Movements'; +import Move from '../source/Move'; import expect from 'expect'; test('Create with empty constructor', function() { @@ -7,32 +8,23 @@ test('Create with empty constructor', function() { }); test('Constructor with human steps', function() { - const m = new Movements([1, 1]); + const m = new Movements([new Move(1, 1)]); expect(m.getLastMovements(5, 5)).toEqual([1, 1]); }); -test('Make human step', function() { +test('Make steps', function() { const m = new Movements(); - m.makeHumanMove(1); - m.makeHumanMove(0); - expect(m.getLastMovements(5, 5)).toEqual([1, 0]); -}); - -test('Make robot step', function() { - const m = new Movements(); - m.makeRobotMove(0); - m.makeRobotMove(1); - expect(m.getLastMovements(5, 5)).toEqual([0, 1]); -}); - -test('Make mixed steps', function() { - const m = new Movements(); - m.makeHumanMove(1); - m.makeRobotMove(0); + m.makeMove(1, 0); expect(m.getLastMovements(5, 5)).toEqual([0, 1]); }); test('Get slice', function() { - const m = new Movements([1, 0, 0, 1], [1, 1, 0, 0]); - expect(m.getLastMovements(2, 2)).toEqual([0, 0, 0, 1]); + const m = new Movements([ + new Move(1, 1), + new Move(0, 1), + new Move(0, 1), + new Move(1, 0), + ]); + + expect(m.getLastMovements(2, 2)).toEqual([1, 0, 0, 1]); });