JuegoPoker.java 8.94 KB
Newer Older
Javier Ferreira committed
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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package juegopoker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author user
 */
public class JuegoPoker {
    
    private Cartas[] mano;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("POKER PLAY\nSe genera la sogiente mano usuario" +
                "\n\tObtuviste las siguietnes combinaciones: ");
        JuegoPoker pokerJuego = new JuegoPoker();
        pokerJuego.cartaAlta();
        pokerJuego.doble();
        pokerJuego.dobleDoble();
        pokerJuego.trio();
        pokerJuego.escalera();
        pokerJuego.color();
        pokerJuego.full();
        pokerJuego.poker();
        pokerJuego.escaleraColor();
}
    
    
     public JuegoPoker(){
        this.mano = this.repartirCincoCartas();
        this.mano = new Cartas[]{
              new Cartas("H", "A", 14),
              new Cartas("H", "2", 2),
                new Cartas("H", "3", 3),
                new Cartas("H", "4", 4),
              new Cartas("H", "5", 5)};
        Arrays.sort(mano, Comparator.comparing(Cartas::getNumericValue).reversed());
        if(mano[1].getValor().equals("2") && mano[4].getValor().equals("A")){
            Cartas temp = mano[0];
            mano[0] = mano[4];
            mano[4] = temp;
        }
    }

    public Cartas[] repartirCincoCartas(){
        ArrayList<Cartas> mazo = Cartas.crearMazo();
        Cartas[] mano = new Cartas[5];
        int[] choices = new int[]{-1, -1, -1, -1, -1};
        boolean repeatedChoice = false;
        Scanner in = new Scanner(System.in);
        int i=0;
        for(i=0;i<5;i++){
            repeatedChoice = false;
            int choice = (int)(Math.random()*(52));
            for(int temp: choices){
                if(temp == choice){
                    i--;
                    repeatedChoice = true;
                }
            }
            if(repeatedChoice) continue;
            choices[i] = choice;
            mano[i] = mazo.get(choice);
        }
        Arrays.sort(mano, Comparator.comparing(Cartas::getNumericValue).reversed());
        if(mano[1].getValor().equals("2") && mano[4].getValor().equals("A")){
            Cartas temp;
            temp = mano[0];
            mano[0] = mano[4];
            mano[4] = temp;
        }
        return mano;
    }

    public void cartaAlta(){
        System.out.print("\nCarta Alta: *");
        for(Cartas carta: mano){
            System.out.print("|"+carta.getValor()+""+carta.getPalo()+"|-");
        }
    }
 public void dobleDoble(){
        String[] act = new String[5];
        String firstDoble = null;
        boolean exist = false;
        int dobleCount = 0;
        for(int i=0;i<mano.length-1;i++){
            if(mano[i].getValor().equals(mano[i + 1].getValor())){
                if(firstDoble==null){
                    act[i] = "*";
                    act[i+1] = "*";
                    firstDoble = mano[i].getValor();
                }else if(!mano[i].getValor().equals(firstDoble)){
                    act[i] = "*";
                    act[i+1] = "*";
                    exist = true;
                }
            }
        }
        if(exist){
            System.out.print("\nDoble Doble: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }
    public void doble(){
        String[] act = new String[5];
        boolean exist = false;
        for(int i=0;i<mano.length-1;i++){
            if(mano[i].getValor().equals(mano[i + 1].getValor())){
                act[i] = "*";
                act[i+1] = "*";
                exist = true;
                break;
            }
        }
        if(exist){
            System.out.print("\nDoble: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

   

    public void trio(){
        String[] act = new String[5];
        boolean exist = false;
        for(int i=0;i<mano.length-2;i++){
            if(mano[i].getValor().equals(mano[i + 1].getValor()) && mano[i].getValor().equals(mano[i+2].getValor())){
                act[i] = "*";
                act[i+1] = "*";
                act[i+2] = "*";
                exist = true;
                break;
            }
        }
        if(exist){
            System.out.print("\nTrio: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

    public void escalera(){
        String[] act = new String[]{"*","*","*","*","*"};
        boolean exist = true;
        for(int i=0;i<mano.length-1;i++){
            if(mano[i].getValor().equals(mano[i + 1].getValor()) ||
                    mano[i].getNumericValue() - mano[i+1].getNumericValue() > 1){
                exist = false;
                break;
            }
        }
        if(exist){
            System.out.print("\nEscalera: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

    public void color(){
        int paloCounterA = 1;
        String[] act = new String[5];
        int start;
        if(mano[0].getPalo().equals(mano[1].getPalo()) ||
                mano[1].getPalo().equals(mano[2].getPalo())){
            start=0;
        }else{
            start=1;
        }
        act[start]="*";
        for(int i=start+1;i<mano.length;i++){
            if(mano[start].getPalo().equals(mano[i].getPalo())) {
                paloCounterA++;
                act[i] = "*";
            }
        }
        if(paloCounterA == 4){
            System.out.print("\nColor: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

    public void full(){
        String[] act = new String[]{"*","*","*","*","*"};
        boolean exist = false;
        if(mano[0].getValor().equals(mano[1].getValor()) &&
                mano[0].getValor().equals(mano[2].getValor()) &&
                mano[3].getValor().equals(mano[4].getValor())){
            exist = true;
        }else if(mano[2].getValor().equals(mano[3].getValor()) &&
                mano[2].getValor().equals(mano[4].getValor()) &&
                mano[0].getValor().equals(mano[1].getValor())){
            exist = true;
        }
        if(exist){
            System.out.print("\nFull House: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

    public void poker(){
        String[] act;
        boolean exist = false;
        if(mano[0].getValor().equals(mano[1].getValor()) &&
                mano[0].getValor().equals(mano[2].getValor()) &&
                mano[0].getValor().equals(mano[3].getValor())){
            exist = true;
            act = new String[]{"*","*","*","*",null};
        }else if(mano[1].getValor().equals(mano[2].getValor()) &&
                mano[1].getValor().equals(mano[3].getValor()) &&
                mano[1].getValor().equals(mano[4].getValor())){
            exist = true;
            act = new String[]{null, "*","*","*","*"};
        }else{
            act = new String[5];
        }
        if(exist){
            System.out.print("\nPoker: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

    public void escaleraColor(){
        String[] act = new String[]{"*","*","*","*","*"};
        boolean exist = true;
        for(int i=0;i<mano.length-1;i++){
            if(mano[i].getValor().equals(mano[i + 1].getValor()) ||
                    mano[i].getNumericValue() - mano[i+1].getNumericValue() > 1 ||
                    !(mano[0].getPalo().equals(mano[i + 1].getPalo()))){
                exist = false;
                break;
            }
        }
        if(exist){
            System.out.print("\nEscalera Color: ");
            for(int i=0;i<mano.length;i++){
                if(act[i]!=null) System.out.print(act[i]);
                System.out.print("|"+mano[i].getValor()+""+mano[i].getPalo()+"|-");
            }
        }
    }

    public Cartas[] getMano() {
        return mano;
    }

}