Using functions

Asked

Viewed 74 times

1

Good afternoon! I am implementing a game of stone, paper and scissors, where occurs the application of functions, with and without argument and return. I was in doubt, after the program identify the options of the user and the computer, because I could not hit the final result Ex: "Stone kneads scissors" but cases where I did not choose scissors and nor stone.

#include < stdio.h > 
#include < stdlib.h >
#include < time.h >

    int ler_jogada() {
        int jog_usuario;

        printf("Digite (1) para pedra, (2) para papel, (3) para tesoura: ");
        scanf("%d", & jog_usuario);
        return jog_usuario;
        while (jog_usuario < 1 || jog_usuario > 3) {
            printf("Valor invalido, informe outro!");
            scanf("%d", & jog_usuario);
            fflush(stdin);
        }
        return jog_usuario;
    }

int ler_comp() {
    int jogada_comp;
    srand(time(NULL));
    jogada_comp = 1 + rand() % 4;

    return jogada_comp;
}
void imprime_jog(int jog_usuario, int jogada_comp) {
    switch (jog_usuario) {
        case 1:
            printf("\nVoce escoheu pedra!");
            break;
        case 2:
            printf("\nVoce escolheu papel!");
            break;
        case 3:
            printf("\nVoce escolheu tesoura!");
            break;
    }
    switch (jogada_comp) {
        case 1:
            printf("\nEscolhi pedra!");
            break;
        case 2:
            printf("\nEscolhi papel!");
            break;
        case 3:
            printf("\nEscolhi tesoura!");
            break;
    }
}
int calc_resultado(int jog_usuario, int jogada_comp) {
    if (jog_usuario == 1 && jogada_comp == 2) {
        printf("\nPapel cobre pedra!");
        return 1;
    }
    if (jog_usuario == 1 && jogada_comp == 3) {
        printf("\nPedra amassa tesoura!");
        return 2;
    }
    if (jog_usuario == 2 && jogada_comp == 1) {
        printf("\nPapel cobre pedra!");
        return 2;
    }
    if (jog_usuario == 2 && jogada_comp == 3) {
        printf("\nTesoura corta papel!");
        return 1;
    }
    if (jog_usuario == 3 && jogada_comp == 1) {
        printf("\nPedra amassa tesoura");
        return 1;
    }
    if (jog_usuario == 3 && jogada_comp == 2) {
        printf("\nTesoura corta papel");
        return 2;
    }
    if (jog_usuario == jogada_comp) {
        return 3;
    }
    return 0;
}

int main() {
    int jog_usuario, jogada_comp, x;

    jog_usuario = ler_jogada();

    jogada_comp = ler_comp();

    imprime_jog(jog_usuario, jogada_comp);

    x = calc_resultado(jog_usuario, jogada_comp);


    switch (x) {
        case 1:
            printf("\n\nGanhei! Quem sabe voce ganha na proxima...!");
            break;
        case 2:
            printf("\n\nVoce ganhou!!!Parabens!");
            break;
        case 3:
            printf("\n\nEmpate! Vamos de novo!");
            printf("\n");
            main();
            break;
    }

    return 0;
}
  • The problem is he falls in if(jog_usuario==3&&jogada_comp==1); even when those were not the options chosen? I did not understand very well what the problem is.

  • For any selection will be informed that stone kneads scissors

  • Hint for the next time you ask a question: it is easier to reproduce the error if you clearly mention which program entry leads to the error. It is also useful to try to create a reduced version of the program that still displays unwanted behavior. The smaller the program, the easier it is to find the error! (for example, you can make a version that does not read from the input and already calls calc_resultado(1,3) straightforward

2 answers

2

You have several ; after the ifs:

if (jog_usuario == 1 && jogada_comp == 3); {  // remova o ; depois do )
    printf("\nPedra amassa tesoura!");
    return 2;
}
// há também outras instâncias desse erro no código

What is happening is that the if is running, but there is a Empty statement after him (the ;). After that, the { opens a block that is always executed (since it is not in any if). That’s why this function always returns 2.

0

The first Obs is that you give a Return on the ler_move() function before entering while(). That is, this validation never happens.

Second thing is this excerpt jogada_comp = 1 + rand() % 4; defines values from 1 to 4 (should be 1 to 3) and this invalidates some situations.

And obviously the problem with the ";" already cited.

Browser other questions tagged

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