Skip to content

Instantly share code, notes, and snippets.

@delucas
Created April 24, 2015 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delucas/fe57037cb7e9f46b7165 to your computer and use it in GitHub Desktop.
Save delucas/fe57037cb7e9f46b7165 to your computer and use it in GitHub Desktop.
Luchadores Japoneses - Adaptado - UNTreF
public class Luchador {
private int peso;
private int altura;
public Luchador(int peso, int altura) {
this.peso = peso;
this.altura = altura;
}
public boolean leGanaA(Luchador otro) {
return masAltoQue(otro) && masGordoQue(otro)
|| igualDeAltoQue(otro) && masGordoQue(otro)
|| masAltoQue(otro) && igualDeGordoQue(otro);
}
private boolean masGordoQue(Luchador otro) {
return this.peso > otro.peso;
}
private boolean igualDeGordoQue(Luchador otro) {
return this.peso == otro.peso;
}
private boolean igualDeAltoQue(Luchador otro) {
return this.altura == otro.altura;
}
private boolean masAltoQue(Luchador otro) {
return this.altura > otro.altura;
}
}
public class Torneo {
private Luchador[] luchadores;
private int posicionProximoLuchador = 0;
public Torneo(final int cantidadLuchadores) {
this.luchadores = new Luchador[cantidadLuchadores];
}
public void agregarLuchador(Luchador luchador) {
if (posicionProximoLuchador >= this.luchadores.length) {
throw new Error("Demasiados luchadore', amigo!");
}
this.luchadores[posicionProximoLuchador] = luchador;
this.posicionProximoLuchador++;
}
public Luchador obtenerMasGanador() {
int[] victorias = new int[this.luchadores.length];
for(int i = 0; i < this.luchadores.length - 1; i++) {
for(int j = i + 1; j < this.luchadores.length; j++) {
Luchador l1 = this.luchadores[i];
Luchador l2 = this.luchadores[j];
if (l1.leGanaA(l2)) {
victorias[i]++;
}
if (l2.leGanaA(l1)){
victorias[j]++;
}
}
}
int indiceMayor = 0;
int victoriasMaximas = 0;
for (int i = 0; i < victorias.length; i++) {
if (victorias[i] > victoriasMaximas) {
victoriasMaximas = victorias[i];
indiceMayor = i;
}
}
return this.luchadores[indiceMayor];
}
}
import org.junit.Assert;
import org.junit.Test;
public class TorneoTests {
@Test
public void queRespetaReglasDeVictoria() {
final int PESO_NORMAL = 100;
final int ALTURA_NORMAL = 100;
Luchador gordoAlto =
new Luchador(PESO_NORMAL * 2, ALTURA_NORMAL * 2);
Luchador normalAlto =
new Luchador(PESO_NORMAL, ALTURA_NORMAL * 2);
Luchador gordoNormal =
new Luchador(PESO_NORMAL * 2, ALTURA_NORMAL);
Luchador normal =
new Luchador(PESO_NORMAL, ALTURA_NORMAL);
Assert.assertFalse(gordoAlto.leGanaA(gordoAlto));
Assert.assertTrue(gordoAlto.leGanaA(normalAlto));
Assert.assertTrue(gordoAlto.leGanaA(gordoNormal));
Assert.assertTrue(gordoAlto.leGanaA(normal));
Assert.assertTrue(normalAlto.leGanaA(normal));
Assert.assertFalse(normalAlto.leGanaA(gordoAlto));
Assert.assertFalse(normalAlto.leGanaA(gordoNormal));
Assert.assertTrue(gordoNormal.leGanaA(normal));
Assert.assertFalse(gordoNormal.leGanaA(gordoAlto));
Assert.assertFalse(gordoNormal.leGanaA(normalAlto));
Assert.assertFalse(gordoNormal.leGanaA(gordoAlto));
Assert.assertFalse(gordoNormal.leGanaA(normalAlto));
Assert.assertFalse(normal.leGanaA(gordoNormal));
}
@Test
public void queSePuedeCalcularUnGanador() {
Torneo torneo = new Torneo(3);
torneo.agregarLuchador(new Luchador(100, 200));
torneo.agregarLuchador(new Luchador(200, 100));
Luchador luchador = new Luchador(200, 200);
torneo.agregarLuchador(luchador);
Assert.assertEquals(
luchador,
torneo.obtenerMasGanador()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment