Skip to content

Commit d0d11d7

Browse files
mouredev#42 - Java
1 parent 6372316 commit d0d11d7

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
import java.util.concurrent.TimeUnit;
5+
import java.util.stream.Collectors;
6+
7+
public class martinbohorquez {
8+
public static void main(String[] args) throws InterruptedException {
9+
// 1. Mostrar luchadores a participar
10+
List<Figther> figthers = createFigthers();
11+
System.out.println("Los participan en el torneo de 'Dragon Ball: Sparking! ZERO!' son:");
12+
figthers.forEach(System.out::println);
13+
14+
// 2. Simular torneo
15+
simulateTournament(figthers);
16+
}
17+
18+
private static void simulateTournament(List<Figther> figthers) throws InterruptedException {
19+
printTittle("Torneo Dragon Ball: Sparking! ZERO");
20+
int rounds = (int) (Math.log(figthers.size())/Math.log(2));
21+
for (int i = 1; i <= rounds; i++) {
22+
if (i == rounds) printTittle("Ronda Final");
23+
else printTittle("Ronda número " + i);
24+
figthers = figthers.stream().filter(figther -> figther.getHealth().equals(100))
25+
.collect(Collectors.toList());
26+
Collections.shuffle(figthers);
27+
for (int j = 0; j < figthers.size() / 2; j++) {
28+
Figther winner = battle(figthers.get(j), figthers.get(figthers.size() - 1 - j));
29+
winner.setHealth(100);
30+
System.out.printf("🌟 %s ha ganado la batalla!%n", winner.getName());
31+
if (i == rounds) printTittle("👑 " + winner.getName() + " es el ganador del torneo Dragon Ball: Sparking! ZERO!");
32+
}
33+
}
34+
}
35+
36+
private static Figther battle(Figther f1, Figther f2) throws InterruptedException {
37+
printSubtitle(f1.getName().toUpperCase() + " 🆚 " + f2.getName().toUpperCase());
38+
Figther fa = f1.getSpeed() > f2.getSpeed()? f1: f2;
39+
Figther fb = fa == f1? f2: f1;
40+
System.out.printf("🏁 %s da el primer ataque.%n", fa.getName());
41+
int turn = 0;
42+
while (f1.getHealth() > 0 && f2.getHealth() > 0) {
43+
System.out.printf("▶️ Turno %s:%n", ++turn);
44+
if (fa.attack(fb)) break;
45+
if (fb.attack(fa)) break;
46+
TimeUnit.SECONDS.sleep(1);
47+
}
48+
return fb.getHealth().equals(0)? fa: fb;
49+
}
50+
51+
private static void printTittle(String tittle) {
52+
int length = tittle.length();
53+
String blankSpaces = " ".repeat(Math.max(20 - length / 2, 0));
54+
System.out.println("\n" + blankSpaces + "╔" + "═".repeat(length + 2) + "╗");
55+
System.out.println(blankSpaces + "║ " + tittle + " ║");
56+
System.out.println(blankSpaces + "╚" + "═".repeat(length + 2) + "╝");
57+
}
58+
59+
private static void printSubtitle(String subtitle) {
60+
int length = subtitle.length();
61+
System.out.println("╔" + "═".repeat(length + 2) + "╗");
62+
System.out.println("║ " + subtitle + " ║");
63+
System.out.println("╚" + "═".repeat(length + 2) + "╝");
64+
}
65+
66+
private static List<Figther> createFigthers(){
67+
return new ArrayList<>(List.of(
68+
new Figther("Goku", 90, 95, 80),
69+
new Figther("Vegeta", 85, 100, 75),
70+
new Figther("Gohan", 80, 90, 70),
71+
new Figther("Frieza", 70, 85, 60),
72+
new Figther("Cell", 75, 88, 65),
73+
new Figther("Piccolo", 60, 80, 70),
74+
new Figther("Krillin", 65, 70, 60),
75+
new Figther("Majin Buu", 55, 75, 85)));
76+
}
77+
78+
private static class Figther {
79+
private Integer id;
80+
private String name;
81+
private Integer speed;
82+
private Integer attack;
83+
private Integer defense;
84+
private Integer health;
85+
private static Integer count = 0;
86+
87+
public Figther(String name, Integer speed, Integer attack, Integer defense) {
88+
this.id = ++count;
89+
this.name = name;
90+
this.speed = speed;
91+
this.attack = attack;
92+
this.defense = defense;
93+
this.health = 100;
94+
}
95+
96+
public Integer getId() {
97+
return id;
98+
}
99+
100+
public void setId(Integer id) {
101+
this.id = id;
102+
}
103+
104+
public String getName() {
105+
return name;
106+
}
107+
108+
public Integer getSpeed() {
109+
return speed;
110+
}
111+
112+
public Integer getAttack() {
113+
return attack;
114+
}
115+
116+
public Integer getDefense() {
117+
return defense;
118+
}
119+
120+
public Integer getHealth() {
121+
return health;
122+
}
123+
124+
public void setHealth(Integer health) {
125+
this.health = health;
126+
}
127+
128+
public void decreseHealth(Integer damage) {
129+
this.health = Math.max(health - damage, 0);
130+
}
131+
132+
public boolean attack(Figther f) {
133+
int damage = f.getDefense() > this.getAttack()
134+
? (int) (this.getAttack() * 0.1): this.getAttack() - f.getDefense();
135+
System.out.printf("⚔️ %s ataca a %s con una daño de %d.%n", this.name, f.name, damage);
136+
137+
if (Math.random() < 0.2) System.out.printf("🌀 %s esquivó el ataque.%n", f.getName());
138+
else {
139+
f.decreseHealth(damage);
140+
System.out.printf("❤️ Salud de %s se reduce a %d.%n", f.name, f.getHealth());
141+
}
142+
return f.getHealth().equals(0);
143+
}
144+
145+
@Override
146+
public String toString() {
147+
return "id: " + id + ", {name: '" + name + "', speed: " + speed + ", attack: " + attack
148+
+ ", defense: " + defense + ", health: " + health + "}";
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)