Advertisement
SuperSpaceMan230

Untitled

Nov 28th, 2022 (edited)
5,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.Scanner;
  5.  
  6. public class borrado {
  7.     /**
  8.      * Lista de cosas nuevas:
  9.      * -Enemigos, cada uno con su dificultad unica.
  10.      * -Modo debug, para probar el juego
  11.      * -Partida guardada
  12.      *
  13.      * @author [BORRADO]
  14.      */
  15.     public static class save {
  16.         /**
  17.          * Clase utilizada para administracion de la partida,
  18.          * Partidas(Int)= Partidas en total
  19.          * PartidasGanadas = Partidas que se han ganado
  20.          * Terminada(bool) = Indica si la partida se ha acabado, si es true para el
  21.          * bucle
  22.          * ModoDebug(bool) = Muestra el número pensado, pensado para pruebas
  23.          *
  24.          * @author [BORRADO]
  25.          */
  26.         int Partidas = 0;
  27.         int PartidasGanadas = 0;
  28.         enemigo EnemigoActual = new enemigo();
  29.         boolean Terminada = false;
  30.         boolean ModoDebug = false; // Muestra el numero pensado, intencionado para probar que el programa funciona
  31.                                     // bien
  32.  
  33.     }
  34.  
  35.     public static class enemigo {
  36.         /**
  37.          *
  38.          */
  39.         String Sprite = "slime";
  40.         int Dificultad = 0;
  41.  
  42.     }
  43.  
  44.     public static int IntAleatorio(int Max, int Min) {
  45.         /**
  46.          * Devuelve un numero aleatorio entre Max y Min
  47.          *
  48.          * @param Max el numero máximo del generador
  49.          * @param Min el numero mínimo del generador
  50.          * @return Int generado
  51.          * @author [BORRADO]
  52.          *
  53.          */
  54.         int numero = (int) (Math.random() * Max + Min);
  55.         return numero;
  56.     }
  57.  
  58.     public static String LeerSprite(String path) throws IOException {
  59.  
  60.         byte[] Archivo = Files.readAllBytes(Paths.get(path));
  61.         String Sprite = new String(Archivo).toString();
  62.         /*
  63.          * TODO:
  64.          * -Opcion para dibujar sprites sin color, el color viene desactivado
  65.          * por defecto en windows y queda feo
  66.          */
  67.         // Sprite = Sprite.replaceAll("W", " ");
  68.         // Sprite = Sprite.replaceAll("B", " ");
  69.         Sprite = Sprite.replaceAll("W", "█");
  70.         Sprite = Sprite.replaceAll("B", "░");
  71.         /**
  72.          * Lee un archivo de la carpeta Sprite, para generar imagenes desde la linea de
  73.          * comandos.
  74.          * Reemplaza W con █, y B con ░
  75.          *
  76.          * @param path Directorio y archivo de texto donde está ("a/b.txt")
  77.          * @return String un String del sprite
  78.          * @throws IOException en el caso de que no se encuentre el archivo
  79.          * @author [BORRADO]
  80.          *
  81.          */
  82.         return Sprite;
  83.     }
  84.  
  85.     public static save AdivinaMiNumero_Main(save Partida) throws IOException {
  86.         /**
  87.          * \brief Funcion principal del juego
  88.          *
  89.          * @author [BORRADO]
  90.          */
  91.         enemigo Slime = new enemigo();
  92.         enemigo Gusano = new enemigo();
  93.         enemigo Gato = new enemigo();
  94.         enemigo Fant = new enemigo();
  95.         Slime.Sprite = "Slime.txt";
  96.         Slime.Dificultad = 1;
  97.         Gusano.Sprite = "worm.txt";
  98.         Gusano.Dificultad = 3;
  99.         Gato.Sprite = "cat.txt";
  100.         Gato.Dificultad = 2;
  101.         Fant.Sprite = "bigbad.txt";
  102.         Fant.Dificultad = 4;
  103.         enemigo Enemigos[] = { Slime, Gusano, Gato, Fant };
  104.         enemigo EnemigoElegido = Enemigos[IntAleatorio(Enemigos.length, 0)];
  105.         System.out.println("");
  106.         Scanner sc = new Scanner(System.in);
  107.         String Sprite = LeerSprite("./sprites/" + EnemigoElegido.Sprite);
  108.         int NumeroMaximo = 100 * EnemigoElegido.Dificultad;
  109.         boolean Fallado = false;
  110.         boolean Preguntar = true;
  111.         boolean Valido = true; // Si no se ha introducido algo valido no sumar, este bool se asegura de esto
  112.         int NumeroPensado = IntAleatorio(NumeroMaximo, 1);
  113.         int NumeroIntento = 0;
  114.         System.out.println("Pienso un numero entre 1 y " + NumeroMaximo + "(ambos inclusive)");
  115.         System.out.println(Sprite);
  116.         if (Partida.ModoDebug) {
  117.             System.out.println("Ya que estas probando el programa, puedo decirte que estoy pensando en el numero "
  118.                     + NumeroPensado + ".");
  119.         }
  120.         if (Partida.Partidas == 0) {
  121.  
  122.             // System.out.println("Pienso un numero entre 1 y "+NumeroMaximo+"(ambos
  123.             // inclusive)");
  124.  
  125.             Partida.EnemigoActual = EnemigoElegido;
  126.             System.out.println("Trata de adivinarlo. Tienes 10 intentos: ");
  127.             System.out.println("(introduce 0 si te  cansas)");
  128.  
  129.         }
  130.         String NumeroMYoMN = "";
  131.         System.out.print("Adivina mi numero: ");
  132.         NumeroIntento = sc.nextInt();
  133.         if (NumeroIntento < 0 || NumeroIntento > 10000) {
  134.             System.out.println("El numero debe estar comprendido entre 1 y 10000");
  135.             Valido = false;
  136.         } else if (Partida.Partidas >= 10) {
  137.             System.out.println("Ya has tenido tus 10 intentos y no has acertado");
  138.             System.out.println("Adios");
  139.             Preguntar = false;
  140.             Partida.Terminada = true;
  141.         }
  142.  
  143.         if (NumeroIntento == NumeroPensado) {
  144.             System.out.println("Has acertado!!! Solo has necesitado " + Partida.Partidas + " intentos.");
  145.             // System.out.println("Adios");
  146.             Partida.Terminada = true;
  147.             Partida.Partidas++;
  148.             Partida.PartidasGanadas++;
  149.         } else if (NumeroIntento > NumeroPensado) {
  150.             NumeroMYoMN = "menor";
  151.             Fallado = true;
  152.         } else if (NumeroIntento < NumeroPensado) {
  153.             NumeroMYoMN = "mayor";
  154.             Fallado = true;
  155.         }
  156.  
  157.         if (NumeroIntento == 0) {
  158.             Preguntar = false;
  159.             Fallado = false;
  160.             Partida.Terminada = true;
  161.             System.out.println("Te has cansado");
  162.             System.out.println("Adios");
  163.         }
  164.  
  165.         if (Valido) {
  166.             Partida.Partidas++;
  167.         }
  168.         if (Fallado && !Partida.Terminada) {
  169.             System.out.println("Mi numero es " + NumeroMYoMN + ". Llevas " + Partida.Partidas + " Intentos");
  170.         } else {
  171.             Partida.Partidas = 0;
  172.         }
  173.         if (Preguntar) {
  174.             String Respuesta = "";
  175.             System.out.print("Otra partidita? [Si/No]: ");
  176.             Respuesta = sc.next();
  177.             if (Respuesta.toLowerCase().equals("no")) {
  178.                 System.out.println("Adios");
  179.                 Partida.Terminada = true;
  180.                 sc.close();
  181.             } else {
  182.                 Partida.Terminada = false;
  183.             }
  184.         }
  185.         System.out.println("============");
  186.         return Partida;
  187.     }
  188.  
  189.     public static void main(String[] args) throws IOException {
  190.         save Partida = new save();
  191.         Partida.Partidas = 0;
  192.         Partida.PartidasGanadas = 0;
  193.         if (args.length > 0) {
  194.             if (args[0].matches("-debug")) {
  195.                 Partida.ModoDebug = true;
  196.             }
  197.         }
  198.         /**
  199.          * Funcion principal del programa
  200.          *
  201.          * @param -debug Activa el modo debug, el cual mostrará el número pensado
  202.          * @author [BORRADO]
  203.          */
  204.  
  205.         while (!Partida.Terminada) {
  206.             Partida = AdivinaMiNumero_Main(Partida);
  207.         }
  208.         System.out.println("Partidas ganadas: " + Partida.PartidasGanadas);
  209.     }
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement