How can this be improved? (It’s just a little game)

Asked

Viewed 121 times

1

I tried to make this game based on the Magic Cards. As I am still learning, that’s how I managed to do it. I would like to see other versions to know how I could have done and what I did of "wrong".

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <locale.h>
main()

{
    setlocale(LC_ALL, "Portuguese");
    //Variavel
    int v1, v2, v3, v4, v5, v6;
    int resultado;
    char alternativa;
    //Group 1
    printf("Pense em um número entre 1 e 63.\n");
    system("pause");
    printf("O número está neste grupo?\n");
    printf("1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,\n");
    printf("35,37,39,41,43,45,47,49,51,53,55,57,59,61,63\n");
    printf("s!\n");
    printf("n!\n");
    scanf("%s", &alternativa);
    if (alternativa == 's') {
        v1 = 1;
    }
    else if (alternativa == 'n') {
        v1 = 0;
    }
    //Group 2
    printf("O número está neste grupo?\n");
    printf("2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,\n");
    printf("35,38,39,42,43,46,47,50,51,54,55,58,59,62,63\n");
    printf("s!\n");
    printf("n!\n");
    scanf("%s", &alternativa);
    if (alternativa == 's') {
        v2 = 2;
    }
    else if (alternativa == 'n') {
        v2 = 0;
    }
    //Group 3
    printf("O número está neste grupo?\n");
    printf("4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31,\n");
    printf("37,38,39,44,45,46,47,52,53,54,55,60,61,62,63\n");
    printf("s!\n");
    printf("n!\n");
    scanf("%s", &alternativa);
    if (alternativa == 's') {
        v3 = 4;
    }
    else if (alternativa == 'n') {
        v3 = 0;
    }
    //Group 4
    printf("O número está neste grupo?\n");
    printf("8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31,\n");
    printf("40,41,42,43,44,45,46,47,56,57,58,59,60,61,62,63\n");
    printf("s!\n");
    printf("n!\n");
    scanf("%s", &alternativa);
    if (alternativa == 's') {
        v4 = 8;
    }
    else if (alternativa == 'n') {
        v4 = 0;
    }
    //Group 5
    printf("O número está neste grupo?\n");
    printf("16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,\n");
    printf("48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63\n");
    printf("s!\n");
    printf("n!\n");
    scanf("%s", &alternativa);
    if (alternativa == 's') {
        v5 = 16;
    }
    else if (alternativa == 'n') {
        v5 = 0;
    }
    //Group 6
    printf("O número está neste grupo?\n");
    printf("32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,\n");
    printf("48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63\n");
    printf("s!\n");
    printf("n!\n");
    scanf("%s", &alternativa);
    if (alternativa == 's') {
        v6 = 32;
    }
    else if (alternativa == 'n') {
        v6 = 0;
    }
    //
    resultado = v1 + v2 + v3 + v4 + v5 + v6;
    printf("O número é:%d\n", resultado);

    system("pause");
    return 0;
}
  • 2

    vc can start using "Cout" instead of "printf", see this thread: https://stackoverflow.com/questions/2872543/printf-vs-cout-in-c

  • You can also use cin which is the standard input flow of C++: http://www.cplusplus.com/reference/iostream/cin/. I also suggest reading a few Good Practices in C++, This can help you improve your code.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the site).

3 answers

4

I would do so:

#include <stdio.h>

int Calcula(char *sequencia1, char *sequencia2, int pontos) {
    printf("O número está neste grupo?\n");
    printf(sequencia1);
    printf(sequencia2);
    printf("s!\nn!\n");
    char alternativa;
    scanf("%c", &alternativa);
    return alternativa == 's' ? pontos : 0;
}
int main() {
    int soma = 0;
    printf("Pense em um número entre 1 e 63 e aperte <enter>.\n");
    char alternativa;
    scanf("%c", &alternativa);
    soma += Calcula("1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,\n", "35,37,39,41,43,45,47,49,51,53,55,57,59,61,63\n", 1);
    soma += Calcula("2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,\n", "35,38,39,42,43,46,47,50,51,54,55,58,59,62,63\n", 2);
    soma += Calcula("4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31,\n", "37,38,39,44,45,46,47,52,53,54,55,60,61,62,63\n", 4);
    soma += Calcula("8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31,\n", "40,41,42,43,44,45,46,47,56,57,58,59,60,61,62,63\n", 8);
    soma += Calcula("16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,\n", "48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63\n", 16);
    soma += Calcula("32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,\n", "48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63\n", 32);
    printf ("O número é:%d\n", soma);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The question has tag C++, but the code is all in C, so I did so, and within the standard normally accepted.

I removed everything that is not being used or not needed. Including comments that said nothing useful.

I organized the code with a more standardized style, intuitive and readable, even the spaces.

I eliminated unnecessary variables.

I eliminated the pause that’s not usually a good idea.

I chose to eliminate the need to give ENTER accepting a character.

The big change was to make everything that was repeated to be in a parameterized function. The name could have been better chosen.

In this function I simplified some things, among them the elimination of if.

Could have created a condition to accept the S or N capital letters.

You can do other cosmetic things.

If you analyze these sequences in more depth, maybe they can be calculated and generated on the fly instead of being explicitly described. What I will probably do later, I already found a pattern. You can call the function in a loop. If you had the requirement that you cannot use function it would be simple within this loop.

1

An optimization possibility is using arrays. A two-dimensional array can be constructed with the series of numbers shown per question, then with a loop for each of the elements of this array is traversed and shown as the resultado.

#define PERGUNTAS 6 //definição da quantidade de perguntas existente para ser facil criar mais

int main()
{
    setlocale(LC_ALL, "Portuguese");

    //agora aqui o array de perguntas
    char* series[PERGUNTAS][50] = {
        {"1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,","35,37,39,41,43,45,47,49,51,53,55,57,59,61,63"},
        {"2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,","35,38,39,42,43,46,47,50,51,54,55,58,59,62,63"},
        {"4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31,","37,38,39,44,45,46,47,52,53,54,55,60,61,62,63"},
        {"8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31,","40,41,42,43,44,45,46,47,56,57,58,59,60,61,62,63"},
        {"16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,","48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63"},
        {"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"}
    };

    int resultado = 0, i, pontuacao = 1;
    char alternativa;
    printf("Pense em um número entre 1 e 63.\n");

    for (i = 0; i < PERGUNTAS; ++i){
        printf("O número está neste grupo?\n%s\n%s\ns!\nn!\n", series[i][0], series[i][1]);
        scanf("%s", &alternativa);
        if (alternativa == 's') resultado += pontuacao; //apenas aumenta resultado se for sim
        pontuacao *= 2; //pontuação para a próxima pergunta calculada aqui
    }

    printf("O número é:%d\n", resultado);

    return 0;
}

1

Each group of numbers can be calculated dynamically by means of a function (obter_grupo()), which, in turn, can be called inside a loop, thus avoiding the unnecessary repetition of code and the declaration of constants filled with magic numbers, let’s see:

#include <stdio.h>


void exibir_grupo( int grp[32] )
{
    int i = 0;

    for( i = 0; i < 32; i++ )
        printf( "%s%d", (i>0)?",":"", grp[i] );

    printf("\n");
}


void obter_grupo( int grp[32], int n )
{
    int i = 0;
    int j = 0;

    for( i = 0; i < 64; i++ )
        if( i & (1 << n) )
            grp[ j++ ] = i;
}


int main( void )
{
    int i = 0;
    int res = 0;
    char op = 0;
    int grp[32];

    printf("Pense em um número entre 1 e 63...\n");

    for( i = 0; i < 6; i++ )
    {
        obter_grupo( grp, i );

        printf("\nO numero esta neste grupo:\n");

        exibir_grupo( grp );

        printf("[S/N]? ");
        scanf( " %c", &op );

        if( op == 'S' || op == 's' )
            res += grp[0];
    }

    printf("\nO numero eh: %d\n", res );

    return 0;
}

Reference: http://www.numericana.com/answer/magic.htm#Binary

Browser other questions tagged

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