Skip to content

Commit f694329

Browse files
authored
Merge pull request #7193 from Kenysdev/42.cs
#42 - c#
2 parents d9a2677 + ac728d3 commit f694329

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
namespace exs42;
2+
/*
3+
_____________________________________
4+
https://github.com/kenysdev
5+
2024 - C#
6+
_____________________________________
7+
42 TORNEO DRAGON BALL
8+
------------------------------------
9+
10+
* EJERCICIO:
11+
* ¡El último videojuego de Dragon Ball ya está aquí!
12+
* Se llama Dragon Ball: Sparking! ZERO.
13+
*
14+
* Simula un Torneo de Artes Marciales, al más puro estilo
15+
* de la saga, donde participarán diferentes luchadores, y el
16+
* sistema decidirá quién es el ganador.
17+
*
18+
* Luchadores:
19+
* - Nombre.
20+
* - Tres atributos: velocidad, ataque y defensa
21+
* (con valores entre 0 a 100 que tú decidirás).
22+
* - Comienza cada batalla con 100 de salud.
23+
* Batalla:
24+
* - En cada batalla se enfrentan 2 luchadores.
25+
* - El luchador con más velocidad comienza atacando.
26+
* - El daño se calcula restando el daño de ataque del
27+
* atacante menos la defensa del oponente.
28+
* - El oponente siempre tiene un 20% de posibilidad de
29+
* esquivar el ataque.
30+
* - Si la defensa es mayor que el ataque, recibe un 10%
31+
* del daño de ataque.
32+
* - Después de cada turno y ataque, el oponente pierde salud.
33+
* - La batalla finaliza cuando un luchador pierde toda su salud.
34+
* Torneo:
35+
* - Un torneo sólo es válido con un número de luchadores
36+
* potencia de 2.
37+
* - El torneo debe crear parejas al azar en cada ronda.
38+
* - Los luchadores se enfrentan en rondas eliminatorias.
39+
* - El ganador avanza a la siguiente ronda hasta que sólo
40+
* quede uno.
41+
* - Debes mostrar por consola todo lo que sucede en el torneo,
42+
* así como el ganador.
43+
*/
44+
45+
using System;
46+
using System.Collections.Generic;
47+
using System.Linq;
48+
49+
public class Fighter(string name, int speed, int attack, int defense)
50+
{
51+
public string Name { get; } = name;
52+
public int Speed { get; } = speed;
53+
public int Attack { get; } = attack;
54+
public int Defense { get; } = defense;
55+
public double Health { get; set; } = 100;
56+
57+
public void ExecuteAttack(Fighter opponent)
58+
{
59+
Console.WriteLine($"'{Name}' ataca a '{opponent.Name}'");
60+
61+
double damage = opponent.Defense >= Attack
62+
? Attack * 0.1 // 10%
63+
: Attack - opponent.Defense;
64+
65+
if (!ActivateDefense())
66+
{
67+
opponent.Health -= damage;
68+
Console.WriteLine($"'{opponent.Name}' ha recibido '{damage}' de daño");
69+
Console.WriteLine($"Salud restante '{opponent.Health}'\n");
70+
}
71+
else
72+
{
73+
Console.WriteLine($"'{opponent.Name}' ha esquivado el ataque.\n");
74+
}
75+
}
76+
77+
private static bool ActivateDefense() => Random.Shared.NextDouble() <= 0.2; // 20%
78+
79+
}
80+
81+
// __________________________________________________________
82+
public class Battle
83+
{
84+
private readonly Fighter _fighter1;
85+
private readonly Fighter _fighter2;
86+
87+
public Battle(Fighter fighter1, Fighter fighter2)
88+
{
89+
_fighter1 = fighter1;
90+
_fighter2 = fighter2;
91+
Console.WriteLine($"__'{_fighter1.Name} VS '{_fighter2.Name}'__\n");
92+
}
93+
94+
private static Fighter Combat(Fighter fighterA, Fighter fighterB)
95+
{
96+
while (true)
97+
{
98+
fighterA.ExecuteAttack(fighterB);
99+
if (fighterB.Health <= 0)
100+
{
101+
Console.WriteLine($"--> '{fighterA.Name}' gana la batalla.__\n");
102+
return fighterA;
103+
}
104+
105+
fighterB.ExecuteAttack(fighterA);
106+
if (fighterA.Health <= 0)
107+
{
108+
Console.WriteLine($"--> '{fighterB.Name}' gana la batalla.\n");
109+
return fighterB;
110+
}
111+
}
112+
}
113+
114+
public Fighter Start() =>
115+
_fighter1.Speed > _fighter2.Speed
116+
? Combat(_fighter1, _fighter2)
117+
: Combat(_fighter2, _fighter1);
118+
}
119+
120+
// __________________________________________________________
121+
public record FighterStats(int Speed, int Attack, int Defense);
122+
123+
public class Tournament(Dictionary<string, FighterStats> fighters)
124+
{
125+
private Dictionary<string, FighterStats> _fighters = fighters;
126+
127+
private bool IsPowerOf2() =>
128+
_fighters.Count > 1 && Math.Log2(_fighters.Count) % 1 == 0;
129+
130+
private (Fighter, Fighter) GetRandomPairs()
131+
{
132+
var randomFighters = _fighters.Keys.OrderBy(_ => Random.Shared.Next()).Take(2).ToArray();
133+
134+
var fighter1Data = _fighters[randomFighters[0]];
135+
var fighter2Data = _fighters[randomFighters[1]];
136+
137+
var fighter1 = new Fighter(randomFighters[0], fighter1Data.Speed, fighter1Data.Attack, fighter1Data.Defense);
138+
var fighter2 = new Fighter(randomFighters[1], fighter2Data.Speed, fighter2Data.Attack, fighter2Data.Defense);
139+
140+
_fighters.Remove(randomFighters[0]);
141+
_fighters.Remove(randomFighters[1]);
142+
143+
return (fighter1, fighter2);
144+
}
145+
146+
public void StartRounds(int roundNum = 1)
147+
{
148+
if (!IsPowerOf2())
149+
{
150+
Console.WriteLine("El número de luchadores debe ser una potencia de 2.");
151+
return;
152+
}
153+
154+
Console.WriteLine($"\n__Ronda #{roundNum}__");
155+
var nextRound = new Dictionary<string, FighterStats>();
156+
157+
while (true)
158+
{
159+
var (fighter1, fighter2) = GetRandomPairs();
160+
var battle = new Battle(fighter1, fighter2);
161+
var winner = battle.Start();
162+
163+
nextRound[winner.Name] = new FighterStats(
164+
Speed: fighter1.Speed,
165+
Attack: fighter1.Attack,
166+
Defense: fighter1.Defense
167+
);
168+
169+
if (_fighters.Count == 0 && nextRound.Count > 1)
170+
{
171+
_fighters = nextRound;
172+
StartRounds(roundNum + 1);
173+
break;
174+
}
175+
176+
if (_fighters.Count == 0 && nextRound.Count == 1)
177+
{
178+
Console.WriteLine($"\n--> El vencedor del torneo es '{winner.Name}'.");
179+
break;
180+
}
181+
}
182+
}
183+
}
184+
185+
// __________________________________________________________
186+
public static class Program
187+
{
188+
private static readonly Dictionary<string, FighterStats> Fighters = new()
189+
{
190+
["Goku"] = new(Speed: 100, Attack: 95, Defense: 85),
191+
["Vegeta"] = new(Speed: 95, Attack: 90, Defense: 90),
192+
["Gohan"] = new(Speed: 85, Attack: 95, Defense: 85),
193+
["Freezer"] = new(Speed: 90, Attack: 90, Defense: 90),
194+
["Piccolo"] = new(Speed: 90, Attack: 85, Defense: 90),
195+
["Krillin"] = new(Speed: 85, Attack: 75, Defense: 75),
196+
["Cell"] = new(Speed: 90, Attack: 95, Defense: 85),
197+
["Majin Buu"] = new(Speed: 80, Attack: 85, Defense: 95)
198+
};
199+
200+
public static void Main()
201+
{
202+
Console.WriteLine("Simulación del Torneo de Artes Marciales");
203+
var tournament = new Tournament(Fighters);
204+
tournament.StartRounds();
205+
}
206+
}

0 commit comments

Comments
 (0)