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(comprobarPalo(cartas)<3) { List valores = new ArrayList<>(); for (Carta c : cartas) { valores.add(extraer(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(comprobarPalo(cartas)<3) { List valores = new ArrayList<>(); for (Carta c : cartas) { valores.add(extraer(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(extraer(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 == 2) { return true; } } } 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(comprobarPalo(cartas)==3){ List valores = new ArrayList<>(); for (Carta c : cartas) { valores.add(extraer(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) { if(comprobarPalo(cartas)==4){ List valores = new ArrayList<>(); comprobarPalo(cartas); for (Carta c : cartas) { valores.add(extraer(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 cantidadNumero){ numero=i; cantidadNumero=counter; } counter=0; } if (cantidadNumero==4){ return true; }else { return false; } }else { return false; } } private int comprobarPalo(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 = estraerPalo(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(extraer(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 = estraerPalo(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(extraer(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 extraer(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 estraerPalo(List cartas) { String value = ""; for (Carta c : cartas) { value = c.palo; break; } return value; } }