Why does Assertion Error give me?

0

I have an error when compiling an application that allows you to play the Baccarat game, with a class TestManoDeBacara . I have the class Carta , with its value and its type stick Enum :

    public enum PaloBarajaFrancesa {
    TREBOLES, DIAMANTES, CORAZONES, PICAS;
}

    public enum ValorBarajaFrancesa {
    AS, DOS, TRES, CUATRO, CINCO, SEIS, SIETE, OCHO, NUEVE, DIEZ, JOTA, REINA, REY;
}

Charter Class:

    public class Carta {
    public final ValorBarajaFrancesa valor;
    public final PaloBarajaFrancesa palo;

    public Carta(ValorBarajaFrancesa valor,PaloBarajaFrancesa palo) {
        this.valor=valor;
        this.palo=palo;
    }

}

Hand of Bacara: the value of a hand of Baccarat is calculated by adding the value of the cards of the hand, but knowing that the cards are AS, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE , are 1,2,3,4,5,6,7,8,9 respectively, and TEN, JOTA, QUEEN, KING are worth 0. To that sum you have to do the modular division between 10. In the bacara, the hand wins. whose score is closer to 9, which is the maximum score.

    public class ManoDeBacara {
    private Carta[] cartas;
    private int indice;
    private static final int NUMERODECARTAS=3;

    // Constructor
    public ManoDeBacara(){
    cartas=new Carta[NUMERODECARTAS];
    indice=0;
    }

    //Método obtenerValor
     //Recorre el array de resultados calcula el ordinal y devuelve el resultado final
    private int obtenerValor(Carta[] cartas){
        int valor=0;
    int valorCarta;
    for(indice=0; indice<NUMERODECARTAS;indice++) {
        if(cartas[indice]!=null) {
            if((cartas[indice].valor.name().equals("DIEZ"))||(cartas[indice].valor.name().equals("JOTA"))||(cartas[indice].valor.name().equals("REY"))||(cartas[indice].valor.name().equals("REINA")))
            {
                valorCarta=0;
        }else {
            valorCarta=cartas[indice].valor.ordinal()+1;                
        }
            valor+=valorCarta;
            valor%=10;
        }
    }
    return valor; 
    }


    /**
     * Método añadirCarta
     * Añade una carta a la mano de Bacará.
     * @param carta - Carta que se añade a la mano de Bacará
     * @return true si se pudo añadir la carta a la mano.
     * false si no se pudo añadir la carta por no admitir más cartas la mano.
     */
    public boolean añadirCarta(Carta carta){
        boolean cartaAñadida = false;
        if(indice<cartas.length){
            cartas[indice]=carta;
                indice++;
                cartaAñadida=true;
        }
            return cartaAñadida;

    }
    /**
     * Método ganaA
     * Indica si la mano utilizada para invocar al método es ganadora frente a la mano que se recibe como argumento.
     * Para saber cómo se puntúa una mano de Bacará se recomienda consultar la documentacion del reglamento del Bacará.
     * @param mano - Mano a comparar con la mano utilizada para invocar al método.
     * @return true si la mano utilizada para invocar al método es ganadora frente a la mano que se recibe como argumento.
     */
    public boolean ganaA(ManoDeBacara mano){
        boolean manoEsGanadora=false;
    manojugador=new ManoDeBacara();
    int valorbanca=mano.obtenerValor(cartas);
    int valorjugador=manojugador.obtenerValor(cartas);
    if((valorjugador > valorbanca)) 
        manoEsGanadora=true;

    return manoEsGanadora;
    }
    /**
     * Método obtenerMano
     * Devuelve una representación textual de la mano en forma de una secuencia de cartas, donde cada carta se representa a su vez con su valor y palo.
     * Por ejemplo, si la mano tiene dos cartas (tres de picas y rey de diamantes) entonces su representación textual será "[TRES/PICAS, REY/DIAMANTES]".
     * @return Representación textual de la mano.
     */
    public String obtenerMano(){
        String cadena="[";

    for(int i=0; i<NUMERODECARTAS;i++){
        if(cartas[i]!=null)
            cadena=cadena.concat(cartas[i].valor.name()+"/"+cartas[i].palo.name()+"],");
    }
    //for, hay que recorrer el array para los valores que tengamos y separarlos por comas

    return cadena;
    }


}

I am passing TestManoDeBacara , which is necessary to pass it without errors, but nevertheless it gives me AssertionError :

public class TestManoDeBacara {

private static final Carta AS = new Carta(ValorBarajaFrancesa.AS, PaloBarajaFrancesa.DIAMANTES);
private static final Carta DOS = new Carta(ValorBarajaFrancesa.DOS, PaloBarajaFrancesa.DIAMANTES);
private static final Carta TRES = new Carta(ValorBarajaFrancesa.TRES, PaloBarajaFrancesa.DIAMANTES);
private static final Carta CUATRO = new Carta(ValorBarajaFrancesa.CUATRO, PaloBarajaFrancesa.DIAMANTES);
private static final Carta CINCO = new Carta(ValorBarajaFrancesa.CINCO, PaloBarajaFrancesa.DIAMANTES);
private static final Carta SEIS = new Carta(ValorBarajaFrancesa.SEIS, PaloBarajaFrancesa.DIAMANTES);
private static final Carta SIETE = new Carta(ValorBarajaFrancesa.SIETE, PaloBarajaFrancesa.DIAMANTES);
private static final Carta OCHO = new Carta(ValorBarajaFrancesa.OCHO, PaloBarajaFrancesa.DIAMANTES);
private static final Carta NUEVE = new Carta(ValorBarajaFrancesa.NUEVE, PaloBarajaFrancesa.DIAMANTES);
private static final Carta DIEZ = new Carta(ValorBarajaFrancesa.DIEZ, PaloBarajaFrancesa.DIAMANTES);
private static final Carta JOTA = new Carta(ValorBarajaFrancesa.JOTA, PaloBarajaFrancesa.DIAMANTES);
private static final Carta REINA = new Carta(ValorBarajaFrancesa.REINA, PaloBarajaFrancesa.DIAMANTES);
private static final Carta REY = new Carta(ValorBarajaFrancesa.REY, PaloBarajaFrancesa.DIAMANTES);
private static final Boolean VERDADERO = Boolean.valueOf(true);
private static final Boolean FALSO = Boolean.valueOf(false);

static final Object[][] jugadasYResultados = {
//          mano1                           mano2                           mano2 es mano ganadora frente a mano1
        { AS,       REY,        null,       AS      , REY       , null,     FALSO },
        { REY,      AS,     null,       REY     , AS        , null,     FALSO },

        { REY,      REY,        null,       AS      , AS        , null,     VERDADERO },
        { REY,      REY,        null,       AS      , DOS       , null,     VERDADERO },
        { REY,      REY,        null,       AS      , TRES      , null,     VERDADERO },
        { REY,      REY,        null,       AS      , CUATRO    , null,     VERDADERO },
        { REY,      REY,        null,       AS      , CINCO     , null,     VERDADERO },
        { REY,      REY,        null,       AS      , SEIS      , null,     VERDADERO },
        { REY,      REY,        null,       AS      , SIETE     , null,     VERDADERO },
        { REY,      REY,        null,       AS      , OCHO      , null,     VERDADERO },
        { REY,      REY,        null,       AS      , NUEVE     , null,     FALSO },
        { REY,      REY,        null,       AS      , DIEZ      , null,     VERDADERO },
        { REY,      REY,        null,       AS      , JOTA      , null,     VERDADERO },
        { REY,      REY,        null,       AS      , REINA     , null,     VERDADERO },
        { REY,      REY,        null,       AS      , REY       , null,     VERDADERO },

        { REY,      AS,         null,       AS      , AS        , null,     VERDADERO },
        { REY,      AS,         null,       AS      , DOS       , null,     VERDADERO },
        { REY,      AS,         null,       AS      , TRES      , null,     VERDADERO },
        { REY,      AS,         null,       AS      , CUATRO    , null,     VERDADERO },
        { REY,      AS,         null,       AS      , CINCO     , null,     VERDADERO },
        { REY,      AS,         null,       AS      , SEIS      , null,     VERDADERO },
        { REY,      AS,         null,       AS      , SIETE     , null,     VERDADERO },
        { REY,      AS,         null,       AS      , OCHO      , null,     VERDADERO },
        { REY,      AS,         null,       AS      , NUEVE     , null,     FALSO },
        { REY,      AS,         null,       AS      , DIEZ      , null,     FALSO },
        { REY,      AS,         null,       AS      , JOTA      , null,     FALSO },
        { REY,      AS,         null,       AS      , REINA     , null,     FALSO },
        { REY,      AS,         null,       AS      , REY       , null,     FALSO },

        { AS,       AS,         null,       AS      , AS        , null,     FALSO },
        { AS,       AS,         null,       AS      , DOS       , null,     VERDADERO },
        { AS,       AS,         null,       AS      , TRES      , null,     VERDADERO },
        { AS,       AS,         null,       AS      , CUATRO    , null,     VERDADERO },
        { AS,       AS,         null,       AS      , CINCO     , null,     VERDADERO },
        { AS,       AS,         null,       AS      , SEIS      , null,     VERDADERO },
        { AS,       AS,         null,       AS      , SIETE     , null,     VERDADERO },
        { AS,       AS,         null,       AS      , OCHO      , null,     VERDADERO },
        { AS,       AS,         null,       AS      , NUEVE     , null,     FALSO },
        { AS,       AS,         null,       AS      , DIEZ      , null,     FALSO },
        { AS,       AS,         null,       AS      , JOTA      , null,     FALSO },
        { AS,       AS,         null,       AS      , REINA     , null,     FALSO },
        { AS,       AS,         null,       AS      , REY       , null,     FALSO },

        { AS,       DOS,        null,       AS      , AS        , null,     FALSO },
        { AS,       DOS,        null,       AS      , DOS       , null,     FALSO },
        { AS,       DOS,        null,       AS      , TRES      , null,     VERDADERO },
        { AS,       DOS,        null,       AS      , CUATRO    , null,     VERDADERO },
        { AS,       DOS,        null,       AS      , CINCO     , null,     VERDADERO },
        { AS,       DOS,        null,       AS      , SEIS      , null,     VERDADERO },
        { AS,       DOS,        null,       AS      , SIETE     , null,     VERDADERO },
        { AS,       DOS,        null,       AS      , OCHO      , null,     VERDADERO },
        { AS,       DOS,        null,       AS      , NUEVE     , null,     FALSO },
        { AS,       DOS,        null,       AS      , DIEZ      , null,     FALSO },
        { AS,       DOS,        null,       AS      , JOTA      , null,     FALSO },
        { AS,       DOS,        null,       AS      , REINA     , null,     FALSO },
        { AS,       DOS,        null,       AS      , REY       , null,     FALSO },

        { DOS,      DOS,        null,       AS      , AS        , null,     FALSO },
        { DOS,      DOS,        null,       AS      , DOS       , null,     FALSO },
        { DOS,      DOS,        null,       AS      , TRES      , null,     FALSO },
        { DOS,      DOS,        null,       AS      , CUATRO    , null,     VERDADERO },
        { DOS,      DOS,        null,       AS      , CINCO     , null,     VERDADERO },
        { DOS,      DOS,        null,       AS      , SEIS      , null,     VERDADERO },
        { DOS,      DOS,        null,       AS      , SIETE     , null,     VERDADERO },
        { DOS,      DOS,        null,       AS      , OCHO      , null,     VERDADERO },
        { DOS,      DOS,        null,       AS      , NUEVE     , null,     FALSO },
        { DOS,      DOS,        null,       AS      , DIEZ      , null,     FALSO },
        { DOS,      DOS,        null,       AS      , JOTA      , null,     FALSO },
        { DOS,      DOS,        null,       AS      , REINA     , null,     FALSO },
        { DOS,      DOS,        null,       AS      , REY       , null,     FALSO },

        { DOS,      TRES,       null,       AS      , AS        , null,     FALSO },
        { DOS,      TRES,       null,       AS      , DOS       , null,     FALSO },
        { DOS,      TRES,       null,       AS      , TRES      , null,     FALSO },
        { DOS,      TRES,       null,       AS      , CUATRO    , null,     FALSO },
        { DOS,      TRES,       null,       AS      , CINCO     , null,     VERDADERO },
        { DOS,      TRES,       null,       AS      , SEIS      , null,     VERDADERO },
        { DOS,      TRES,       null,       AS      , SIETE     , null,     VERDADERO },
        { DOS,      TRES,       null,       AS      , OCHO      , null,     VERDADERO },
        { DOS,      TRES,       null,       AS      , NUEVE     , null,     FALSO },
        { DOS,      TRES,       null,       AS      , DIEZ      , null,     FALSO },
        { DOS,      TRES,       null,       AS      , JOTA      , null,     FALSO },
        { DOS,      TRES,       null,       AS      , REINA     , null,     FALSO },
        { DOS,      TRES,       null,       AS      , REY       , null,     FALSO },

        { TRES,     TRES,       null,       AS      , AS        , null,     FALSO },
        { TRES,     TRES,       null,       AS      , DOS       , null,     FALSO },
        { TRES,     TRES,       null,       AS      , TRES      , null,     FALSO },
        { TRES,     TRES,       null,       AS      , CUATRO    , null,     FALSO },
        { TRES,     TRES,       null,       AS      , CINCO     , null,     FALSO },
        { TRES,     TRES,       null,       AS      , SEIS      , null,     VERDADERO },
        { TRES,     TRES,       null,       AS      , SIETE     , null,     VERDADERO },
        { TRES,     TRES,       null,       AS      , OCHO      , null,     VERDADERO },
        { TRES,     TRES,       null,       AS      , NUEVE     , null,     FALSO },
        { TRES,     TRES,       null,       AS      , DIEZ      , null,     FALSO },
        { TRES,     TRES,       null,       AS      , JOTA      , null,     FALSO },
        { TRES,     TRES,       null,       AS      , REINA     , null,     FALSO },
        { TRES,     TRES,       null,       AS      , REY       , null,     FALSO },

        { TRES,     CUATRO,     null,       AS      , AS        , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , DOS       , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , TRES      , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , CUATRO    , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , CINCO     , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , SEIS      , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , SIETE     , null,     VERDADERO },
        { TRES,     CUATRO,     null,       AS      , OCHO      , null,     VERDADERO },
        { TRES,     CUATRO,     null,       AS      , NUEVE     , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , DIEZ      , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , JOTA      , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , REINA     , null,     FALSO },
        { TRES,     CUATRO,     null,       AS      , REY       , null,     FALSO },

        { CUATRO,   CINCO,      null,       AS      , AS        , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , DOS       , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , TRES      , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , CUATRO    , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , CINCO     , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , SEIS      , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , SIETE     , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , OCHO      , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , NUEVE     , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , DIEZ      , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , JOTA      , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , REINA     , null,     FALSO },
        { CUATRO,   CINCO,      null,       AS      , REY       , null,     FALSO },
        null
};

public static void main(String[] args) {
    int idxJugada;
    idxJugada = 0;
    final ManoDeBacara manoDeBacara = new ManoDeBacara();
    for(java.lang.reflect.Field f:manoDeBacara.getClass().getDeclaredFields())
        EVALUAR ( java.lang.reflect.Modifier.isPrivate(f.getModifiers()), "Error: puedo acceder al atributo " + f.getName() + " de la mano de Bacara" );

    while(jugadasYResultados[idxJugada] != null)
        extraerYProbarDatos(jugadasYResultados[idxJugada++]);
    System.out.println("Test terminado sin errores.");
}

private static void extraerYProbarDatos(Object[] elementos) {
    int idxElemento;
    idxElemento = 0;
    ManoDeBacara a;
    a = new ManoDeBacara();
    while(elementos[idxElemento] != null)
        a.añadirCarta((Carta)elementos[idxElemento++]);
    ManoDeBacara b;
    b = new ManoDeBacara();
    idxElemento++;
    while(elementos[idxElemento] != null)
        b.añadirCarta((Carta)elementos[idxElemento++]);
    boolean esManoGanadora = (Boolean)elementos[++idxElemento];
    testJugada(a, b, esManoGanadora);
}

private static void testJugada(ManoDeBacara a, ManoDeBacara b, boolean esManoGanadora) {
    EVALUAR(b.ganaA(a) == esManoGanadora, "ERROR: [" + (esManoGanadora ? b.obtenerMano() : a.obtenerMano()) + "] debe ser mano ganadora frente a [" + (esManoGanadora ? a.obtenerMano() : b.obtenerMano()) + "]");
}

private static void EVALUAR(boolean predicado, String mensajeDeError) {
    if(!predicado) throw new AssertionError(mensajeDeError);
}

}

And the error on the screen: Exception in thread "main" java.lang.AssertionError: ERROR: [[AS / DIAMONDS], AS / DIAMONDS],] must be winning hand in front of [[KING / DIAMONDS], KING / DIAMONDS],]     at TestManoDeBacara.EVALUAR (TestManoDeBacara.java:196)     at TestManoDeBacara.testJugada (TestManoDeBacara.java:192)     at TestManoDeBacara.extract and TestData (TestManoDeBacara.java:188)     at TestManoDeBacara.main (TestManoDeBacara.java:171)

    
asked by celiamusic 08.04.2018 в 11:04
source

1 answer

0

The error comes from a test in class TestManoDeBacara . Specifically, the test is TestManoDeBacara#testJugada , which checks that the #ganaA method works correctly.

I followed the ganaA method and realized that you are creating a new hand each time you call the method. It's a mistake, because that way you're not comparing an alien hand with the hand you're calling the method .

Put another way: imagine that you have a hand and want to check against the bank. As you have done, with the ganaA method belonging to an instance of ManoDeBacara (your hand) , you are asking your hand to check its value against that of the bank. Up there well. The problem is that when you create a new hand and do not use yours (using this ), suddenly you are not checking your hand against the bank but a new empty hand against the bank .

From there I continued looking and I found another error, which I describe briefly: to calculate the value of a hand, you have to pass an array of letters, and the array of cards is private, as well as the method. How do you expect to draw the value of the cards from another hand, if you do not know what cards that hand has? I would change the signature of the method so that you do not need the array of cards, and take the one you already have in the class itself.

So it would look like this:

public int obtenerValor(){  // ahora es publico para poder acceder a el
    int valor=0;            // y no necesita un array de cartas para calcularlo, usa el de la mano
    int valorCarta;
    for(indice=0; indice<NUMERODECARTAS;indice++) {
        if(cartas[indice]!=null) {
            if((cartas[indice].valor.name().equals("DIEZ"))||(cartas[indice].valor.name().equals("JOTA"))||(cartas[indice].valor.name().equals("REY"))||(cartas[indice].valor.name().equals("REINA")))
            {
                valorCarta=0;
            }else {
                valorCarta=cartas[indice].valor.ordinal()+1;
            }
            valor+=valorCarta;
            valor%=10;
        }
    }
    return valor;
}

public boolean ganaA(Mano mano) {
    int valorJugador = obtenerValor();      // el valor de ESTA mano
    int valorBanca = mano.obtenerValor();   // el valor de la otra mano

    return valorJugador > valorBanca;
}
    
answered by 08.04.2018 / 22:28
source