簡體   English   中英

使用功能的隨機骰子游戲

[英]Random Dice Game using functions

剛開始學習函數,並且對它們有了很好的了解,這要歸功於我發布的該線程通過帶有功能的switch語句傳遞變量

但是,創建一個骰子游戲確實很麻煩。 似乎它應該比我之前提出的問題要容易,但事實並非如此。 我在通過函數傳遞三個隨機骰子時遇到麻煩。 而且,正如預期的那樣,我的if語句最后不起作用,但是我不知道為什么。 這是我目前的立場。 抱歉,我的菜單名稱令人討厭

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

    #define MAXROLLS 5
    #define LOWERBOUND 1
    #define UPPERBOUND 6
    #define PAUSE system("pause")

    int diceOne, diceTwo, diceThree;
    int currentDiceSum=0, totalDiceSum=0;
    int quit= 0;
    int count = 0;





    char menuChoice ()
    {

    char choice;

    printf("\n\n==============================================================================\n");
    printf("\n\n==  W E L C O M E     T O     M Y     D I  C E     R O L L I N G     G A M E==\n");
    printf("\n\n==============================================================================\n");
    printf("\n Roll the dice, but you only get 5 rolls! You can't play forever, you know. \n");

    printf("Main Menu\n");
    printf("A.Roll the Dice\n");
    printf("B.Display the Result of Last Roll\n");
    printf("C.Quit\n");


    printf("Enter your choice:   ");
    scanf(" %c", &choice);
    choice = toupper(choice);

}


int rollTheDice() 
{
    int diceOne = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    int diceTwo = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    int diceThree = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    srand((unsigned)time(NULL));



    return diceOne;
    return diceTwo;
    return diceThree;



}


int getDiceRoll()
{

    currentDiceSum = diceOne + diceTwo + diceThree;
    totalDiceSum+= currentDiceSum;
}


int quitTotal()
{

   totalDiceSum+= currentDiceSum;


}

int main()
{


     while(quit!=1) //begin menu loop
    {
        char menu;



        menu = menuChoice();
        switch(menu)
        {
            case 'A':
            {
                 rollTheDice();
                 printf("Dice are rolled!\n");
                 count++;
                 printf("You have %i rolls left.\n", MAXROLLS - count);
                 break;
            }


            case 'B':

               getDiceRoll();  
               printf("Dice 1: %d\n", diceOne);

               printf("Dice 2: %d\n", diceTwo);

               printf("Dice 2: %d\n", diceThree);

               printf("Dice Total: %d\n", currentDiceSum);

            break;
            case 'C':
              quitTotal();
               printf("Number of rolls: %d\n", count);

               printf("Total of all dice for all rolls: %d\n", totalDiceSum);

               printf("Goodbye, hope to see you again!!!\n");
               PAUSE;
               quit = 1;
                break;
            default:
                printf("Please enter A,B,C\n");
                break;
        } //end switch
    } // end loop


if (count == MAXROLLS)

{

    printf("Sorry, your rolls are up!!!\n");


    printf("Your final roll was:\n");

    printf("Dice 1: %d\n", diceOne);

    printf("Dice 2: %d\n", diceTwo);

    printf("Dice 3: %d\n", diceThree);

    currentDiceSum = diceOne + diceTwo + diceThree;

    printf("Your final dice sum was\n");
    printf("Dice Total: %d\n", currentDiceSum);

    totalDiceSum+= currentDiceSum;



    printf("Number of rolls: %d\n", count);
    printf("Total of all dice for all rolls: %d\n", totalDiceSum);
    printf("Goodbye, hope to see you again!!!\n");



}

} //end function

截至目前,我很茫然。 我相信每個函數只能返回一個結果。 因此,也許我需要為每個骰子創建三個單獨的函數?

如下更改功能:

typedef struct{
  int diceOne;
  int diceTwo;
  int diceThree;
}DiceRolls;

DiceRolls rollTheDice() 
{
  DiceRolls dice;

  dice.diceOne = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
  dice.diceTwo = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
  dice.diceThree = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
  return dice;
}

根據下面的注釋,建議對srand()的調用在初始化期間僅執行一次。

在C語言中,函數只能返回一個值。 因為首先返回diceOne,所以將從rollTheDice()返回diceOne。 如果您只是想讓它正常工作,並且不想打擾結構或其他任何事情,我會在變量聲明之前刪除“ int”以將值分配給全局變量,而不是創建新的局部變量,這將導致rollTheDice()看起來像這樣:

int rollTheDice() 
{
    diceOne = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    diceTwo = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    diceThree = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    srand((unsigned)time(NULL));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM