Advertisement
AleOlivera

functions implementation

May 17th, 2025
353
0
358 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | Gaming | 0 0
  1. // ----------CODIGO PRINCIPAL DEL JUEGO-----------
  2. int vidaJugador = 100;
  3. string nombreJugador = ingresarNombre();
  4. int armaJugador = seleccionarArma(nombreJugador);
  5.  
  6. bool lucharActivo = deseaLuchar();
  7.  
  8. while (lucharActivo)
  9. {
  10.     iniciarBatalla(nombreJugador,vidaJugador,armaJugador);
  11.     lucharActivo = deseaReiniciarLucha();
  12. }
  13.  
  14. Console.Clear();
  15. Console.WriteLine("Muchas gracias por jugar!");
  16.  
  17. //-------------------FUNCIONES---------------------
  18. bool deseaLuchar()
  19. {
  20.     bool response = false;
  21.     int opcion;
  22.     do
  23.     {
  24.         Console.WriteLine("¿Querés luchar? 1) Si 2) No");
  25.         opcion = int.Parse(Console.ReadLine());
  26.  
  27.         Console.Clear();
  28.  
  29.         switch (opcion)
  30.         {
  31.             case 1:
  32.                 response = true;
  33.                 break;
  34.             case 2:
  35.                 response = false;
  36.                 break;
  37.             default:
  38.                 Console.WriteLine("El ingreso es incorrecto.");
  39.                 break;
  40.         }    
  41.  
  42.     } while (opcion < 1 || opcion > 2);
  43.     return response;
  44. }
  45. bool deseaReiniciarLucha()
  46. {
  47.     int opcion;
  48.     do
  49.     {
  50.         Console.WriteLine("¿Querés reiniciar la luchar? 1) Si 2) No");
  51.         opcion = int.Parse(Console.ReadLine());
  52.  
  53.         Console.Clear();
  54.  
  55.         if (opcion < 1 || opcion > 2)
  56.             Console.WriteLine("El ingreso es incorrecto.");
  57.  
  58.     } while (opcion < 1 || opcion > 2);
  59.  
  60.     if (opcion == 1)
  61.         return true;
  62.     else
  63.         return false;
  64. }
  65. string ingresarNombre()
  66. {
  67.     string nombre = "";
  68.     do
  69.     {
  70.         Console.WriteLine("Ingresá tu nombre para comenzar a jugar:");
  71.  
  72.         nombre = Console.ReadLine();
  73.         Console.Clear();
  74.  
  75.         if (nombre == null || nombre.Length < 8)
  76.             Console.WriteLine("Tu ingreso debe tener al menos 8 caracteres.");
  77.  
  78.     } while (nombre == null || nombre.Length < 8 );
  79.     return nombre;
  80. }
  81.  
  82. int seleccionarArma(string nombre)
  83. {
  84.     const int ESPADA = 10;
  85.     const int HACHA = 7;
  86.     const int DAGA = 3;
  87.  
  88.     int opcionesDeMenu;
  89.     int armaSeleccionada = 0;
  90.     do
  91.     {
  92.         Console.WriteLine("Bien, " + nombre + ". Elegí tu arma: 1) Espada 2) Hacha 3) Daga");
  93.         opcionesDeMenu = int.Parse(Console.ReadLine());
  94.  
  95.         switch (opcionesDeMenu)
  96.         {
  97.             case 1:
  98.                 armaSeleccionada = ESPADA;
  99.                 break;
  100.             case 2:
  101.                 armaSeleccionada = HACHA;
  102.                 break;
  103.             case 3:
  104.                 armaSeleccionada = DAGA;
  105.                 break;
  106.             default:
  107.                 Console.WriteLine("El ingreso es incorrecto");
  108.                 break;
  109.         }
  110.     } while (armaSeleccionada == 0);
  111.  
  112.     return armaSeleccionada;
  113. }
  114. bool atacarEnemigo(string nombre,int vidaJugador, int arma, int vidaMonstruo)
  115. {
  116.    
  117.     do
  118.     {
  119.         Console.WriteLine("Presiona 1 para atacar:");
  120.         int atacar = int.Parse(Console.ReadLine());
  121.  
  122.         Console.Clear();
  123.  
  124.         if (atacar == 1)
  125.         {
  126.             vidaMonstruo -= arma;
  127.             Console.WriteLine("La vida de el monstruo ahora es de " + vidaMonstruo + ".");
  128.  
  129.             Random ataqueMonstruo = new Random();
  130.             int ataqueM = ataqueMonstruo.Next(1, 15);
  131.  
  132.             vidaJugador -= ataqueM;
  133.             Console.WriteLine("El monstruo también ha atacado con un daño de " + ataqueM + ". Tu vida es de " + vidaJugador + ".");
  134.         }
  135.         else
  136.         {
  137.             Console.WriteLine("El ingreso es incorrecto.");
  138.         }
  139.  
  140.     } while (vidaMonstruo > 0 && vidaJugador > 0);
  141.    
  142.     if (vidaJugador > vidaMonstruo)
  143.         return true;
  144.     else
  145.         return false;
  146. }
  147.  
  148. void iniciarBatalla(string nombre, int vida, int arma)
  149. {
  150.     int vidaMonstruo = 100;
  151.    
  152.     Console.WriteLine("El monstruo ha ingresado a la sala. Su vida es de " + vidaMonstruo + ".");
  153.     bool victoria = atacarEnemigo(nombre, vida, arma, vidaMonstruo);
  154.  
  155.     if (victoria)
  156.         Console.WriteLine("El monstruo fue derrotado.");
  157.     else
  158.         Console.WriteLine("Perdiste.");
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement