c-骰子游戏数字不是随机的



基本上,我应该模拟一个掷三个骰子的程序,把它加起来。然后我会让用户猜测下一个滚动是更高、更低还是相同,或者他们只是想退出。我有两个问题。

  1. 这些数字不是随机的,显然这是我的一个大错误,但我似乎搞不清楚。我想我不需要第二次包含3个骰子对吗?不管怎样,他们一点帮助都没有。

  2. 无论如何,我的程序一次遍历所有if/else-if语句。显然我不希望这种情况发生。


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
    int diceOne, diceTwo, diceThree, diceSum=0, timesCorrect=0, choice; 
    int newDiceOne, newDiceTwo, newDiceThree, newDiceSum;
    srand( time(NULL) );
    diceOne      = rand() % 6 + 1;
    diceTwo      = rand() % 6 + 1;
    diceThree    = rand() % 6 + 1;
    newDiceOne   = rand() % 6 + 1;
    newDiceTwo   = rand() % 6 + 1;
    newDiceThree = rand() % 6 + 1;
    printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
    diceSum = diceOne + diceTwo + diceThree;
    printf("nTotal sum of dice is %dn", diceSum);
    do {
        printf("Guess higher(1), lower(2), same(3) or quit?(4)n");
        printf(" You have been correct %d timesn", timesCorrect);
        scanf("%d", &choice);
        printf("The three dice rolls: %d, %d, %d", newDiceOne, newDiceTwo, newDiceThree);
        newDiceSum= newDiceOne + newDiceTwo + newDiceThree;
        printf("nTotal sum of dice is %dn", newDiceSum);
        if (choice == 1)
        {
            if (newDiceSum > diceSum);
                timesCorrect++;
            printf("You are correct!n");
        }
        else if (newDiceSum < diceSum); 
        {
            printf("You are incorrect, sorry!n");
        }
        if (choice == 2)
        {
            if (newDiceSum < diceSum);
                timesCorrect++;
            printf("You are correct!n");
        }
        else if (newDiceSum > diceSum); 
        {
            printf("You are incorrect, sorry!n");
        }
        if (choice == 3)
        {
            if (newDiceSum == diceSum);
                timesCorrect ++;
            printf("You are correct!n");
        }
        else if (newDiceSum != diceSum); 
        {
            printf("You are incorrect, sorry!n");
        }
        if (choice == 4)
        {
            printf("Thanks for playing!!!!!!n");
            system("pause");
            return 0;
        }
    } while (choice!= 4 );
}

else if条件句后面有一个额外的分号,如这里的

else if (newDiceSum < diceSum); 
                /*            ^ this should not be here */

如果你使用一个具有良好诊断功能并启用警告的编译器,它应该警告你"打字错误",如果你想让块为空,请使用像这样的大括号

else if (newDiceSum < diceSum) {}

此外,使用rand()设置第一个骰子,它们是随机值,但在循环中始终使用相同的值。

以下代码:

  1. 处理错误条件

  2. 消除不需要的变量

  3. 干净地编译

  4. 在没有失败的情况下执行


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( void )
{
    int diceOne;
    int diceTwo;
    int diceThree;
    int diceSum=0;
    int timesCorrect=0;
    int choice;
    int newDiceSum;
    srand( time(NULL) );
    diceOne      = rand() % 6 + 1;
    diceTwo      = rand() % 6 + 1;
    diceThree    = rand() % 6 + 1;

    printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
    diceSum = diceOne + diceTwo + diceThree;
    printf("nTotal sum of dice is %dn", diceSum);
    do {
        printf("Guess higher(1), lower(2), same(3) or quit?(4)n");
        printf(" You have been correct %d timesn", timesCorrect);
        if( 1 != scanf("%d", &choice) )
        { // then scanf failed
            perror( "scanf for choice failed" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf successful
        diceOne      = rand() % 6 + 1;
        diceTwo      = rand() % 6 + 1;
        diceThree    = rand() % 6 + 1;
        printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
        newDiceSum = diceOne + diceTwo + diceThree;
        printf("nTotal sum of dice is %dn", newDiceSum);
        switch( choice )
        {
        case 1:
            if (newDiceSum > diceSum)
            {
                timesCorrect++;
                printf("You are correct!n");
            }
            else
            {
                printf("You are incorrect, sorry!n");
            }
            break;
        case 2:
            if (newDiceSum < diceSum)
            {
                timesCorrect++;
                printf("You are correct!n");
            }
            else
            {
                printf("You are incorrect, sorry!n");
            }
            break;
        case 3:
            if (newDiceSum == diceSum)
            {
                timesCorrect ++;
                printf("You are correct!n");
            }
            else
            {
                printf("You are incorrect, sorry!n");
            }
            break;
        case 4:
            printf("Thanks for playing!!!!!!n");
            system("pause");
            break;
        default:
            printf( " invalid choice, valid choices are 1...4n");
            break;
        } // end switch
    } while (choice!= 4 );
    return(0);
} // end function: main

最新更新