Skip to content

Commit 0df8431

Browse files
authored
Merge pull request #7073 from RaulDoezon/42-JavaScript
#42 - JavaScript
2 parents 4d321ee + c7c5dc2 commit 0df8431

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
EJERCICIO:
3+
¡El último videojuego de Dragon Ball ya está aquí!
4+
Se llama Dragon Ball: Sparking! ZERO.
5+
6+
Simula un Torneo de Artes Marciales, al más puro estilo
7+
de la saga, donde participarán diferentes luchadores, y el
8+
sistema decidirá quién es el ganador.
9+
10+
Luchadores:
11+
- Nombre.
12+
- Tres atributos: velocidad, ataque y defensa
13+
(con valores entre 0 a 100 que tú decidirás).
14+
- Comienza cada batalla con 100 de salud.
15+
Batalla:
16+
- En cada batalla se enfrentan 2 luchadores.
17+
- El luchador con más velocidad comienza atacando.
18+
- El daño se calcula restando el daño de ataque del
19+
atacante menos la defensa del oponente.
20+
- El oponente siempre tiene un 20% de posibilidad de
21+
esquivar el ataque.
22+
- Si la defensa es mayor que el ataque, recibe un 10%
23+
del daño de ataque.
24+
- Después de cada turno y ataque, el oponente pierde salud.
25+
- La batalla finaliza cuando un luchador pierde toda su salud.
26+
Torneo:
27+
- Un torneo sólo es válido con un número de luchadores
28+
potencia de 2.
29+
- El torneo debe crear parejas al azar en cada ronda.
30+
- Los luchadores se enfrentan en rondas eliminatorias.
31+
- El ganador avanza a la siguiente ronda hasta que sólo
32+
quede uno.
33+
- Debes mostrar por consola todo lo que sucede en el torneo,
34+
así como el ganador.
35+
*/
36+
37+
const fighters = [
38+
{
39+
name: "Goku",
40+
attributes: { speed: 90, attack: 90, defense: 65 },
41+
health: 100,
42+
},
43+
{
44+
name: "Vegeta",
45+
attributes: { speed: 85, attack: 85, defense: 70 },
46+
health: 100,
47+
},
48+
{
49+
name: "Piccolo",
50+
attributes: { speed: 75, attack: 75, defense: 70 },
51+
health: 100,
52+
},
53+
{
54+
name: "Gohan",
55+
attributes: { speed: 80, attack: 80, defense: 65 },
56+
health: 100,
57+
},
58+
{
59+
name: "Trunks",
60+
attributes: { speed: 70, attack: 70, defense: 55 },
61+
health: 100,
62+
},
63+
{
64+
name: "Goten",
65+
attributes: { speed: 70, attack: 65, defense: 60 },
66+
health: 100,
67+
},
68+
{
69+
name: "Krillin",
70+
attributes: { speed: 60, attack: 60, defense: 55 },
71+
health: 100,
72+
},
73+
{
74+
name: "Tien Shinhan",
75+
attributes: { speed: 65, attack: 60, defense: 60 },
76+
health: 100,
77+
},
78+
];
79+
80+
if (Math.log2(fighters.length) % 1 === 0) {
81+
let definePositions = new Set();
82+
83+
while(definePositions.size !== fighters.length) {
84+
definePositions.add(Math.floor(Math.random() * fighters.length));
85+
}
86+
87+
let positions = [...definePositions];
88+
89+
for (let roundIndex = 0; roundIndex <= positions.length; roundIndex++) {
90+
let numberOfFights = positions.length / 2;
91+
let fightCounter = 0;
92+
let positionsBackup = [];
93+
let fights = [];
94+
95+
for (let index = 0; index < numberOfFights; index++) {
96+
fights.push(positions.slice(fightCounter, fightCounter + 2));
97+
98+
function combat(fighter1Health, fighter2Health) {
99+
let fighter1 = fighters[fights[index][0]];
100+
let fighter2 = fighters[fights[index][1]];
101+
const fighter1Speed = fighter1.attributes.speed;
102+
const fighter2Speed = fighter2.attributes.speed;
103+
104+
if (fighter2Speed > fighter1Speed) {
105+
fighter1 = fighters[fights[index][1]];
106+
fighter2 = fighters[fights[index][0]];
107+
}
108+
109+
const fighter1Attack = Math.random() <= 0.20 ? 0 : fighter1.attributes.attack;
110+
const fighter2Attack = Math.random() <= 0.20 ? 0 : fighter2.attributes.attack;
111+
const fighter1Defense = fighter1.attributes.defense;
112+
const fighter2Defense = fighter2.attributes.defense;
113+
114+
if (fighter1Health <= 0) {
115+
console.log(`\n✌🏼 ${fighter2.name} derrotó a ${fighter1.name}`);
116+
positionsBackup.push(fighters.findIndex(x => x.name === fighter2.name));
117+
118+
return;
119+
} else if (fighter2Health <= 0) {
120+
console.log(`\n✌🏼 ${fighter1.name} derrotó a ${fighter2.name}`);
121+
positionsBackup.push(fighters.findIndex(x => x.name === fighter1.name));
122+
123+
return;
124+
}
125+
126+
console.log(`\n🏁 ${fighter1.name} (${fighter1Health}) vs ${fighter2.name} (${fighter2Health})`);
127+
console.log(`💥 ${fighter1.name} ataca`);
128+
129+
if (fighter1Attack === 0) {
130+
console.log(`🔴 ${fighter2.name} esquivó el ataque`);
131+
} else {
132+
if (fighter1Attack > fighter2Defense) {
133+
fighter2Health = fighter2Health - (fighter1Attack - fighter2Defense);
134+
} else {
135+
fighter2Health = fighter2Health - ((fighter1Attack * 10) / 100);
136+
}
137+
}
138+
139+
console.log(`💥 ${fighter2.name} ataca`);
140+
141+
if (fighter2Attack === 0) {
142+
console.log(`🔴 ${fighter1.name} esquivó el ataque`);
143+
} else {
144+
if (fighter2Attack > fighter1Defense) {
145+
fighter1Health = fighter1Health - (fighter2Attack - fighter1Defense);
146+
} else {
147+
fighter1Health = fighter1Health - ((fighter2Attack * 10) / 100);
148+
}
149+
}
150+
151+
return combat(fighter1Health, fighter2Health);
152+
}
153+
154+
combat(100, 100);
155+
156+
fightCounter = fightCounter + 2;
157+
158+
console.log('----------------------------------');
159+
}
160+
161+
positions = [...positionsBackup];
162+
163+
console.log('--------- FINAL DE RONDA ---------');
164+
165+
if (positions.length === 1) {
166+
console.log(`\n¡${fighters[positions[0]].name} es el campeón! 🏆`);
167+
}
168+
}
169+
} else {
170+
console.log('El torneo no puede llevarse a cabo porque no hay participantes suficientes.');
171+
}

0 commit comments

Comments
 (0)