Toros_Vacas.java 4.24 KB
Newer Older
willgonzz 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
import java.util.Scanner;

public class Toros_Vacas{
    public static void main(String[] args) {
        String NumA;
        String NumB;
        int Vacas=0;
        int Toros=0;
        String [] NumbIntentados=new String[12];
        Scanner scanner = new Scanner(System.in);
        System.out.println("Favor ingrese el Numero del Jugador A: ");
        NumA=scanner.nextLine();
        char Recorrido;int i;
        for(i=0;i<12;i++){
            System.out.print("Intento B "+(i+1)+": ");
            NumB=scanner.nextLine();
            //validados que el formato sea correcto
            for(;NumB.length()!=4;){
                System.out.println("Jugada Invalida: Jugada con Formato Incorrecto");
                System.out.println("favor Ingrese una jugada valida: ");
                NumB=scanner.nextLine();
            }
            //validados que los digitos no sean repetidos y volvemos a mirar el formato
            for(int j=0; j<3;j++){
                for(int k=j+1;k<4;k++){
                    if(NumB.charAt(j)==NumB.charAt(k)){
                        System.out.println("Jugada Invalida - Numero Con digitos repetidos"); 
                        System.out.println("favor Ingrese una jugada valida");
                        NumB=scanner.nextLine();
                        j=0;
                        k=j+1;
                        for(;NumB.length()!=4;){
                            System.out.println("Jugada Invalida: Jugada con Formato Incorrecto");
                            System.out.println("favor Ingrese una jugada valida: ");
                            NumB=scanner.nextLine();
                        }     
                    };
                }
            }
            //validamos que anteriormente no se haya ingresado ya el numero, al igual que no se repitan los digitos y que el formato sea correcto
            for(int j=0; j<i;j++){
                if(NumB.equals((NumbIntentados[j]))){
                    System.out.println("Jugada Invalida - El numero INGRESADO ya a sido insertado anteriormente, favor ingrese otro");
                    NumB=scanner.nextLine();
                    j=0;
                    for(int l=0; l<3;l++){
                        for(int k=l+1;k<4;k++){
                            if(NumB.charAt(l)==NumB.charAt(k)){
                                System.out.println("Jugada Invalida - Numero Con digitos repetidos"); 
                                System.out.println("favor Ingrese una jugada valida");
                                NumB=scanner.nextLine();
                                l=0;
                                k=l+1;
                                for(;NumB.length()!=4;){
                                    System.out.println("Jugada Inválida: Jugada con Formato Incorrecto");
                                    System.out.println("favor Ingrese una jugada valida: ");
                                    NumB=scanner.nextLine();
                                }     
                            };
                        }
                    }
                }
            }

            char P0,P1,P2,P3;
            P0=NumA.charAt(0);
            P1=NumA.charAt(1);
            P2=NumA.charAt(2);
            P3=NumA.charAt(3);
            
            for(int k=0;k<4;k++){
                if(NumB.charAt(k)==(P0) || NumB.charAt(k)==(P1) || NumB.charAt(k)==(P2) || NumB.charAt(k)==(P3)){
                    Vacas++;
                }
            }
            if(NumB.charAt(0)==(P0))Toros++;
            if(NumB.charAt(1)==(P1))Toros++;
            if(NumB.charAt(2)==(P2))Toros++;
            if(NumB.charAt(3)==(P3))Toros++;
            //System.out.println("vacas= "+Vacas);
            Vacas-=Toros;
            if(Toros==4){
                System.out.println("Respuesta A :"+Toros+" Toros, "+Vacas+" Vacas");
                System.out.println("Fin del Juego - Gano B en "+(i+1)+" intentos");
                break;    
            }
            System.out.println("Numero A: "+NumA);
            System.out.println("Intento B "+(i+1)+" "+NumB);
            System.out.println("Respuesta A :"+Toros+" Toros, "+Vacas+" Vacas");
            Vacas=0;
            Toros=0;
            NumbIntentados[i]=NumB;

        }
        if(i==12){
            System.out.println("Fin del Juego- Gano A");
        }
    }
}