the .exe file stops working in c ++

-1

I'm doing a blackjack in c ++, which when compiling it stops working the .exe file (blackjack.exe), I tried to change the main, the classes, everything but still does not work. I leave the program to see if that is the problem.

class Carta{
private:
    int numero; 
    string pinta;
    string color;

public:
    Carta (int numero, string pinta, string color) 
    {
        this->numero = numero;
        this->pinta = pinta;
        this->color = color;
    }
    void setNumero(int numero)
    {
        this->numero=numero;
    }
    void setPinta(string pinta)
    {
        this->pinta=pinta;
    }
    void setColor (string color)
    {
        this->color=color;
    }           
    int getNumero(){
        return this->numero;
    }               
    string getPinta(){
        return this->pinta;
    }       
    string getColor(){
        return this->color;
    }
};

class Baraja {
private:
    Carta *cartas[52];
public:
        Baraja (){
        for (int i=0; i<52;j++){
            cartas[i] = NULL;
            }

        for (int i=0; i<52;j++){
            if (i<13){
                for (int j=1; j<=13; j++){
                cartas[i]->setNumero(j);
                cartas[i]->setColor("negro"); 
                cartas[i]->setPinta("trebol"); 
                }
            }
            else if(i<26 && i>=13){
                for (int j=1; j<=13; j++){
                cartas[i]->setNumero(j);
                cartas[i]->setColor("negro");
                cartas[i]->setPinta("pica");
                }
            }
            else if(i<39 && i>=26){
                for(int j=1; j<=13; j++){
                cartas[i]->setNumero(j);
                cartas[i]->setColor("rojo");
                cartas[i]->setPinta("diamante");                    
                }
            }
            else{
                for(int j=1; j<=13; j++){
                cartas[i]->setNumero(j);
                cartas[i]->setColor("rojo");
                cartas[i]->setPinta("corazones");                   
                }
            }
            }
    }


    void barajar(){
        srand (time(NULL));
        for (int i = 0; i < 52; i++){
            int r = rand() % 52;
            Carta *aux = cartas[r];
            cartas[r] = cartas[i];
            cartas[i] = aux;
        }
    }

    Carta* getCartas(int i){
        return this->cartas [i];
    }

    Carta* robar(){
    Carta* aux = NULL;
    barajar();
    Inicio:
    if (cartas[52] == NULL){
        barajar();
        goto Inicio;
    }
    else{
    cartas [52] = aux;
    cartas [52] = NULL; 
    return aux;
    }
}



};

class Jugador{
protected:
    string nombre;
    int puntaje;

public:

    Jugador (string nombre, int puntaje) //constructor -inicio-
    {
        this->nombre = nombre;
        this->puntaje = 0;
    }
    void setNombre (string nombre)
    {
        this->nombre = nombre;
    }
    void setPuntaje (int puntaje)
    {
        this->puntaje = puntaje;
    }
    string getNombre()
    {
        return this->nombre;
    }
    int getPuntaje()
    {
        return this->puntaje;
    }                                    //constructor  -fin-                       

    void Jugar (Baraja *b){
        string respuesta;
        while (true){
        cout<<"Desea 'robar' o 'plantarse'?";
        cin>>respuesta;
        if (respuesta == "robar" || respuesta == "Robar"){
            if (puntaje >21){
                puntaje = 0;
                false;
            }
            else{
                if (b->robar()->getNumero() == 1){
                    b->robar()->setNumero(11);
                    puntaje = b->robar()->getNumero() + puntaje;
                }
                else{
                    puntaje = b->robar()->getNumero() + puntaje;
                }
            }               
        }
        else{
            if (puntaje >21){
                puntaje = 0;
                false;
            }
            else{
                puntaje = puntaje;
                false;
            }
        }
    }
}
};
class Crupier:public Jugador{
public:
Crupier(string nombre, int puntaje):Jugador(nombre, puntaje){
    this->nombre = "Crupier";
}
void Jugar (Baraja *b){
    while (puntaje<=16){
        if(b->robar()->getNumero() == 1){
            b->robar()->setNumero(11);
            puntaje = b->robar()->getNumero() + puntaje;    
        }
        else{
            puntaje = b->robar()->getNumero() + puntaje;
        }
    }
    if (puntaje>21){
        puntaje = 0;
    }
    else{
        puntaje = puntaje;
    }
}
};

int main(){   

Baraja* Inglesa = new Baraja();
Jugador* Jugador1 = new Jugador("Jugador uno",0) ;
Jugador* Jugador2 = new Jugador("Jugador dos",0);
Crupier* crupier  = new Crupier("Crupier",0);
int a=0;
int b=0;
int c=0;
Inglesa->barajar();
cout<<"Bienvenidos al Blackjack"<<endl;
for (int i = 0; i<2; i++){
    a = Inglesa->robar()->getNumero() + a;
    b = Inglesa->robar()->getNumero() + b;
    c = Inglesa->robar()->getNumero() + c;  
}
Jugador1->setPuntaje(a);
Jugador2->setPuntaje(b);
crupier->setPuntaje(c);

cout<<"Jugador 1 tu puntaje inicial es: "<<Jugador1->getPuntaje()<<endl;
Jugador1->Jugar(Inglesa);
cout<<"Jugador 2 tu puntaje inicial es: "<<Jugador2->getPuntaje()<<endl;
Jugador2->Jugar(Inglesa);
crupier->Jugar(Inglesa);

if(Jugador1->getPuntaje() > Jugador2->getPuntaje() && Jugador1->getPuntaje() > crupier->getPuntaje() )
{
    cout<<"El Jugador 1 es el ganador!";
}
if(Jugador2->getPuntaje() > Jugador1->getPuntaje() && Jugador2->getPuntaje() > crupier->getPuntaje() )
{
    cout<<"El Jugador 2 es el ganador!";
}
else{
    cout<<"Gana la mesa";
}
delete Inglesa;
delete Jugador1;
delete Jugador2;
delete crupier;

return 0;
}

This compiles, but when the console is displayed, the message "blackjack.exe stopped working" appears.

    
asked by Bishuu 10.10.2017 в 01:43
source

1 answer

1

the problem is in the code line Carta *cartas[52]; you are creating a two-dimensional array and you are only defining 1 dimension, when you put cartas[i] = NULL; you are not allocating memory, you are saying that it is 0.

the solution is simply to allocate the memory well with a simple for assigning 13 cards, since they are 4 suits.

for (int i = 0; i < 52; i++) {
    cartas[i] = new Carta[13];
}

One note ... use this only when you have ambiguity, such as inheritance or polymorphism. It is not a requirement to use it as much as you do.
And you can also use destructors to avoid doing the delete at the end.

    
answered by 10.10.2017 в 04:27