Juego.java 11.8 KB
Newer Older
Jose Baez committed
1 2 3
import org.w3c.dom.ls.LSOutput;

import java.util.*;
4 5 6

public class Juego {
    List<Integer> valores = null;
7 8
    final int MAX=3;
    final int MIN=1;
9 10 11

    public Juego() {
    }
Jose Baez committed
12 13 14 15 16 17 18 19 20 21

    public int mano1VsMano2(int carta1, int carta2) {
        if (carta1 > carta2) {
            return 1;
        } else if (carta1 < carta2) {
            return 2;
        } else if (carta1 == carta2) {
            return 0;
        }
        return 0;
22
    }
Jose Baez committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

    public void desempateCarta(int cartas1, int cartas2) {
        if (cartas1 > cartas2) {
            System.out.println("1");
        }
        System.out.println("2");
    }

    public int sumaManoValores(Carta[] cartas) {
        List<Integer> valores = new ArrayList<>();
        int contador = 0;
        for (Carta c : cartas) {
            valores.add(convertirValorStringAInteger(c.getValor()));
            contador++;
        }
        return contador;
    }

41
    public int juegosPosibles(List<Carta> cartas) {
42 43 44 45
        boolean siguiente = true;
        if (escaleraColor(cartas)) {
            siguiente = false;
            System.out.println("ESCALERA COLOR");
46
            return 9;
47 48 49
        }
        if (siguiente && poker(cartas)) {
            siguiente = false;
50 51
            System.out.println("ESCALERA COLOR");
            return 8;
52 53 54
        }
        if (siguiente && full(cartas)) {
            siguiente = false;
Jose Baez committed
55
            System.out.println("FULL");
56
            return 7;
57 58 59 60
        }
        if (siguiente && escalera(cartas)) {
            siguiente = false;
            System.out.println("ESCALERA");
61
            return 6;
62 63 64 65
        }
        if (siguiente && color(cartas)) {
            siguiente = false;
            System.out.println("COLOR");
66
            return 5;
67 68 69 70
        }
        if (siguiente && trio(cartas)) {
            siguiente = false;
            System.out.println("TRIO");
71
            return 4;
72 73 74 75
        }
        if (siguiente && parDoble(cartas)) {
            siguiente = false;
            System.out.println("PAR DOBLE");
76
            return 3;
77 78 79 80
        }
        if (siguiente && par(cartas)) {
            siguiente = false;
            System.out.println("PAR");
81
            return 1;
82
        }
83 84
        System.out.println("CARTA ALTA");
        return 0;
85 86 87 88 89 90
    }

    public boolean par(List<Carta> cartas) {
        if (cantidadPalosIguales(cartas) < 3) {
            List<Integer> valores = new ArrayList<>();
            for (Carta c : cartas) {
91
                valores.add(convertirValorStringAInteger(c.getValor()));
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            }
            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) {
112
                valores.add(convertirValorStringAInteger(c.getValor()));
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
            }
            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) {
132
            valores.add(convertirValorStringAInteger(c.getValor()));
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
        }
        //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) {
163
                valores.add(convertirValorStringAInteger(c.getValor()));
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
            }
            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) {
189
            valores.add(convertirValorStringAInteger(c.getValor()));
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
        }
        Collections.sort(valores);
        int counter = 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) {
                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) {

219
            if (c.getPalo().equals("K")) {
220 221
                contador++;
            }
222
            if (c.getPalo().equals("Q")) {
223 224
                contador++;
            }
225
            if (c.getPalo().equals("K")) {
226 227
                contador++;
            }
228
            if (c.getPalo().equals("C")) {
229 230 231 232 233 234 235 236 237 238 239
                contador++;
            }
        }

        return contador;

    }

    public boolean color(List<Carta> cartas) {
        String valuePalo = estraerPrimerPalo(cartas);
        for (int i = 0; i < cartas.size(); i++) {
240
            if (!valuePalo.equals(cartas.get(i).getPalo())) {
241 242 243 244 245 246 247 248 249 250 251
                return false;
            }
        }
        return true;
    }

    public boolean escalera(List<Carta> cartas) {
        int contador = 0;
        List<Integer> valores = new ArrayList<>();

        for (Carta c : cartas) {
252
            valores.add(convertirValorStringAInteger(c.getValor()));
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
        }
        Collections.sort(valores);

        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++) {
277
            if (!valuePalo.equals(cartas.get(i).getPalo())) {
278 279 280 281 282 283 284
                valor1 = false;
            }
        }
        int contador = 0;
        List<Integer> valores = new ArrayList<>();
        if (valor1) {
            for (Carta c : cartas) {
285
                valores.add(convertirValorStringAInteger(c.getValor()));
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 317 318
            }
            Collections.sort(valores);

            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;
Jose Baez committed
319
        } else if (carta.equals("T")) {
320
            return valor = 10;
Jose Baez committed
321
        } else if (carta.equals("J")) {
322
            return valor = 11;
Jose Baez committed
323
        } else if (carta.equals("Q")) {
324
            return valor = 12;
Jose Baez committed
325
        } else if (carta.equals("K")) {
326 327 328 329 330 331 332 333
            return valor = 13;
        }
        return Integer.valueOf(carta);
    }

    public String estraerPrimerPalo(List<Carta> cartas) {
        String value = "";
        for (Carta c : cartas) {
334
            value = c.getPalo();
335 336 337 338 339
            break;
        }
        return value;
    }

Jose Baez committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    public int isRepetidos(Carta[] m1, Carta[] m2) {
        Carta[] m3 = new Carta[10];
        Set<Carta> cartas = new HashSet<>();
        int c = 5;
        for (int i = 0; i < m3.length; i++) {
            if (i < 5) {
                cartas.add(m1[i]);
            } else if (i > 4) {
                cartas.add(m2[i - c]);
            }
        }
        return cartas.size();

    }

    public Carta[] cambiarMano1(Carta[] m1) {
356 357
        int range = MAX - MIN + 1;
        int rand = (int) (Math.random() * range) + MIN;
Jose Baez committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        if (rand == 1) {
            return m1 = new Carta[]{
                    new Carta("AS"),
                    new Carta("2S"),
                    new Carta("4S"),
                    new Carta("6S"),
                    new Carta("8S")};
        } else if (rand == 2) {
            return m1 = new Carta[]{
                    new Carta("KH"),
                    new Carta("2H"),
                    new Carta("5C"),
                    new Carta("TS"),
                    new Carta("JS")};
        }else {
            return m1 = new Carta[]{
                    new Carta("JC"),
                    new Carta("2H"),
                    new Carta("5S"),
                    new Carta("TC"),
                    new Carta("JS")};
        }
    }

    public Carta[] cambiarMano2(Carta[] m2) {
383 384
        int range = MAX - MIN + 1;
        int rand = (int) (Math.random() * range) + MIN;
Jose Baez committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        if (rand == 1) {
            return m2 = new Carta[]{
                    new Carta("AK"),
                    new Carta("TC"),
                    new Carta("5H"),
                    new Carta("TH"),
                    new Carta("JH")};
        }else if(rand == 2){
            return m2 = new Carta[]{
                    new Carta("AC"),
                    new Carta("6C"),
                    new Carta("7H"),
                    new Carta("8H"),
                    new Carta("9H")};
        }else {
            return m2 = new Carta[]{
                    new Carta("TH"),
                    new Carta("9C"),
403
                    new Carta("5D"),
Jose Baez committed
404 405 406 407
                    new Carta("TH"),
                    new Carta("JH")};
        }
    }
408 409
}

Jose Baez committed
410