Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Scanner;
- public class borrado {
- /**
- * Lista de cosas nuevas:
- * -Enemigos, cada uno con su dificultad unica.
- * -Modo debug, para probar el juego
- * -Partida guardada
- *
- * @author [BORRADO]
- */
- public static class save {
- /**
- * Clase utilizada para administracion de la partida,
- * Partidas(Int)= Partidas en total
- * PartidasGanadas = Partidas que se han ganado
- * Terminada(bool) = Indica si la partida se ha acabado, si es true para el
- * bucle
- * ModoDebug(bool) = Muestra el número pensado, pensado para pruebas
- *
- * @author [BORRADO]
- */
- int Partidas = 0;
- int PartidasGanadas = 0;
- enemigo EnemigoActual = new enemigo();
- boolean Terminada = false;
- boolean ModoDebug = false; // Muestra el numero pensado, intencionado para probar que el programa funciona
- // bien
- }
- public static class enemigo {
- /**
- *
- */
- String Sprite = "slime";
- int Dificultad = 0;
- }
- public static int IntAleatorio(int Max, int Min) {
- /**
- * Devuelve un numero aleatorio entre Max y Min
- *
- * @param Max el numero máximo del generador
- * @param Min el numero mínimo del generador
- * @return Int generado
- * @author [BORRADO]
- *
- */
- int numero = (int) (Math.random() * Max + Min);
- return numero;
- }
- public static String LeerSprite(String path) throws IOException {
- byte[] Archivo = Files.readAllBytes(Paths.get(path));
- String Sprite = new String(Archivo).toString();
- /*
- * TODO:
- * -Opcion para dibujar sprites sin color, el color viene desactivado
- * por defecto en windows y queda feo
- */
- // Sprite = Sprite.replaceAll("W", "[47m [0m");
- // Sprite = Sprite.replaceAll("B", "[42m [0m");
- Sprite = Sprite.replaceAll("W", "█");
- Sprite = Sprite.replaceAll("B", "░");
- /**
- * Lee un archivo de la carpeta Sprite, para generar imagenes desde la linea de
- * comandos.
- * Reemplaza W con █, y B con ░
- *
- * @param path Directorio y archivo de texto donde está ("a/b.txt")
- * @return String un String del sprite
- * @throws IOException en el caso de que no se encuentre el archivo
- * @author [BORRADO]
- *
- */
- return Sprite;
- }
- public static save AdivinaMiNumero_Main(save Partida) throws IOException {
- /**
- * \brief Funcion principal del juego
- *
- * @author [BORRADO]
- */
- enemigo Slime = new enemigo();
- enemigo Gusano = new enemigo();
- enemigo Gato = new enemigo();
- enemigo Fant = new enemigo();
- Slime.Sprite = "Slime.txt";
- Slime.Dificultad = 1;
- Gusano.Sprite = "worm.txt";
- Gusano.Dificultad = 3;
- Gato.Sprite = "cat.txt";
- Gato.Dificultad = 2;
- Fant.Sprite = "bigbad.txt";
- Fant.Dificultad = 4;
- enemigo Enemigos[] = { Slime, Gusano, Gato, Fant };
- enemigo EnemigoElegido = Enemigos[IntAleatorio(Enemigos.length, 0)];
- System.out.println("");
- Scanner sc = new Scanner(System.in);
- String Sprite = LeerSprite("./sprites/" + EnemigoElegido.Sprite);
- int NumeroMaximo = 100 * EnemigoElegido.Dificultad;
- boolean Fallado = false;
- boolean Preguntar = true;
- boolean Valido = true; // Si no se ha introducido algo valido no sumar, este bool se asegura de esto
- int NumeroPensado = IntAleatorio(NumeroMaximo, 1);
- int NumeroIntento = 0;
- System.out.println("Pienso un numero entre 1 y " + NumeroMaximo + "(ambos inclusive)");
- System.out.println(Sprite);
- if (Partida.ModoDebug) {
- System.out.println("Ya que estas probando el programa, puedo decirte que estoy pensando en el numero "
- + NumeroPensado + ".");
- }
- if (Partida.Partidas == 0) {
- // System.out.println("Pienso un numero entre 1 y "+NumeroMaximo+"(ambos
- // inclusive)");
- Partida.EnemigoActual = EnemigoElegido;
- System.out.println("Trata de adivinarlo. Tienes 10 intentos: ");
- System.out.println("(introduce 0 si te cansas)");
- }
- String NumeroMYoMN = "";
- System.out.print("Adivina mi numero: ");
- NumeroIntento = sc.nextInt();
- if (NumeroIntento < 0 || NumeroIntento > 10000) {
- System.out.println("El numero debe estar comprendido entre 1 y 10000");
- Valido = false;
- } else if (Partida.Partidas >= 10) {
- System.out.println("Ya has tenido tus 10 intentos y no has acertado");
- System.out.println("Adios");
- Preguntar = false;
- Partida.Terminada = true;
- }
- if (NumeroIntento == NumeroPensado) {
- System.out.println("Has acertado!!! Solo has necesitado " + Partida.Partidas + " intentos.");
- // System.out.println("Adios");
- Partida.Terminada = true;
- Partida.Partidas++;
- Partida.PartidasGanadas++;
- } else if (NumeroIntento > NumeroPensado) {
- NumeroMYoMN = "menor";
- Fallado = true;
- } else if (NumeroIntento < NumeroPensado) {
- NumeroMYoMN = "mayor";
- Fallado = true;
- }
- if (NumeroIntento == 0) {
- Preguntar = false;
- Fallado = false;
- Partida.Terminada = true;
- System.out.println("Te has cansado");
- System.out.println("Adios");
- }
- if (Valido) {
- Partida.Partidas++;
- }
- if (Fallado && !Partida.Terminada) {
- System.out.println("Mi numero es " + NumeroMYoMN + ". Llevas " + Partida.Partidas + " Intentos");
- } else {
- Partida.Partidas = 0;
- }
- if (Preguntar) {
- String Respuesta = "";
- System.out.print("Otra partidita? [Si/No]: ");
- Respuesta = sc.next();
- if (Respuesta.toLowerCase().equals("no")) {
- System.out.println("Adios");
- Partida.Terminada = true;
- sc.close();
- } else {
- Partida.Terminada = false;
- }
- }
- System.out.println("============");
- return Partida;
- }
- public static void main(String[] args) throws IOException {
- save Partida = new save();
- Partida.Partidas = 0;
- Partida.PartidasGanadas = 0;
- if (args.length > 0) {
- if (args[0].matches("-debug")) {
- Partida.ModoDebug = true;
- }
- }
- /**
- * Funcion principal del programa
- *
- * @param -debug Activa el modo debug, el cual mostrará el número pensado
- * @author [BORRADO]
- */
- while (!Partida.Terminada) {
- Partida = AdivinaMiNumero_Main(Partida);
- }
- System.out.println("Partidas ganadas: " + Partida.PartidasGanadas);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement