Last active
June 8, 2021 03:49
-
-
Save j2deme/a217b285f0fc79df8e2a35f6fb26fb71 to your computer and use it in GitHub Desktop.
Simulación de Dados y Monedas - Generación de números aleatorios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.j2deme; | |
public class ConsolaColor { | |
public static final String ANSI_RESET = "\u001B[0m"; | |
// Códigos ANSI para colores de letra | |
public static final String ANSI_BLACK = "\u001B[30m"; | |
public static final String ANSI_RED = "\u001B[31m"; | |
public static final String ANSI_GREEN = "\u001B[32m"; | |
public static final String ANSI_YELLOW = "\u001B[33m"; | |
public static final String ANSI_BLUE = "\u001B[34m"; | |
public static final String ANSI_PURPLE = "\u001B[35m"; | |
public static final String ANSI_CYAN = "\u001B[36m"; | |
public static final String ANSI_WHITE = "\u001B[37m"; | |
// Códigos ANSI para colores brillantes de letra | |
public static final String ANSI_BRIGHT_BLACK = "\u001B[90m"; | |
public static final String ANSI_BRIGHT_RED = "\u001B[91m"; | |
public static final String ANSI_BRIGHT_GREEN = "\u001B[92m"; | |
public static final String ANSI_BRIGHT_YELLOW = "\u001B[93m"; | |
public static final String ANSI_BRIGHT_BLUE = "\u001B[94m"; | |
public static final String ANSI_BRIGHT_PURPLE = "\u001B[95m"; | |
public static final String ANSI_BRIGHT_CYAN = "\u001B[96m"; | |
public static final String ANSI_BRIGHT_WHITE = "\u001B[97m"; | |
// Códigos ANSI para colores de fondo | |
public static final String ANSI_BG_BLACK = "\u001B[40m"; | |
public static final String ANSI_BG_RED = "\u001B[41m"; | |
public static final String ANSI_BG_GREEN = "\u001B[42m"; | |
public static final String ANSI_BG_YELLOW = "\u001B[43m"; | |
public static final String ANSI_BG_BLUE = "\u001B[44m"; | |
public static final String ANSI_BG_PURPLE = "\u001B[45m"; | |
public static final String ANSI_BG_CYAN = "\u001B[46m"; | |
public static final String ANSI_BG_WHITE = "\u001B[47m"; | |
// Códigos ANSI para colores brillantes de fondo | |
public static final String ANSI_BRIGHT_BG_BLACK = "\u001B[100m"; | |
public static final String ANSI_BRIGHT_BG_RED = "\u001B[101m"; | |
public static final String ANSI_BRIGHT_BG_GREEN = "\u001B[102m"; | |
public static final String ANSI_BRIGHT_BG_YELLOW = "\u001B[103m"; | |
public static final String ANSI_BRIGHT_BG_BLUE = "\u001B[104m"; | |
public static final String ANSI_BRIGHT_BG_PURPLE = "\u001B[105m"; | |
public static final String ANSI_BRIGHT_BG_CYAN = "\u001B[106m"; | |
public static final String ANSI_BRIGHT_BG_WHITE = "\u001B[107m"; | |
/** COLORES DE TEXTO **/ | |
String azul(String texto){ | |
return ANSI_BLUE + texto + resetearColor(); | |
} | |
String verde(String texto){ | |
return ANSI_GREEN + texto + resetearColor(); | |
} | |
String rojo(String texto){ | |
return ANSI_RED + texto + resetearColor(); | |
} | |
String amarillo(String texto){ | |
return ANSI_YELLOW + texto + resetearColor(); | |
} | |
String morado(String texto){ | |
return ANSI_PURPLE + texto + resetearColor(); | |
} | |
String cyan(String texto){ | |
return ANSI_CYAN + texto + resetearColor(); | |
} | |
String blanco(String texto){ | |
return ANSI_WHITE + texto + resetearColor(); | |
} | |
String negro(String texto){ | |
return ANSI_BLACK + texto + resetearColor(); | |
} | |
/** COLORES DE FONDO **/ | |
String fondoNegro(String texto){ | |
return ANSI_BRIGHT_BG_BLACK + texto + resetearColor(); | |
} | |
String fondoBlanco(String texto){ | |
return ANSI_BRIGHT_BG_WHITE + texto + resetearColor(); | |
} | |
/** REINICIAR COLOR **/ | |
String resetearColor(){ | |
return ANSI_RESET; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.j2deme; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
imprimirMenu(); | |
} | |
static void imprimirMenu(){ | |
int opcion; | |
do { | |
System.out.println("Elige tu opción"); | |
System.out.println("1. Lanzar moneda"); | |
System.out.println("2. Lanzar dado"); | |
System.out.println("3. Salir"); | |
opcion = teclado().nextInt(); | |
switch (opcion){ | |
case 1: | |
// Operador ternario (logica) ? true : false ; | |
System.out.print("La moneda cayo en "); | |
System.out.println((lanzarMoneda()) ? cc().verde("Cara") | |
: cc().verde("Cruz")); | |
break; | |
case 2: | |
imprimirMenuDados(); | |
break; | |
case 3: | |
// Salir | |
break; | |
default: | |
System.err.println("Opción no válida"); | |
} | |
} while(opcion != 3); | |
} | |
static void imprimirMenuDados(){ | |
int numDados = 1; | |
int sumDados = 0; | |
System.out.print("¿Cuántos dados desea lanzar? "); | |
numDados = teclado().nextInt(); | |
for (int i = 0; i < numDados; i++){ | |
//sumDados = sumDados + lanzarDado(); | |
sumDados += lanzarDado(); | |
} | |
System.out.println("Resultado de Dado(s): " + | |
cc().azul("" + sumDados) + "."); | |
} | |
static boolean lanzarMoneda(){ | |
double randomNumber = Math.random(); | |
/* | |
* El if-else se puede simplificar usando: | |
* return randomNumber < 0.5 | |
*/ | |
if(randomNumber < 0.5){ | |
return true; // Cara | |
} else { | |
return false; // Cruz | |
} | |
} | |
static int lanzarDado(){ | |
/* | |
* La función nextInt(número Entero) devuelve como resultado un número | |
* entero entre 0 y el número que recibe como párametro (sin incluirlo) | |
* es importante notar que si se quiere cambiar el conteo cero basado a | |
* conteo natural (iniciando en 1) se debe sumar 1 al resultado de la | |
* función | |
*/ | |
Random random = new Random(); | |
return random.nextInt(6) +1 ; | |
} | |
// Devuelve un "Scanner" para leer desde el teclado | |
static Scanner teclado(){ | |
return new Scanner(System.in); | |
} | |
// Devuelve un "acceso" a las funciones del archivo ConsolaColor | |
static ConsolaColor cc(){ | |
return new ConsolaColor(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment