Skip to content

Commit 2dd262f

Browse files
authored
Merge pull request #6609 from Thompson6626/main
#42 - Java
2 parents a4dbf3d + 05e0bd7 commit 2dd262f

File tree

1 file changed

+289
-0
lines changed

1 file changed

+289
-0
lines changed
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
2+
import java.util.*;
3+
4+
public class thompson6626 {
5+
6+
public static void main(String[] args) {
7+
List<Luchador> luchadores = randomLuchadores();
8+
Torneo torneo = new Torneo(luchadores);
9+
torneo.iniciar();
10+
}
11+
public static List<Luchador> randomLuchadores() {
12+
List<String> dragonBallFighters = Arrays.asList(
13+
"Goku", "Vegeta", "Piccolo", "Gohan", "Freezer", "Cell",
14+
"Majin Boo", "Trunks", "Krilin", "Ten Shin Han", "Yamcha",
15+
"Broly", "Bills", "Whis", "Jiren", "Hit",
16+
"Zamas", "Androide 18", "Androide 17", "Gotenks", "Raditz",
17+
"Nappa", "Bardock", "Kid Boo", "Kaiosama", "Maestro Roshi",
18+
"Dende", "Videl", "Pan", "Mr. Satán", "Chichi", "Bulma",
19+
"Zarbon", "Dodoria", "Ginyu", "Recoome", "Burter", "Jeice", "Guldo",
20+
"Shenlong", "Tapion", "Puar", "Oolong"
21+
);
22+
23+
var random = new Random();
24+
int numeroDeLuchadores;
25+
do {
26+
numeroDeLuchadores = random.nextInt(dragonBallFighters.size() + 1);
27+
}while (numeroDeLuchadores < 2 || !Utils.esPotenciadeDos(numeroDeLuchadores));
28+
29+
Set<String> nombresTomados = new HashSet<>();
30+
List<Luchador> luchadores = new ArrayList<>();
31+
for (int i = 0; i < numeroDeLuchadores; i++) {
32+
int nombreIndex;
33+
do {
34+
nombreIndex = random.nextInt(numeroDeLuchadores);
35+
}while (nombresTomados.contains(dragonBallFighters.get(nombreIndex)));
36+
nombresTomados.add(dragonBallFighters.get(nombreIndex));
37+
luchadores.add(new Luchador(
38+
dragonBallFighters.get(nombreIndex),
39+
random.nextInt(101),
40+
random.nextInt(101),
41+
random.nextInt(101)
42+
));
43+
}
44+
return luchadores;
45+
}
46+
47+
48+
49+
static class Luchador{
50+
51+
private static final int VIDA_MAXIMA = 100;
52+
private static final int CHANCE_DE_EVASION = 20;
53+
private final Random random = new Random();
54+
private final String nombre;
55+
private final int velocidad;
56+
private final int ataque;
57+
private final int defensa;
58+
private int vida;
59+
public Luchador(String nombre,int velocidad, int ataque, int defensa) {
60+
this.nombre = nombre;
61+
this.velocidad = velocidad;
62+
this.ataque = ataque;
63+
this.defensa = defensa;
64+
this.vida = VIDA_MAXIMA;
65+
}
66+
public void descansar(){
67+
this.vida = VIDA_MAXIMA;
68+
}
69+
public boolean atacar(Luchador contrincante){
70+
int vidaRestanteDelContrincaten = contrincante.recibirDaño(this);
71+
boolean fatal = vidaRestanteDelContrincaten <= 0;
72+
return fatal;
73+
}
74+
public int recibirDaño(Luchador atacante){
75+
if (esquivar()){
76+
System.out.printf(
77+
"%s esquivo el ataque de %s %n",
78+
this.nombre ,
79+
atacante.nombre
80+
);
81+
return this.vida;
82+
}
83+
int dañoARecibir;
84+
if (this.defensa > atacante.ataque){
85+
dañoARecibir = Utils.porcentajeDe(10 , atacante.ataque);// Si la defensa es mayor al ataque
86+
System.out.printf(
87+
"%s tuvo una defensa mayor al ataque de %s y el daño se reduce a 10%% del total! (%d -> %d) %n",
88+
this.nombre,
89+
atacante.nombre,
90+
atacante.ataque,
91+
dañoARecibir
92+
);
93+
}else{
94+
dañoARecibir = Math.max(0 , atacante.ataque - this.defensa);
95+
}
96+
97+
int vidaAntesDeAtaque = this.vida;
98+
this.vida -= dañoARecibir;
99+
System.out.printf("%s recibe un daño total de %d (%d -> %d)%n",
100+
this.nombre,
101+
dañoARecibir,
102+
vidaAntesDeAtaque,
103+
this.vida
104+
);
105+
return this.vida;
106+
}
107+
public boolean esquivar() {
108+
int chance = random.nextInt(100);
109+
return chance < CHANCE_DE_EVASION;
110+
}
111+
public int getVelocidad() {
112+
return velocidad;
113+
}
114+
public int getVida() {
115+
return vida;
116+
}
117+
118+
public String getNombre() {
119+
return nombre;
120+
}
121+
}
122+
123+
static class Batalla{
124+
private final Luchador rapido;
125+
private final Luchador lento;
126+
public Batalla(Luchador luchador1, Luchador luchador2) {
127+
if (luchador1.getVelocidad() > luchador2.getVelocidad()){
128+
this.rapido = luchador1;
129+
this.lento = luchador2;
130+
}else {
131+
this.rapido = luchador2;
132+
this.lento = luchador1;
133+
}
134+
}
135+
// True gano el rapido false gano el lento
136+
public Map<Resultado,Luchador> iniciar(){
137+
System.out.printf("--- %S VS %S --- %n",
138+
rapido.getNombre(),
139+
lento.getNombre()
140+
);
141+
boolean rapidoGano = false;
142+
do{
143+
if (rapido.atacar(lento)){
144+
rapidoGano = true;
145+
break;
146+
}
147+
lento.atacar(rapido);
148+
}while (rapido.getVida() > 0 && lento.getVida() > 0);
149+
150+
Luchador ganador;
151+
Luchador perdedor;
152+
if (rapidoGano){
153+
ganador = rapido;
154+
perdedor = lento;
155+
}else{
156+
ganador = lento;
157+
perdedor = rapido;
158+
}
159+
// Recuperar vida
160+
System.out.printf(
161+
"%S gana con %d de vida restante! %n",
162+
ganador.getNombre(),
163+
ganador.getVida()
164+
);
165+
ganador.descansar();
166+
return Map.of(Resultado.GANADOR, ganador,Resultado.PERDEDOR,perdedor);
167+
}
168+
169+
170+
}
171+
static class Torneo{
172+
List<Luchador> luchadores;
173+
private final Random RANDOM = new Random();
174+
private int ronda = 1;
175+
public Torneo(List<Luchador> luchadores) {
176+
if (luchadores.size() % 2 != 0) throw new IllegalArgumentException("Numero de luchadores invalido.");
177+
this.luchadores = luchadores;
178+
}
179+
public Torneo(Luchador... luchadores){
180+
if (luchadores.length % 2 != 0) throw new IllegalArgumentException("Numero de luchadores invalido.");
181+
this.luchadores = Arrays.asList(luchadores);
182+
}
183+
184+
public void siguienteEtapa(){
185+
int luchadoresRestantes = luchadores.size();
186+
Etapa etapa = Etapa.etapaPorLuchadores(luchadoresRestantes);
187+
188+
if (etapa != null) System.out.printf("-- %s -- %n", etapa.getNombre());
189+
else System.out.printf("-- Ronda %d -- %n",ronda);
190+
191+
Set<Integer> yaJugaron = new HashSet<>();
192+
List<Luchador> pasaron = new ArrayList<>();
193+
while (yaJugaron.size() < luchadoresRestantes){
194+
int participante1;
195+
do {
196+
participante1 = RANDOM.nextInt(luchadoresRestantes);
197+
}while (yaJugaron.contains(participante1));
198+
int participante2;
199+
do {
200+
participante2 = RANDOM.nextInt(luchadoresRestantes);
201+
}while (yaJugaron.contains(participante2) || participante2 == participante1);
202+
203+
var batalla = new Batalla(
204+
luchadores.get(participante1),
205+
luchadores.get(participante2)
206+
);
207+
208+
Map<Resultado,Luchador> resultado = batalla.iniciar();
209+
System.out.printf(
210+
"%S ha sido eliminado! %n",
211+
resultado.get(Resultado.PERDEDOR).getNombre()
212+
);
213+
pasaron.add(resultado.get(Resultado.GANADOR));
214+
yaJugaron.add(participante1);
215+
yaJugaron.add(participante2);
216+
luchadores.forEach(e -> System.out.println(e.getNombre()));
217+
System.out.println(participante1 +" "+participante2);
218+
System.out.println(yaJugaron);
219+
System.out.println(luchadoresRestantes);
220+
}
221+
System.out.println(".....");
222+
luchadores = pasaron;
223+
ronda++;
224+
}
225+
public void iniciar(){
226+
while (luchadores.size() > 1) {
227+
siguienteEtapa();
228+
}
229+
Luchador ganador = luchadores.removeLast();
230+
String mensaje = String.format("%S es el ganador del torneo!!!", ganador.getNombre());
231+
232+
int ancho = mensaje.length() + 6;
233+
String borde = "*".repeat(ancho);
234+
235+
System.out.println(borde);
236+
System.out.printf("* %-" + (ancho - 4) + "s *%n", " ");
237+
System.out.printf("* %-" + (ancho - 4) + "s *%n", mensaje);
238+
System.out.printf("* %-" + (ancho - 4) + "s *%n", " ");
239+
System.out.println(borde);
240+
}
241+
242+
enum Etapa {
243+
GRAN_FINAL("Gran Final",2),
244+
SEMIFINALES("Semifinales",4),
245+
CUARTOS_DE_FINAL("Cuartos de Final",8),
246+
OCTAVOS_DE_FINAL("Octavos de Final",16);
247+
private final String nombre;
248+
private final int jugadores;
249+
public static final Etapa[] ETAPAS = values();
250+
Etapa(String nombre, int luchadores) {
251+
this.nombre = nombre;
252+
this.jugadores = luchadores;
253+
}
254+
public String getNombre() {
255+
return nombre;
256+
}
257+
public static Etapa etapaPorLuchadores(int luchadores){
258+
for(Etapa etapa : ETAPAS){
259+
if (etapa.jugadores == luchadores){
260+
return etapa;
261+
}
262+
}
263+
return null;
264+
}
265+
}
266+
267+
}
268+
enum Resultado{
269+
GANADOR,
270+
PERDEDOR
271+
}
272+
public static class Utils{
273+
public static int porcentajeDe(int porcentaje,int valor){
274+
return Math.round(((float) porcentaje / 100) * valor);
275+
}
276+
public static boolean esPotenciadeDos(int n) {
277+
if (n == 0)
278+
return false;
279+
280+
while (n != 1) {
281+
if (n % 2 != 0)
282+
return false;
283+
n = n / 2;
284+
}
285+
return true;
286+
}
287+
}
288+
289+
}

0 commit comments

Comments
 (0)