diff --git b/.idea/.gitignore a/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ a/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git b/.idea/misc.xml a/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ a/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git b/.idea/modules.xml a/.idea/modules.xml new file mode 100644 index 0000000..32185f1 --- /dev/null +++ a/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git b/java-e005-poker-02.iml a/java-e005-poker-02.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ a/java-e005-poker-02.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git b/out/production/java-e005-poker-02/Carta.class a/out/production/java-e005-poker-02/Carta.class new file mode 100644 index 0000000..68bc665 Binary files /dev/null and a/out/production/java-e005-poker-02/Carta.class differ diff --git b/out/production/java-e005-poker-02/Juego.class a/out/production/java-e005-poker-02/Juego.class new file mode 100644 index 0000000..3f93001 Binary files /dev/null and a/out/production/java-e005-poker-02/Juego.class differ diff --git b/out/production/java-e005-poker-02/MyClass.class a/out/production/java-e005-poker-02/MyClass.class new file mode 100644 index 0000000..48fefca Binary files /dev/null and a/out/production/java-e005-poker-02/MyClass.class differ diff --git b/src/Carta.java a/src/Carta.java new file mode 100644 index 0000000..e121c84 --- /dev/null +++ a/src/Carta.java @@ -0,0 +1,81 @@ +import java.util.ArrayList; +import java.util.List; + +public class Carta { + String valor; + String palo; + + public Carta() { + valorAleatorio(); + paloAleatorio(); + } + + public Carta(String completo) { + String a = String.valueOf(completo.charAt(0)); + String b = String.valueOf(completo.charAt(1)); + this.valor = a.toUpperCase(); + this.palo = b.toUpperCase(); + } + + String valorPalo() { + return this.valor + this.palo; + } + + public void valorAleatorio(){ + int max = 14; + int min = 1; + int range = max - min + 1; + int rand = (int)(Math.random() * range) + min; + for (int i = 0; i < 1; i++) { + if(rand==10){ + this.valor="T"; + continue; + } + + if(rand==11){ + this.valor="J"; + continue; + } + if(rand==12){ + this.valor="Q"; + continue; + } + if(rand==13){ + this.valor="K"; + continue; + } + if(rand==14){ + this.valor="A"; + }else { + this.valor=String.valueOf(rand); + } + } + + + + } + + public void paloAleatorio(){ + int max = 1; + int min = 4; + int range = max - min + 1; + int rand = (int)(Math.random() * range) + min; + for (int i = 0; i < 1; i++) { + if(rand==1){ + this.palo="C"; + continue; + } + if(rand==2){ + this.palo="H"; + continue; + } + if(rand==4){ + this.palo="D"; + }else { + this.palo="S"; + } + } + + } + +} diff --git b/src/Juego.java a/src/Juego.java new file mode 100644 index 0000000..fd5aa3e --- /dev/null +++ a/src/Juego.java @@ -0,0 +1,316 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Juego { + List valores = null; + + public Juego() { + } + + public void juegosPosibles(List cartas) { + boolean siguiente = true; + if (escaleraColor(cartas)) { + siguiente = false; + System.out.println("ESCALERA COLOR"); + } + if (siguiente && poker(cartas)) { + siguiente = false; + System.out.println("POKER"); + } + if (siguiente && full(cartas)) { + siguiente = false; + System.out.println("FULL"); + } + if (siguiente && escalera(cartas)) { + siguiente = false; + System.out.println("ESCALERA"); + } + if (siguiente && color(cartas)) { + siguiente = false; + System.out.println("COLOR"); + } + if (siguiente && trio(cartas)) { + siguiente = false; + System.out.println("TRIO"); + } + if (siguiente && parDoble(cartas)) { + siguiente = false; + System.out.println("PAR DOBLE"); + } + if (siguiente && par(cartas)) { + siguiente = false; + System.out.println("PAR"); + } + if (siguiente) { + System.out.println("CARTA ALTA"); + } + } + + public boolean par(List cartas) { + if (cantidadPalosIguales(cartas) < 3) { + List valores = new ArrayList<>(); + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + Collections.sort(valores); + int[] numerosArray = pasarAArray(valores); + int con = 0; + for (int i = 0; i < numerosArray.length - 1; i++) { + if (numerosArray[i] == numerosArray[i + 1]) { + con++; + if (con == 1) { + return true; + } + } + } + } + return false; + } + + public boolean parDoble(List cartas) { + if (cantidadPalosIguales(cartas) < 3) { + List valores = new ArrayList<>(); + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + Collections.sort(valores); + int[] numerosArray = pasarAArray(valores); + int con = 0; + for (int i = 0; i < numerosArray.length - 1; i++) { + if (numerosArray[i] == numerosArray[i + 1]) { + con++; + if (con == 2) { + return true; + } + } + } + } + return false; + } + + public boolean trio(List cartas) { + List valores = new ArrayList<>(); + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + //Collections.sort(valores); + int[] numerosArray = pasarAArray(valores); + int counter = 0; + int cantidadNumero = 0; + for (int i = 11; i < 14; i++) { + for (int j = 0; j < numerosArray.length - 1; j++) { + if (numerosArray[j] == numerosArray[j + 1]) { + counter++; + } + } + if (counter > cantidadNumero) { + + cantidadNumero = counter; + } + counter = 0; + } + + if (cantidadNumero == 3) { + return true; + } else { + return false; + } + } + + //Dos cartas iguales (mismo valor) junto con tres cartas iguales (mismo valor). + public boolean full(List cartas) { + if (cantidadPalosIguales(cartas) == 3) { + List valores = new ArrayList<>(); + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + Collections.sort(valores); + int[] numerosArray = pasarAArray(valores); + int con = 0; + for (int i = 0; i < numerosArray.length - 1; i++) { + if (numerosArray[i] == numerosArray[i + 1]) { + con++; + if (con == 3 && numerosArray[2] == numerosArray[4]) { + return true; + } else if (con == 3 && numerosArray[2] == numerosArray[0]) { + return true; + } + } + } + return false; + } else { + return false; + } + } + + //Cuatro cartas iguales (mismo valor). + public boolean poker(List cartas) { + + List valores = new ArrayList<>(); + cantidadPalosIguales(cartas); + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + Collections.sort(valores); + int counter = 0; + int numero = 0; + int cantidadNumero = 0; + for (int i = 1; i < 14; i++) { + for (int j = 0; j < valores.size(); j++) { + int s = valores.get(j); + if (s == i) { + counter++; + } + } + if (counter > cantidadNumero) { + numero = i; + cantidadNumero = counter; + } + counter = 0; + } + if (cantidadNumero == 4) { + return true; + } else { + return false; + } + + + } + + private int cantidadPalosIguales(List cartas) { + int contador = 0; + for (Carta c : cartas) { + + if (c.palo.equals("K")) { + contador++; + } + if (c.palo.equals("Q")) { + contador++; + } + if (c.palo.equals("K")) { + contador++; + } + if (c.palo.equals("C")) { + contador++; + } + } + + return contador; + + } + + public boolean color(List cartas) { + String valuePalo = estraerPrimerPalo(cartas); + for (int i = 0; i < cartas.size(); i++) { + if (!valuePalo.equals(cartas.get(i).palo)) { + return false; + } + } + return true; + } + + public boolean escalera(List cartas) { + int contador = 0; + List valores = new ArrayList<>(); + + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + Collections.sort(valores); + //valores.forEach(System.out::println); + + int[] valoresArry = pasarAArray(valores); + for (int i = 0; i < valoresArry.length - 1; i++) { + + if (valoresArry[i] > 1) { + if (valoresArry[i + 1] - valoresArry[i] != 1) { + return false; + } + } + + contador += valoresArry[i]; + if (contador == 34 && valoresArry[0] == 1) { + return true; + } + } + return true; + } + + public boolean escaleraColor(List cartas) { + boolean valor1 = true; + String valuePalo = estraerPrimerPalo(cartas); + for (int i = 0; i < cartas.size(); i++) { + if (!valuePalo.equals(cartas.get(i).palo)) { + valor1 = false; + } + } + int contador = 0; + List valores = new ArrayList<>(); + if (valor1) { + for (Carta c : cartas) { + valores.add(convertirValorStringAInteger(c.valor)); + } + Collections.sort(valores); + //valores.forEach(System.out::println); + + int[] valoresArry = pasarAArray(valores); + for (int i = 0; i < valoresArry.length - 1; i++) { + + if (valoresArry[i] > 1) { + if (valoresArry[i + 1] - valoresArry[i] != 1) { + return false; + } + } + + contador += valoresArry[i]; + if (contador == 34 && valoresArry[0] == 1) { + return true; + } + } + return true; + } + + return false; + + } + + private int[] pasarAArray(List valores) { + int[] value = new int[5]; + for (int i = 0; i < valores.size(); i++) { + value[i] = valores.get(i); + } + return value; + } + + public Integer convertirValorStringAInteger(String carta) { + int valor = 0; + if (carta.equals("A")) { + return valor = 1; + } + if (carta.equals("T")) { + return valor = 10; + } + if (carta.equals("J")) { + return valor = 11; + } + if (carta.equals("Q")) { + return valor = 12; + } + if (carta.equals("K")) { + return valor = 13; + } + return Integer.valueOf(carta); + } + + public String estraerPrimerPalo(List cartas) { + String value = ""; + for (Carta c : cartas) { + value = c.palo; + break; + } + return value; + } + +} + diff --git b/src/MyClass.java a/src/MyClass.java new file mode 100644 index 0000000..527870c --- /dev/null +++ a/src/MyClass.java @@ -0,0 +1,54 @@ +import java.util.ArrayList; +import java.util.List; + +public class MyClass { + // este es el único método que tienen que desarrollar + // para hacerlo, pueden hacer uso de otras clases (si es necesario), + // pero la corrección del ejercicio será automática en base a este + // método que está acá + public String ganadores(List jugadas) { + System.out.println("Cantidad de jugadas: " + jugadas.size()); + for (Carta[] mano : jugadas) { + System.out.println("JUGADA ======================"); + for (Carta c : mano) { + System.out.println(c.valorPalo()); + } + } + return "0"; + } + + public static void main(String args[]) { + + MyClass mc = new MyClass(); + List jugadas = new ArrayList(); +// Carta[] m1 = new Carta[5]; +// m1[0] = new Carta("AH"); +// m1[1] = new Carta("AD"); +// m1[2] = new Carta("TH"); +// m1[3] = new Carta("TC"); +// m1[4] = new Carta("6S"); +// Carta[] m2 = new Carta[5]; +// m2[0] = new Carta("AH"); +// m2[1] = new Carta("KD"); +// m2[2] = new Carta("QH"); +// m2[3] = new Carta("3C"); +// m2[4] = new Carta("3S"); + Carta[] m1 = new Carta[5]; + m1[0] = new Carta(); + m1[1] = new Carta(); + m1[2] = new Carta(); + m1[3] = new Carta(); + m1[4] = new Carta(); + Carta[] m2 = new Carta[5]; + m2[0] = new Carta(); + m2[1] = new Carta(); + m2[2] = new Carta(); + m2[3] = new Carta(); + m2[4] = new Carta(); + + jugadas.add(m1); + jugadas.add(m2); + String ganadores = mc.ganadores(jugadas); + System.out.println("Ganadores = " + ganadores); + } +} \ No newline at end of file