DojoDeveloppement/Mercredi30Mai2012Rétrospective de la session du /Mercredi23Mai2012
Les sujets proposés
Le code produit ce soir
describe("RoboRally", function() {
var robo;
beforeEach(function() {
robo = new ROBO.Robo();
robo.position = [0, 0];
robo.direction = [0, 1];
});
it("should be on a terrain and execute commands and move", function() {
robo.commandes = ["tout droit", "à droite", "tout droit"];
robo.exécute();
expect(robo.position).toEqual([1,1]);
});
it("should be able to turn right twice", function() {
robo.commandes = ["tout droit", "à droite", "à droite", "tout droit"];
robo.exécute();
expect(robo.position).toEqual([0,0]);
});
it("should be able to turn left, make a U-turn, and move backward", function() {
robo.position = [10, 10];
robo.commandes = ["à gauche", "tout droit", "demi-tour", "en arrière"];
robo.exécute();
expect(robo.position).toEqual([8,10]);
});
it("should push other robots if they are in the way", function() {
robo2 = new ROBO.Robo();
robo2.position = [0, 1];
robo.commandes = ["tout droit"];
robo.adversaires = [robo2];
robo.exécute();
expect(robo2.position).toEqual([0, 2]);
});
it("should push other robots in the same direction, even if it is facing elsewhere", function() {
robo2 = new ROBO.Robo();
robo2.position = [0, -1];
robo.commandes = ["en arrière"];
robo.adversaires = [robo2];
robo.exécute();
expect(robo2.position).toEqual([0, -2]);
});
it("should timeshare with other robots", function () {
robo.commandes = ["tout droit"];
robo2 = new ROBO.Robo();
robo2.position = [5, 0];
robo2.commandes = ["tout droit"];
céduleur = new ROBO.Céduleur([robo, robo2]);
céduleur.passe();
expect(robo.position).toEqual([0, 1]);
expect(robo2.position).toEqual([5, 1]);
});
it("should push other robots during the time sharing", function() {
robo.commandes = ["tout droit"];
robo2 = new ROBO.Robo();
robo2.position = [0, 1];
robo2.commandes = ["tout droit"];
céduleur = new ROBO.Céduleur([robo, robo2]);
céduleur.passe();
expect(robo2.position).toEqual([0, 3]);
});
});
var ROBO = ROBO || {};
(function () {
var X = 0,
Y = 1;
function Robo() {
this.position = [0, 0];
this.commandes = [];
this.direction = [0, 1];
this.adversaires = [];
};
Robo.prototype.exécute = function() {
for (var i = 0; i < this.commandes.length; i++) {
switch (this.commandes[i]) {
case "tout droit":
this.bouge(this.direction);
break;
case "en arrière":
this.bouge([-this.direction[X], -this.direction[Y]]);
break;
case "à gauche":
this.direction = [-this.direction[Y], this.direction[X]];
break;
case "à droite":
this.direction = [this.direction[Y], -this.direction[X]];
break;
case "demi-tour":
this.direction = [-this.direction[X], -this.direction[Y]];
break;
}
}
}
Robo.prototype.bouge = function(direction) {
this.position[X] += direction[X];
this.position[Y] += direction[Y];
this.bouge_adversaires(direction);
}
Robo.prototype.bouge_adversaires = function(direction) {
for (var i = 0; i < this.adversaires.length; i++) {
var adv = this.adversaires[i];
if(this.position[0] == adv.position[0] && this.position[1] == adv.position[1]) {
adv.bouge(direction);
}
};
}
Céduleur = function (robos) {
this.robos = robos;
}
Céduleur.prototype.passe = function() {
for (var i = 0; i < this.robos.length; i++) {
this.robos[i].exécute();
};
}
ROBO.Robo = Robo;
ROBO.Céduleur = Céduleur;
}());