Juego.java 8.91 KB
Newer Older
1 2
import java.util.ArrayList;
import java.util.Collections;
Jose Baez committed
3 4 5
import java.util.List;

public class Juego {
6
    List<Integer> valores = null;
Jose Baez committed
7 8 9 10

    public Juego() {
    }

Jose Baez committed
11
    public void juegosPosibles(List<Carta> cartas) {
12
        boolean siguiente = true;
Jose Baez committed
13 14
        if (escaleraColor(cartas)) {
            siguiente = false;
15 16
            System.out.println("ESCALERA COLOR");
        }
Jose Baez committed
17 18 19 20
        if (siguiente && poker(cartas)) {
            siguiente = false;
            System.out.println("POKER");
        }
Jose Baez committed
21 22 23 24
        if (siguiente && full(cartas)) {
            siguiente = false;
            System.out.println("FULL");
        }
Jose Baez committed
25 26
        if (siguiente && escalera(cartas)) {
            siguiente = false;
27 28
            System.out.println("ESCALERA");
        }
Jose Baez committed
29 30
        if (siguiente && color(cartas)) {
            siguiente = false;
Jose Baez committed
31 32
            System.out.println("COLOR");
        }
Josebaezx committed
33 34 35 36
        if (siguiente && trio(cartas)) {
            siguiente = false;
            System.out.println("TRIO");
        }
Jose Baez committed
37 38 39 40
        if (siguiente && parDoble(cartas)) {
            siguiente = false;
            System.out.println("PAR DOBLE");
        }
Jose Baez committed
41 42 43 44
        if (siguiente && par(cartas)) {
            siguiente = false;
            System.out.println("PAR");
        }
Jose Baez committed
45
        if (siguiente) {
Jose Baez committed
46
            System.out.println("CARTA ALTA");
Jose Baez committed
47
        }
48
    }
Jose Baez committed
49

Jose Baez committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    public boolean par(List<Carta> cartas) {
        if(comprobarPalo(cartas)<3) {
            List<Integer> 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;
    }
Jose Baez committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    public boolean parDoble(List<Carta> cartas) {
        if(comprobarPalo(cartas)<3) {
            List<Integer> 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;
    }
Josebaezx committed
90 91 92 93 94 95
    public boolean trio(List<Carta> cartas) {
        List<Integer> valores = new ArrayList<>();
        for (Carta c : cartas) {
            valores.add(extraer(c.valor));
        }
        //Collections.sort(valores);
Jose Baez committed
96
        int[] numerosArray = pasarAArray(valores);
Josebaezx committed
97 98 99
        int counter=0;
        int cantidadNumero=0;
        for(int i=11; i<14; i++){
Jose Baez committed
100 101
            for (int j = 0; j < numerosArray.length - 1; j++) {
                if (numerosArray[j] == numerosArray[j + 1]) {
Josebaezx committed
102
                    counter++;
Jose Baez committed
103 104 105
                    if (counter == 2) {
                        return true;
                    }
Josebaezx committed
106 107 108
                }
            }
            if(counter > cantidadNumero){
Jose Baez committed
109

Josebaezx committed
110 111 112 113 114 115 116 117 118 119 120 121
                cantidadNumero=counter;
            }
            counter=0;
        }

        if (cantidadNumero==3){
            return true;
        }else {
            return false;
        }
    }

Jose Baez committed
122 123
    //Dos cartas iguales (mismo valor) junto con tres cartas iguales (mismo valor).
    public boolean full(List<Carta> cartas) {
124
        if(comprobarPalo(cartas)==3){
Jose Baez committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
            List<Integer> 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;
                    }
Jose Baez committed
140 141
                }
            }
Jose Baez committed
142 143 144
            return false;
        }else {
            return false;
Jose Baez committed
145 146
        }
    }
147

Jose Baez committed
148 149
    //Cuatro cartas iguales (mismo valor).
    public boolean poker(List<Carta> cartas) {
150
        if(comprobarPalo(cartas)==4){
Jose Baez committed
151
            List<Integer> valores = new ArrayList<>();
152
            comprobarPalo(cartas);
Jose Baez committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            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<valores.size(); j++) {
                    int s= valores.get(j);
                    if(s == i){
                        counter++;
                    }
                }
                if(counter > cantidadNumero){
                    numero=i;
                    cantidadNumero=counter;
Jose Baez committed
170
                }
Jose Baez committed
171
                counter=0;
Jose Baez committed
172
            }
Jose Baez committed
173 174 175 176
            if (cantidadNumero==4){
                return true;
            }else {
                return false;
Jose Baez committed
177
            }
Jose Baez committed
178

Jose Baez committed
179 180 181
        }else {
            return false;
        }
Jose Baez committed
182 183
    }

184
    private int comprobarPalo(List<Carta> cartas) {
Jose Baez committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        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;
Jose Baez committed
203 204 205

    }

Jose Baez committed
206 207 208 209 210 211 212 213 214
    public boolean color(List<Carta> cartas) {
        String valuePalo = estraerPalo(cartas);
        for (int i = 0; i < cartas.size(); i++) {
            if (!valuePalo.equals(cartas.get(i).palo)) {
                return false;
            }
        }
        return true;
    }
Jose Baez committed
215

216 217 218 219 220 221 222 223
    public boolean escalera(List<Carta> cartas) {
        int contador = 0;
        List<Integer> valores = new ArrayList<>();

        for (Carta c : cartas) {
            valores.add(extraer(c.valor));
        }
        Collections.sort(valores);
224
        //valores.forEach(System.out::println);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

        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;
            }
        }
Jose Baez committed
240
        return true;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    }

    public boolean escaleraColor(List<Carta> 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<Integer> valores = new ArrayList<>();
        if (valor1) {
            for (Carta c : cartas) {
                valores.add(extraer(c.valor));
            }
            Collections.sort(valores);
Jose Baez committed
258
            //valores.forEach(System.out::println);
259 260 261

            int[] valoresArry = pasarAArray(valores);
            for (int i = 0; i < valoresArry.length - 1; i++) {
Jose Baez committed
262

263 264 265 266 267 268 269 270 271 272 273
                if (valoresArry[i] > 1) {
                    if (valoresArry[i + 1] - valoresArry[i] != 1) {
                        return false;
                    }
                }

                contador += valoresArry[i];
                if (contador == 34 && valoresArry[0] == 1) {
                    return true;
                }
            }
Jose Baez committed
274
            return true;
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
        }

        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 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);
    }
Jose Baez committed
308

309 310 311 312 313 314 315
    public String estraerPalo(List<Carta> cartas) {
        String value = "";
        for (Carta c : cartas) {
            value = c.palo;
            break;
        }
        return value;
Jose Baez committed
316 317 318
    }

}