How can this be improved? (it's just a little game)

0

I tried to make this game based on Magic Cards. As I'm 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 "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;
}
    
asked by anonymous 11.09.2017 / 03:53

3 answers

3

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);
}

See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

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

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

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

I've deleted unneeded variables.

I deleted the pause which is not usually a good idea.

I preferred to eliminate ENTER by accepting a character.

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

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

You could have created a condition to accept S or N uppercase.

You can do other cosmetic things.

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

    
11.09.2017 / 04:38
0

One possibility of optimization is using arrays. You can construct a two-dimensional array with the series of numbers shown by question, then with a loop for you go through each element of that array and show it as you calculate 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;
}
    
11.09.2017 / 04:35
0

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 unnecessary code repetition and the declaration of constants full of numbers magicians, 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: link

    
11.09.2017 / 16:06