Juego.java 8.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Juego {
    List<Integer> valores = null;

    public Juego() {
    }

    public void juegosPosibles(List<Carta> 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<Carta> cartas) {
        if (cantidadPalosIguales(cartas) < 3) {
            List<Integer> 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<Carta> cartas) {
        if (cantidadPalosIguales(cartas) < 3) {
            List<Integer> 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<Carta> cartas) {
        List<Integer> 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<Carta> cartas) {
        if (cantidadPalosIguales(cartas) == 3) {
            List<Integer> 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<Carta> cartas) {

        List<Integer> 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<Carta> 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<Carta> 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<Carta> cartas) {
        int contador = 0;
        List<Integer> 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<Carta> 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<Integer> 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<Integer> 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<Carta> cartas) {
        String value = "";
        for (Carta c : cartas) {
            value = c.palo;
            break;
        }
        return value;
    }

}