Add Move class

This commit is contained in:
Anton Vakhrushev 2018-06-05 13:36:39 +03:00
parent f27fcf1210
commit 1c96ea31ad
4 changed files with 40 additions and 36 deletions

14
source/Move.js Normal file
View File

@ -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;
}
}

View File

@ -1,23 +1,22 @@
import Move from './Move';
export default class Movements { export default class Movements {
humanMovements = []; moves = [];
robotMovements = [];
constructor(human = [], robot = []) { constructor(moves = []) {
this.humanMovements = human; this.moves = moves;
this.robotMovements = robot;
} }
makeHumanMove(value) { makeMove(human, robot) {
this.humanMovements.push(value === 1 ? value : 0); this.moves.push(new Move(human, robot));
}
makeRobotMove(value) {
this.robotMovements.push(value === 1 ? value : 0);
} }
getLastMovements(humanCount, robotCount) { getLastMovements(humanCount, robotCount) {
const humanSlice = this.humanMovements.slice(-humanCount); const humanMoves = this.moves.map(m => m.human);
const robotSlice = this.robotMovements.slice(-robotCount); const robotMoves = this.moves.map(m => m.robot);
return [].concat(robotSlice, humanSlice); return [].concat(
robotMoves.slice(-robotCount),
humanMoves.slice(-humanCount)
);
} }
} }

View File

@ -47,8 +47,7 @@ test('Daemon 1-1', function() {
// expect(d._getStepSlice(m)).toEqual(step_slice(steps)); // expect(d._getStepSlice(m)).toEqual(step_slice(steps));
expect(robot).toEqual(step.robot); expect(robot).toEqual(step.robot);
d.adjust(m, step.human, index + 1); d.adjust(m, step.human, index + 1);
m.makeHumanMove(step.human); m.makeMove(step.human, step.robot);
m.makeRobotMove(step.robot);
console.log('Step', index + 1, d); console.log('Step', index + 1, d);
}); });
}); });

View File

@ -1,4 +1,5 @@
import Movements from '../source/Movements'; import Movements from '../source/Movements';
import Move from '../source/Move';
import expect from 'expect'; import expect from 'expect';
test('Create with empty constructor', function() { test('Create with empty constructor', function() {
@ -7,32 +8,23 @@ test('Create with empty constructor', function() {
}); });
test('Constructor with human steps', 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]); expect(m.getLastMovements(5, 5)).toEqual([1, 1]);
}); });
test('Make human step', function() { test('Make steps', function() {
const m = new Movements(); const m = new Movements();
m.makeHumanMove(1); m.makeMove(1, 0);
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);
expect(m.getLastMovements(5, 5)).toEqual([0, 1]); expect(m.getLastMovements(5, 5)).toEqual([0, 1]);
}); });
test('Get slice', function() { test('Get slice', function() {
const m = new Movements([1, 0, 0, 1], [1, 1, 0, 0]); const m = new Movements([
expect(m.getLastMovements(2, 2)).toEqual([0, 0, 0, 1]); 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]);
}); });