cho-han bakuchi

Asked

Viewed 277 times

3

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

What I’m missing in my code?

I wouldn’t like the full code, just the point I’m missing so I can make the correction.

#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;
}
  • In the edition you changed the code on case 1:. Are you sure that’s right?

  • So William I’m learning I’m not sure I’m making attempts and keeping the code updated

  • 1

    Do not change the question code. Use the one from the moment you had the doubt and keep it that way. Note the edition history to know right what has changed. The idea is that the question does not lose context with the answer to a future reader.

  • ok but good I’ll leave it so because I’m no longer moving in the code (for now) I know q can not be 2+Rand()%12 and yes 1+Rand()%6 twice, as I do to add that I can put the result within an aux?

  • int i = 0; i += 1+rand()%6; i += 1+rand()%6; printf("%d", i);

1 answer

5


Always Compile using warnings! If you use GCC or Clang, pass this option: -Wall to enable all main ones. You can add -Wextra to have even more alerts. They would point out some of the errors that I will list:

  1. Uninitialized variable:

    You declared the variable int apost and soon afterwards read its value: while(apost!=EOF). 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!

  2. Testing EOF in a int:

    apost is an integer, stores the value of the bet. scanf("%d", &apost); reads a number. On the other hand EOF is a char which indicates end of reading and will never be returned by scanf if you asked it 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;
    
        /* ... */
    }
    
  3. getc returns a letter, not a number:

    Understand that '3' nay is the same thing as 3. The first is a letter, which is equivalent to the 53 integer on most systems (ASCII is used). You should test by the letters on your switch, like this: case '1':.

  4. Probability distribution:

    The sum of two random numbers from 1 to 6 is different of 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. Whereas 2 may be only 1+1. So it will be 4 times more likely. This is called the normal distribution. The correction, however, is simple: generate the two values separately and then disappear.

  • Guilherme I even edit my while at the time you made the edition it looks dinovo my code

  • i had done that four tip way the first time and still had not worked out this giving the same error the program stops responding as if I had forgotten to put & scanf

  • Guilherme sorry I saw some mistakes I made including the while was bad

  • 1

    Hmmm: EOF is a type value int

  • @pmg, yes, is a macro that translates to an integer. But it represents the end of the input, and was not used in that context.

  • @Guilhermebernal I’ll change the code I modified the code a lot but I’m having an error with the function In case you can help me I already decided to run the game but he was bugging there I tried to get some tips from book C how to program Ir. Deitel. If you can take a look.

  • @Leonardov.Degasperin I will reverse your edition. For another question open another question. Do not reuse one and mischaracterize answers already given.

Show 2 more comments

Browser other questions tagged

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