Skip to content

Commit 9f10f00

Browse files
committed
mouredev#42 - Java
1 parent 1995983 commit 9f10f00

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
import java.util.*;
2+
3+
public class Josegs95 {
4+
public static void main(String[] args) {
5+
new Josegs95().DragonBallTournament();
6+
}
7+
8+
private double delay = 1;
9+
10+
public void DragonBallTournament(){
11+
List<Fighter> participantList = getFighters();
12+
13+
if (!checkParticipantCount(participantList.size())){
14+
System.out.println("No se puede empezar el torneo porque no hay suficientes participantes");
15+
return;
16+
}
17+
System.out.print("Introduzca el delay para las peleas (en segundos): ");
18+
delay = Double.parseDouble(new Scanner(System.in).nextLine());
19+
20+
System.out.println("¡¡ Comienza el torneo de artes marciales !!");
21+
System.out.println();
22+
23+
Fighter winner = tournament(new ArrayList<>(participantList));
24+
25+
System.out.println("\n");
26+
System.out.println("¡¡" + winner.getNAME() + " ha ganado el torneo de artes marciales!!");
27+
}
28+
29+
private Fighter tournament(ArrayList<Fighter> participants){
30+
int nParticipants = participants.size();
31+
switch (nParticipants){
32+
case 2:
33+
System.out.println("¡ Empieza la final !");
34+
break;
35+
case 4:
36+
System.out.println("¡ Empieza las semifinales !");
37+
break;
38+
case 8:
39+
System.out.println("¡ Empieza los cuartos de final !");
40+
break;
41+
case 16:
42+
System.out.println("¡ Empieza los octavos de final !");
43+
break;
44+
default:
45+
System.out.println("¡ Empieza la ronda de " + nParticipants + " !");
46+
break;
47+
}
48+
Collections.shuffle(participants);
49+
System.out.print("COMBATES: ");
50+
for (int i = 0; i < nParticipants; i += 2){
51+
System.out.print("[" + participants.get(i).getNAME() + " vs " + participants.get(i + 1).getNAME() + "] ");
52+
}
53+
System.out.println();
54+
55+
System.out.println("Pulse INTRO para continuar la simulación");
56+
new Scanner(System.in).nextLine();
57+
58+
List<Fighter> loserList = new ArrayList<>();
59+
for (int i = 0; i < nParticipants; i += 2){
60+
battle(participants.get(i), participants.get(i + 1), loserList);
61+
System.out.println();
62+
}
63+
64+
participants.removeAll(loserList);
65+
66+
if (participants.size() == 1)
67+
return participants.get(0);
68+
else
69+
return tournament(participants);
70+
}
71+
72+
private void battle (Fighter fighter1, Fighter fighter2, List<Fighter> loserList){
73+
Fighter first, last;
74+
if (fighter1.getSPD() > fighter2.getSPD()) {
75+
first = fighter1;
76+
last = fighter2;
77+
} else if (fighter1.getSPD() < fighter2.getSPD()) {
78+
first = fighter2;
79+
last = fighter1;
80+
} else {
81+
first = Math.random() < 0.5 ? fighter1 : fighter2;
82+
last = first == fighter1 ? fighter2 : fighter1;
83+
}
84+
85+
try {
86+
int round = 1;
87+
while(first.isAlive()){
88+
System.out.print("Ronda " + round++ + ". ");
89+
System.out.print(first.getNAME() + "(" + first.getHp() + "/100) ");
90+
System.out.println(last.getNAME() + "(" + last.getHp() + "/100)");
91+
92+
attack(first, last);
93+
if (!last.isAlive())
94+
break;
95+
attack(last, first);
96+
97+
if (round < 20)
98+
Thread.sleep((long) (delay * 1000));
99+
System.out.println();
100+
}
101+
} catch (InterruptedException e) {
102+
throw new RuntimeException(e);
103+
}
104+
105+
106+
if (first.isAlive()) {
107+
System.out.println(first.getNAME() + " ha ganado el combate.");
108+
first.heal();
109+
loserList.add(last);
110+
} else {
111+
System.out.println(last.getNAME() + " ha ganado el combate.");
112+
last.heal();
113+
loserList.add(first);
114+
}
115+
}
116+
117+
private void attack(Fighter attacker, Fighter defender){
118+
if (Math.random() < 0.2){
119+
System.out.println(defender.getNAME() + " ha esquivado el ataque de " + attacker.getNAME());
120+
return;
121+
}
122+
123+
int attackerATK = attacker.getATK();
124+
int defenderDEF = defender.getDEF();
125+
double damageDealt = Math.max(attackerATK - defenderDEF, 10);
126+
127+
if (attackerATK < defenderDEF)
128+
damageDealt *= 0.1;
129+
130+
defender.takeDamage(damageDealt);
131+
System.out.println(attacker.getNAME() + " ha hecho " + damageDealt + " puntos de daño a "
132+
+ defender.getNAME());
133+
}
134+
135+
private boolean checkParticipantCount(int nParticipants){
136+
for (int i = 1, participantLimit; (participantLimit = (int) Math.pow(2, i)) <= nParticipants; i++){
137+
if (participantLimit == nParticipants)
138+
return true;
139+
}
140+
141+
return false;
142+
}
143+
144+
private List<Fighter> getFighters(){
145+
Fighter goku = new Fighter("Goku", 80, 50, 60);
146+
Fighter vegeta = new Fighter("Vegeta", 70, 60, 55);
147+
Fighter piccolo = new Fighter("Piccolo");
148+
Fighter gohan = new Fighter("Gohan");
149+
Fighter krilin = new Fighter("Krilin");
150+
Fighter yamcha = new Fighter("Yamcha");
151+
Fighter tien = new Fighter("Tien");
152+
Fighter jackie = new Fighter("Jackie Chun");
153+
154+
return Arrays.asList(goku, vegeta, piccolo, gohan,
155+
krilin, yamcha, tien, jackie);
156+
}
157+
158+
public class Fighter{
159+
final private String NAME;
160+
final private int ATK;
161+
final private int DEF;
162+
final private int SPD;
163+
private int hp = 100;
164+
165+
public Fighter(String name, int attack, int defense, int speed) {
166+
this.NAME = name;
167+
this.ATK = attack;
168+
this.DEF = defense;
169+
this.SPD = speed;
170+
}
171+
172+
public Fighter(String name){
173+
this.NAME = name;
174+
Random rnd = new Random();
175+
ATK = rnd.nextInt(0, 101);
176+
DEF = rnd.nextInt(0, 101);
177+
SPD = rnd.nextInt(0, 101);
178+
}
179+
180+
public void takeDamage(double amount){
181+
hp -= amount;
182+
hp = (hp < 0) ? 0 : hp;
183+
}
184+
185+
public void heal(){
186+
hp = 100;
187+
}
188+
189+
public boolean isAlive(){
190+
return hp > 0;
191+
}
192+
193+
public String getNAME() {
194+
return NAME;
195+
}
196+
197+
public int getATK() {
198+
return ATK;
199+
}
200+
201+
public int getDEF() {
202+
return DEF;
203+
}
204+
205+
public int getSPD() {
206+
return SPD;
207+
}
208+
209+
public int getHp() {
210+
return hp;
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)