Skip to content

Commit

Permalink
Merge pull request mouredev#4868 from jorgenavarroenamoradotokio/main
Browse files Browse the repository at this point in the history
Reto #[2] - [Java] Reto #[3] - [Java]  Reto #[4] - [Java]   Reto #[6] - [Java]
Roswell468 authored Sep 4, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 0c941da + 5530f21 commit ed8d4bf
Showing 4 changed files with 354 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.retos.ej02;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/*
* Escribe un programa que muestre cómo transcurre un juego de tenis y quién lo ha ganado.
* El programa recibirá una secuencia formada por "P1" (Player 1) o "P2" (Player 2), según quien
* gane cada punto del juego.
*
* - Las puntuaciones de un juego son "Love" (cero), 15, 30, 40, "Deuce" (empate), ventaja.
* - Ante la secuencia [P1, P1, P2, P2, P1, P2, P1, P1], el programa mostraría lo siguiente:
* 15 - Love
* 30 - Love
* 30 - 15
* 30 - 30
* 40 - 30
* Deuce
* Ventaja P1
* Ha ganado el P1
* - Si quieres, puedes controlar errores en la entrada de datos.
* - Consulta las reglas del juego si tienes dudas sobre el sistema de puntos.
*/

public class jorgenavarroenamoradotokio {

private static final Map<Integer, String> MAP_PUNTUACIONES_PARTIDA = new HashMap<>();
private static final String DEUCE = "Deuce";
private static final String VENTAJA = "Ventaja";

static {
MAP_PUNTUACIONES_PARTIDA.put(0, "Love");
MAP_PUNTUACIONES_PARTIDA.put(1, "15");
MAP_PUNTUACIONES_PARTIDA.put(2, "30");
MAP_PUNTUACIONES_PARTIDA.put(3, "40");
}

// Clase interna encargada de almacenar la informacion de un jugador
public class Jugador{

private String nombre;
private int puntuacion;
private boolean advance;
private boolean ganador;

public Jugador(String nombre) {
this.nombre = nombre;
this.puntuacion = 0;
this.advance = false;
this.ganador = false;
}

public void addPunto() {
puntuacion += 1;
if (advance && puntuacion == 5)
ganador = true;
else if (!advance && puntuacion == 4)
ganador = true;
}

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public int getPuntuacion() {
return puntuacion;
}

public boolean isAdvance() {
return advance;
}

public void setAdvance(boolean advance) {
this.advance = advance;
}

public boolean isGanador() {
return ganador;
}
}

// Clase interna encargada de la logica del partido
public class Partido {
private Jugador p1;
private Jugador p2;

public void inscribirJugadores(Jugador p1, Jugador p2) {
this.p1 = p1;
this.p2 = p2;
}

public void enfrentamiento() {
do {
int numP1 = (int) (Math.random() * 5) + 1;;
int numP2 = (int) (Math.random() * 5) + 1;;

if (numP1 > numP2)
p1.addPunto();
else
p2.addPunto();

if (isDeuce())
System.out.println(DEUCE);
else if (isAdvantagePlayer(p1, p2) || isAdvantagePlayer(p2, p1))
System.out.println(VENTAJA);
else if (!p1.isGanador() && !p2.isGanador())
System.out.println(MAP_PUNTUACIONES_PARTIDA.get(p1.getPuntuacion()) + " - " + MAP_PUNTUACIONES_PARTIDA.get(p2.getPuntuacion()));
} while (!p1.isGanador() && !p2.isGanador());

System.out.println("Ha ganado el jugador " + (p1.isGanador() ? p1.getNombre() : p2.getNombre()));
}

private boolean isDeuce() {
return p1.getPuntuacion() >= 4 && p1.getPuntuacion() == p2.getPuntuacion();
}

private boolean isAdvantagePlayer(Jugador consulta, Jugador rival) {
boolean advance = (consulta.getPuntuacion() > 4) && (consulta.getPuntuacion() - rival.getPuntuacion() == 1);
consulta.setAdvance(advance);
return advance;
}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Desea lanzar una prueba: \n 1: dinamica \n 2: fija");
String opcion = sc.nextLine();
sc.close();

if ("1".equals(opcion)) {
jorgenavarroenamoradotokio tenis = new jorgenavarroenamoradotokio();
jorgenavarroenamoradotokio.Jugador p1 = tenis.new Jugador("P1");
jorgenavarroenamoradotokio.Jugador p2 = tenis.new Jugador("P2");

jorgenavarroenamoradotokio.Partido partido = tenis.new Partido();
partido.inscribirJugadores(p1, p2);
partido.enfrentamiento();

} else if ("2".endsWith(opcion)) {
String[] jugadas = { "P1", "P1", "P2", "P2", "P1", "P2", "P1", "P1" };
int puntosGanadosP1 = 0, puntosGanadosP2 = 0;

for (String jugada : jugadas) {
if ("P1".equals(jugada))
puntosGanadosP1 += 1;
else if ("P2".equals(jugada))
puntosGanadosP2 += 1;

if (puntosGanadosP1 == 3 && (puntosGanadosP1 == puntosGanadosP2))
System.out.println(DEUCE);
else if ((puntosGanadosP1 == 4 && puntosGanadosP2 == 3) || (puntosGanadosP2 == 4 && puntosGanadosP1 == 3))
System.out.println(VENTAJA);
else if ((puntosGanadosP1 == 4 || puntosGanadosP1 == 5) && puntosGanadosP2 == 3)
System.out.println("Ha ganado el P1");
else if ((puntosGanadosP2 == 4 || puntosGanadosP2 == 5) && puntosGanadosP1 == 3)
System.out.println("Ha ganado el P2");
else
System.out.println(MAP_PUNTUACIONES_PARTIDA.get(puntosGanadosP1) + " - " + MAP_PUNTUACIONES_PARTIDA.get(puntosGanadosP2));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.retos.ej03;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/*
* Escribe un programa que sea capaz de generar contraseñas de forma aleatoria.
* Podrás configurar generar contraseñas con los siguientes parámetros:
* - Longitud: Entre 8 y 16.
* - Con o sin letras mayúsculas.
* - Con o sin números.
* - Con o sin símbolos.
* (Pudiendo combinar todos estos parámetros entre ellos)
*/

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {

int longitudPassword = 15;
boolean mayus = true;
boolean numeros = true;
boolean simbolos = true;

// Registramos el codigo ascii de los caracteres del alfabeto en minusculas
List<Integer> caracteres = IntStream.rangeClosed(97, 122).boxed().collect(Collectors.toList());

// Registramos el codigo ascii de los caracteres del alfabeto en mayusculas
if (mayus)
caracteres.addAll(IntStream.rangeClosed(65, 90).boxed().collect(Collectors.toList()));

// Registramos el codigo ascii de los numeros
if (numeros) {
caracteres.addAll(IntStream.rangeClosed(48, 57).boxed().collect(Collectors.toList()));
}

// Registramos el codigo ascii de los caracteres
if (simbolos) {
caracteres.addAll(IntStream.rangeClosed(33, 47).boxed().collect(Collectors.toList()));
caracteres.addAll(IntStream.rangeClosed(58, 64).boxed().collect(Collectors.toList()));
caracteres.addAll(IntStream.rangeClosed(91, 96).boxed().collect(Collectors.toList()));
}

// Ajustamos la longitud de la password
if (longitudPassword < 8)
longitudPassword = 8;
else if (longitudPassword > 16)
longitudPassword = 16;


String password = "";
while (password.length() < longitudPassword) {
int caracterSeleccionado = (int) (Math.random() *caracteres.size()) + 1;
password += (char) (int) caracteres.get(caracterSeleccionado);
}

System.out.println(password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.retos.ej04;

import java.util.stream.IntStream;

public class jorgenavarroenamoradotokio {

/*
* Escribe un programa que, dado un número, compruebe y muestre si es primo, fibonacci y par.
* Ejemplos:
* - Con el número 2, nos dirá: "2 es primo, fibonacci y es par"
* - Con el número 7, nos dirá: "7 es primo, no es fibonacci y es impar"
*/

public static void main(String[] args) {

int num1 = 2;
int num2 = 21;

System.out.println("El numero " + num1 + " es Primo " + esNumeroPrimo(num1) + " es fibonacci " + esFibonacci(num1) + " es par " + esPar(num1));
System.out.println("El numero " + num2 + " es Primo " + esNumeroPrimo(num2) + " es fibonacci " + esFibonacci(num2) + " es par " + esPar(num2));
}

private static boolean esNumeroPrimo(int numero) {
if (numero <= 1) {
return false;
}

// Utilizamos la función noneMatch para verificar si no hay divisores entre 2 y la raíz cuadrada del número
return IntStream.rangeClosed(2, (int) Math.sqrt(numero)).noneMatch(i -> numero % i == 0);
}

private static boolean esFibonacci(int numero) {
if (numero < 0)
return false;

int a = 0;
int b = 1;

while (b < numero) {
int siguiente = a + b;
a = b;
b = siguiente;

if (b == numero) {
return true;
}
}
return b == numero;
}

private static boolean esPar(int numero) {
return numero % 2 == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.retos.ej06;

public class jorgenavarroenamoradotokio {

private static final String PIEDRA = "PIEDRA";
private static final String PAPEL = "PAPEL";
private static final String TIJERA = "TIJERA";
private static final String LAGARTO = "LAGARTO";
private static final String SPOCK = "SPOCK";

public static void main(String[] args) {

String elementoJugador1 = PIEDRA, elementoJugador2 = PIEDRA;

int resultado = switch (elementoJugador1) {
case PIEDRA -> isGanaRoca(elementoJugador2);
case PAPEL -> isGanaPapel(elementoJugador2);
case TIJERA -> isGanaTijeras(elementoJugador2);
case LAGARTO -> isGanaLagarto(elementoJugador2);
case SPOCK -> isGanaSpock(elementoJugador2);
default -> throw new IllegalArgumentException("Unexpected value: " + elementoJugador1);
};

if (resultado < 0)
System.out.println("El jugador 2 ha ganado");
else if (resultado > 0)
System.out.println("El jugador 1 ha ganado");
else
System.out.println("Empate");
}

private static int isGanaRoca(String opcionCombatir) {
if (LAGARTO.equals(opcionCombatir) || TIJERA.equals(opcionCombatir))
return 1;
if (PIEDRA.equals(opcionCombatir))
return 0;
return -1;
}

private static int isGanaPapel(String opcionCombatir) {
if (PIEDRA.equals(opcionCombatir) || SPOCK.equals(opcionCombatir))
return 1;
if (PAPEL.equals(opcionCombatir))
return 0;
return -1;
}

private static int isGanaTijeras(String opcionCombatir) {
if (LAGARTO.equals(opcionCombatir) || PAPEL.equals(opcionCombatir))
return 1;
if (TIJERA.equals(opcionCombatir))
return 0;
return -1;
}

private static int isGanaLagarto(String opcionCombatir) {
if (SPOCK.equals(opcionCombatir) || PAPEL.equals(opcionCombatir))
return 1;
if (LAGARTO.equals(opcionCombatir))
return 0;
return -1;
}

private static int isGanaSpock(String opcionCombatir) {
if (LAGARTO.equals(opcionCombatir) || PAPEL.equals(opcionCombatir))
return 1;
if (SPOCK.equals(opcionCombatir))
return 0;
return -1;
}
}

0 comments on commit ed8d4bf

Please sign in to comment.