Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions 4_buenas_practicas/asesoria/todito/spec/toditoSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,58 @@ describe("Todito", function () {
});
});
});
describe("Jugador", function () {
it("bebe una vez, no está ebrio", function () {
var jugador = new Jugador();
jugador.tomarShot();
expect(jugador.estaEbrio()).toBeFalsy();
});
it("bebe 10 veces, está ebrio", function () {
var jugador = new Jugador();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
jugador.tomarShot();
expect(jugador.getShots()).toEqual(10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lo que deberías medir es el nivelAlcohol

expect(jugador.estaEbrio()).toBeTruthy();
});
});
describe("Juego", function () {
describe("aplicar castigos", function () {
it("toma el de mi izquierda", function () {
var juego = new Juego(3);
juego.aplicarCastigo("Toma el de mi izquierda", 1);
expect(juego.jugadores[0].getShots()).toEqual(1);
});
it("toma el de mi derecha", function () {
var juego = new Juego(3);
juego.aplicarCastigo("Toma el de mi derecha", 1);
expect(juego.jugadores[2].getShots()).toEqual(1);
});
it("toman todos", function () {
var juego = new Juego(3);
var i = 0;
juego.aplicarCastigo("Toman todos", 1);
for (i; i < 3; i++) {
expect(juego.jugadores[i].getShots()).toEqual(1);
}
});
it("soy el primero de la lista y toma el de mi izquierda", function () {
var juego = new Juego(3);
juego.aplicarCastigo("Toma el de mi izquierda", 0);
expect(juego.jugadores[2].getShots()).toEqual(1);
});
it("soy el último de la lista y toma el de mi derecha", function () {
var juego = new Juego(3);
juego.aplicarCastigo("Toma el de mi derecha", 2);
expect(juego.jugadores[0].getShots()).toEqual(1);
});
});
});
});
65 changes: 54 additions & 11 deletions 4_buenas_practicas/asesoria/todito/src/todito.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,71 @@ function Dado() {
}

function Todito() {
this.jugar = function () {
var dado = new Dado();

dado.lanzar();
definirCastigo(dado.resultado);
};

var castigo = "";
var definirCastigo = function (n) {
switch (n) {
case 1:
case 5:
console.log("Toman todos");
castigo = "Toman todos";
break;
case 2:
case 6:
console.log("Toma otro");
castigo = "Toma otro";
break;
case 3:
console.log("Toma el de mi derecha");
castigo = "Toma el de mi derecha";
break;
case 4:
console.log("Toma el de mi izquierda");
castigo = "Toma el de mi izquierda";
break;
}
};
this.jugar = function () {
var dado = new Dado();
dado.lanzar();
definirCastigo(dado.resultado);
};
this.getCastigo = function () {
return castigo;
};
}

function Jugador() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilizar nivelAlcohol

var shots = 0;
this.tomarShot = function () {
shots++;
};
this.estaEbrio = function () {
return shots === 10;
};
this.getShots = function () {
return shots;
};
}

function Juego(numJugadores) {
this.jugadores = [];
var i = 0;
var j = 0;
var todito = new Todito();
for (i; i < numJugadores; i++) {
this.jugadores[i] = new Jugador();
}

this.aplicarCastigo = function (castigo, index) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La función Juego no debe saber sobre reglas específicas del juego, esto le corresponde a cada implementación

switch (castigo) {
case "Toman todos":
for (j; j < this.jugadores.length; j++) {
this.jugadores[j].tomarShot();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quien hace tomar es Todito

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todito también debe tener una referencia al Array de jugadores que Juego crea

}
break;
case "Toma otro":
break;
case "Toma el de mi derecha":
index === this.jugadores.length - 1 ? this.jugadores[0].tomarShot() : this.jugadores[index+1].tomarShot();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esta lógica de izquierda, derecha, debe estar en Todito

break;
case "Toma el de mi izquierda":
index === 0 ? this.jugadores[this.jugadores.length - 1].tomarShot() : this.jugadores[index-1].tomarShot();
break;
}
};
Expand Down