Problem zeroing out a game score

Asked

Viewed 84 times

0

Well, I read Bjarne’s book on programming and I’m learning programming there, I’m in chapter 5 which is about mistakes, at the end of the chapter there’s a list of exercises for me to do, I came across an exercise where I have to create a kind of a game that checks a vector of 4 number from 1 to 9 that the user type with the random value created on the computer for each right number in the right position that you hit is an armadillo and for each right number in the position wrong is a Pivara, the problem is that at first it works, but then what happens is that the program does not Zera the value of armadillos and capybaras, I’ve tried everything that is way, does anyone have any suggestions? Sometimes it’s something silly that I missed and didn’t even realize.

#include <iostream>
#include <algorithm>
#include <vector>
#include <locale>
using namespace std;

vector<int> chave; //chave q o user vai tentar advinhar
vector<int> adv; //vetor com os valores do user
int jogadas = 0; //quantitades de vezes que o jogador jogou
int tatu = 0; // numero certo no lugar certo
int capi = 0; // numero certo no lugar errado
const int jogador = 4;

class Deubad{
    //exceção do rolê que deu ruim
};

class acabou{
    //exceção do fim
};

int gerador(int s){
    srand(s); //gera uma seed nova pra cada vez que é chamado 
    for(int x = 0; x < 9; x++){ //gera um vetor de 1 a 9 ou seja com 10 valores dentro
        chave.push_back(x+1);  
    }
    random_shuffle(chave.begin(),chave.end()); //tipo embaralha as cartas hehe, lembrando que usa somente os 4 primeiros numeros da sequencia
    for(int x : chave){
        cout<<x<<endl; //isso é só pra listar, pois não sei usar depurador
    }
}

vector<int> inserir(vector<int> f){ //insere os valores que o user digitar
    for(int x = 0; x < 4;x++){
        int val;
        cin>>val;
        if(val == 0 || val > 9) throw Deubad{};
        else;
        f.push_back(val);
    }
    return f;
}

int verificar(){
    for(int x = 0; x < 4;x++){
        if(adv[x] == chave[x]){ //comparando se os números são de mesmo valor e mesma posição
            tatu += 1;
        }else{
        }
        for(int y = 0; y < 4;y++){
            if(adv[x] == chave[y] && adv[x] != chave[x]){ //comparando se os numeros são de mesmo valor e posição diferente
                capi += 1;
            }else{
            }
        }
    }
    jogadas += 1;
    cout<<"Você tem "<<tatu<<" tatus e "<<capi<<" capivaras..."<<endl;
    tatu = 0;
    capi = 0;
}

int ganhou(){ //verifica se o jogador acertou tudo
    int val = 1;
    if(tatu == 4){
        cout<<"Você ganhou!!!"<<endl;
        return val;
    }else{
        val = 2;
        return val;
    }
}

int jogardnov(){ // pergunta se o usuario quer jogar novamente
    char r = ' ';
    cout<<"Quer jogar novamente? Digite S para sim e N para não"<<endl;
    cin>>r;
    int val = 2;
    if(r == 'S' || r == 's'){
        return val;
    }else if(r == 'N' || r == 'n'){
        return 0;
    }else{
        throw Deubad{};
    }
}

void novament(){ //executa tudo dnovo
    do{
        cout<<"Você já tentou "<<jogadas<<" vezes"<<endl;
        tatu = 0;
        capi = 0;
        adv = inserir(adv);
        verificar();
        int vef = ganhou();
        if(vef == 1){
            if(jogardnov() == 2){
                break;
            }else{
                throw acabou{};
            }
        }else{
            tatu = 0;
            capi = 0;
            break;
        }
    } while(tatu != 4);
}

int main(){
try{
    setlocale(LC_ALL,"Portuguese");
    cout<<"Isto é um jogo, mesmo que não tenha um grande gráfico é um jogo"<<endl
        <<"Esse jogo chama tatus e capivaras"<<endl //o charme do joguin tá no nome que ele recebe saca
        <<"Funciona da seguinte maneira, tenho uma chave de 4 digitos \n que contém números de 1 a 9 aqui comigo..."<<endl
        <<"Voce tem que ser ninja pra acertar tudo de primeira..."<<endl
        <<"Pra cada número certo no lugar certo que você tem tatu"<<endl
        <<"Já se você acertar o número no lugar errado você tem uma capivara"<<endl
        <<"Então vamos ver o que aconteceu..."<<endl
        <<"Preciso que você digite um número qualquer"<<endl;
    int n;
    cin>>n;
    if(chave.size() == 0){
        gerador(n);
    }else{
        srand(n);
        random_shuffle(chave.begin(),chave.end());
    }
    cout<<"Agora você vai preencher as suas possibilidades, \n com números de 1 a 9, não esqueça!!"<<endl;
    adv = inserir(adv);
    verificar();
    int val = ganhou();
    do{
        if(val == 1){
            val = jogardnov();
            if(val == 2){
                system("CLS");
                main();
            }else{
                return 0;
            }
        }else{
            novament();
        }
    }while(true);
}
catch(Deubad){
    cerr<<"Você digitou algo errado em... SEU BABACA ANALFABETO OTARIO"<<endl;
    novament(); 
}
catch(acabou){
    cerr<<"Seu arrogante não gostou do meu jogo seu babacão!! \nfalo mesmo não sou baú"<<endl;
}
}
  • all cases (lose, win, etc...) are not zero?

  • I went to review the entire code I found the error

  • Put the answer then.

1 answer

0


I found the error, not problem with score but with vector.

The vector q you typed in the last round is still there, which is why it hinders checking the vector items. I used a line to solve this problem:

adv.clear();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.