cho-han bakuchi

3

I'm trying to make the game cho-han bakuchi in C. It is very simple, the Japanese game consists of throwing two 6-sided dice in a cup, before the dice are shown the player makes the bet saying cho (pairs) or han (odd). Then it shows the data and the values are summed.

What am I missing in my code?

I would not like the full code, just from the point where I'm making a mistake.

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

int main()
{
    int i,x,apost,cred=100,result=0;

    while(apost!=EOF)
    {
        printf("faça a aposta:(digite -1 para sair)\n");
        scanf("%d", &apost);

        cred=cred-apost;

        printf("escolha 1-cho(par) 2-han(impar):\n");
        scanf("%d", &x);

        switch(getc(x))
        {
            case 1:
                i=2+rand()%12;
                printf("\n%10d", i);

                if(i%2==0)
                {
                    printf("ganhou!\n");
                    cred=cred+(apost*2);
                }

                else
                    printf("perdeu!");

                break;

                case 2:
                    i=2+rand()%12;
                    printf("\n%10d", i);

                    if(i%2!=0)
                    {
                        printf("ganhou!\n");
                        cred=cred+(apost*2);
                    }

                    else
                    printf("perdeu!");
        }

    }

    return 0;
}
    
asked by anonymous 09.07.2014 / 15:10

1 answer

5

Always compile using warnings! If you use GCC or Clang, pass this option: -Wall to enable all principals. You can add% with% to get even more alerts. They would point out some of the mistakes I'm going to list:

  • Uninitialized variable:

    You declared the variable -Wextra and then read its value: int apost . Reading the value of a variable without first giving it a value is invalid, its executable can do anything. In practice, this condition can give true or false in an unpredictable way. Always initialize your variables!

  • Test EOF on a while(apost!=EOF) :

    int is an integer, stores the value of the bet. apost reads a number. On the other hand scanf("%d", &apost); is a EOF that indicates end of reading and will never be returned by scanf if you asked for a number. Consider using the following logic:

    while (true) {
        int apost;
    
        printf("faça a aposta:(digite -1 para sair)\n");
        scanf("%d", &apost);
    
        if (apost < 0) break;
    
        /* ... */
    }
    
  • char returns a letter, not a number:

    Understand that getc is not is the same as '3' . The first is a letter, which is equivalent to the integer 53 on most systems (if you use ASCII). You should test for the letters in your switch, like this: 3 .

  • Probability distribution:

    The sum of two random numbers from 1 to 6 is different from a random number of 2 to 12. This is because combinations that result in 5, for example are much more likely than combinations that result in 2. To form 5 we can have: 1 + 4, 2 + 3, 3 + 2, 4 + 1. While 2 may be only 1 + 1. So it will be 4 times more likely. This is the so-called normal distribution. The correction, however, is simple: generate the two values separately and then add up.

  • 09.07.2014 / 15:36