Skip to content

Commit f58cad5

Browse files
authored
Merge pull request #5412 from deathwing696/main
#32 - C#
2 parents 238c5be + bc61967 commit f58cad5

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/*
2+
* EJERCICIO:
3+
* ¡Deadpool y Wolverine se enfrentan en una batalla épica!
4+
* Crea un programa que simule la pelea y determine un ganador.
5+
* El programa simula un combate por turnos, donde cada protagonista posee unos
6+
* puntos de vida iniciales, un daño de ataque variable y diferentes cualidades
7+
* de regeneración y evasión de ataques.
8+
* Requisitos:
9+
* 1. El usuario debe determinar la vida inicial de cada protagonista.
10+
* 2. Cada personaje puede impartir un daño aleatorio:
11+
* - Deadpool: Entre 10 y 100.
12+
* - Wolverine: Entre 10 y 120.
13+
* 3. Si el daño es el máximo, el personaje que lo recibe no ataca en el
14+
* siguiente turno, ya que tiene que regenerarse (pero no aumenta vida).
15+
* 4. Cada personaje puede evitar el ataque contrario:
16+
* - Deadpool: 25% de posibilidades.
17+
* - Wolverine: 20% de posibilidades.
18+
* 5. Un personaje pierde si sus puntos de vida llegan a cero o menos.
19+
* Acciones:
20+
* 1. Simula una batalla.
21+
* 2. Muestra el número del turno (pausa de 1 segundo entre turnos).
22+
* 3. Muestra qué pasa en cada turno.
23+
* 4. Muestra la vida en cada turno.
24+
* 5. Muestra el resultado final.
25+
*/
26+
27+
using System;
28+
using System.CodeDom;
29+
using System.Collections.Generic;
30+
using System.Security.Cryptography;
31+
using System.Threading;
32+
using System.Threading.Tasks;
33+
34+
namespace deathwing696
35+
{
36+
public class deathwing696
37+
{
38+
const string kMENSAJE_ERROR = "No hay batalla en curso, simular primero una batalla";
39+
40+
public class Wolverine
41+
{
42+
private int hp;
43+
private int attack_min;
44+
private int attack_max;
45+
46+
public int HP { get { return hp; } }
47+
48+
public Wolverine(int hp)
49+
{
50+
this.hp = hp;
51+
this.attack_min = 10;
52+
this.attack_max = 120;
53+
}
54+
55+
public int Attack(out bool isMaxAttack)
56+
{
57+
Random random = new Random();
58+
int attack = random.Next(attack_min, attack_max);
59+
isMaxAttack = attack == this.attack_max;
60+
61+
return attack;
62+
63+
}
64+
65+
public bool Dodge()
66+
{
67+
Random random = new Random();
68+
int prob = random.Next(1, 5);
69+
70+
return prob == 1;
71+
}
72+
73+
public bool RecievesDamage(int damage)
74+
{
75+
this.hp -= damage;
76+
77+
return this.hp <= 0;
78+
}
79+
}
80+
public class Deadpool
81+
{
82+
private int hp;
83+
private int attack_min = 10;
84+
private int attack_max = 100;
85+
86+
public int HP { get { return hp; } }
87+
88+
public Deadpool(int hp)
89+
{
90+
this.hp = hp;
91+
}
92+
93+
public int Attack(out bool isMaxAttack)
94+
{
95+
Random random = new Random();
96+
int attack = random.Next(attack_min, attack_max);
97+
isMaxAttack = attack == this.attack_max;
98+
99+
return attack;
100+
101+
}
102+
103+
public bool Dodge()
104+
{
105+
Random random = new Random();
106+
int prob = random.Next(1, 4);
107+
108+
return prob == 1;
109+
}
110+
111+
public bool RecievesDamage(int damage)
112+
{
113+
this.hp -= damage;
114+
115+
return this.hp <= 0;
116+
}
117+
118+
}
119+
public class Battle
120+
{
121+
private int wolverineHP;
122+
private int deadpoolHP;
123+
private int turn;
124+
private string description;
125+
private static string winner;
126+
127+
public int WolverineHP { get { return wolverineHP; } }
128+
public int DeadpoolHP { get { return deadpoolHP; } }
129+
public int Turn { get { return turn; } }
130+
public string Description { get { return description; } }
131+
public static string Winner { get; set; }
132+
133+
public Battle(int wolverineHP, int deadpoolHP, int turn, string description)
134+
{
135+
this.wolverineHP = wolverineHP;
136+
this.deadpoolHP = deadpoolHP;
137+
this.turn = turn;
138+
this.description = description;
139+
}
140+
141+
public string ShowBattleLog()
142+
{
143+
return $"turno {this.turn}: {this.description}";
144+
}
145+
146+
public string ShowBattleHP()
147+
{
148+
return $"turno {this.turn}: La vida de Wolverine es {this.wolverineHP}. La vida de Deadpool es {this.deadpoolHP}";
149+
}
150+
}
151+
152+
static void Main(string[] args)
153+
{
154+
Console.Write("Introduce la vida de Wolverine:");
155+
int wolverineHP = Int16.Parse(Console.ReadLine());
156+
Console.Write("Introduce la vida de Deadpool:");
157+
int deadPoolHP = Int16.Parse(Console.ReadLine());
158+
Wolverine wolverine = new Wolverine(wolverineHP);
159+
Deadpool deadpool = new Deadpool(deadPoolHP);
160+
List<Battle> log = new List<Battle>();
161+
bool isSimulating = false;
162+
163+
while(true)
164+
{
165+
Console.WriteLine("1.Simula una batalla.");
166+
Console.WriteLine("2.Muestra el número del turno(pausa de 1 segundo entre turnos).");
167+
Console.WriteLine("3.Muestra qué pasa en cada turno.");
168+
Console.WriteLine("4.Muestra la vida en cada turno.");
169+
Console.WriteLine("5.Muestra el resultado final.");
170+
Console.Write("Introduce una opción:");
171+
172+
int option = Int16.Parse(Console.ReadLine());
173+
174+
switch(option)
175+
{
176+
case 1:
177+
if (!isSimulating)
178+
{
179+
Task.Run(() => SimulaBatalla(wolverine, deadpool, log));
180+
isSimulating = true;
181+
}
182+
break;
183+
case 2:
184+
if (log.Count == 0)
185+
{
186+
Console.WriteLine(kMENSAJE_ERROR);
187+
}
188+
else
189+
{
190+
Battle battle = log[log.Count - 1];
191+
192+
if (battle != null)
193+
Console.WriteLine($"Turno {battle.Turn}");
194+
}
195+
break;
196+
case 3:
197+
if (log.Count == 0)
198+
{
199+
Console.WriteLine(kMENSAJE_ERROR);
200+
}
201+
else
202+
{
203+
foreach (Battle turn in log)
204+
Console.Write(turn.ShowBattleLog());
205+
}
206+
break;
207+
case 4:
208+
if (log.Count == 0)
209+
{
210+
Console.WriteLine(kMENSAJE_ERROR);
211+
}
212+
else
213+
{
214+
foreach (Battle turn in log)
215+
Console.WriteLine(turn.ShowBattleHP());
216+
}
217+
break;
218+
case 5:
219+
if (log.Count == 0)
220+
{
221+
Console.WriteLine("No ha habido batalla");
222+
}
223+
else
224+
{
225+
Console.WriteLine($"El ganador de la batalla ha sido {Battle.Winner}");
226+
}
227+
228+
Console.Write("Batalla terminada");
229+
Console.ReadKey();
230+
return;
231+
default:
232+
Console.WriteLine("Opción inválida");
233+
break;
234+
}
235+
}
236+
}
237+
238+
private static async void SimulaBatalla(Wolverine wolverine, Deadpool deadpool, List<Battle> log)
239+
{
240+
bool isMaxAttackWolverin = false;
241+
bool isMaxAttackDeadpool = false;
242+
int turn = 1;
243+
244+
while (wolverine.HP > 0 && deadpool.HP > 0)
245+
{
246+
string description = String.Empty;
247+
248+
if (!isMaxAttackWolverin)
249+
{
250+
int wolverineAttack = wolverine.Attack(out isMaxAttackWolverin);
251+
if (!deadpool.Dodge())
252+
{
253+
deadpool.RecievesDamage(wolverineAttack);
254+
description += $"Wolverine ataca a Deadpool y le hace {wolverineAttack} puntos de daño dejando a Deadpool con {deadpool.HP} puntos de vida\n";
255+
}
256+
else
257+
{
258+
description += $"Wolverine ataca a Deadpool pero este esquiva el ataque\n";
259+
}
260+
}
261+
262+
if (!isMaxAttackDeadpool)
263+
{
264+
int deadpoolAttack = deadpool.Attack(out isMaxAttackDeadpool);
265+
266+
if (!wolverine.Dodge())
267+
{
268+
wolverine.RecievesDamage(deadpoolAttack);
269+
description += $"Deadpool ataca a Wolverine y le hace {deadpoolAttack} puntos de daño dejando a Wolverine con {wolverine.HP} puntos de vida\n";
270+
}
271+
else
272+
{
273+
description += $"Deadpool ataca a Wolverine pero este consigue esquivar el ataque\n";
274+
}
275+
}
276+
277+
Battle battle = new Battle(wolverine.HP, deadpool.HP, turn, description);
278+
log.Add(battle);
279+
280+
turn++;
281+
await Task.Delay(1000);
282+
}
283+
284+
if (wolverine.HP <= 0 && deadpool.HP > 0)
285+
Battle.Winner = "Deadpool";
286+
else if (deadpool.HP <= 0 && wolverine.HP > 0)
287+
Battle.Winner = "Wolverine";
288+
else
289+
Battle.Winner = "Empate";
290+
}
291+
}
292+
}

0 commit comments

Comments
 (0)