2015-07-12 84 views
0

剛剛開始學習功能,並有一個很好的把握,他們感謝這個線程我張貼Passing variable through switch statement with functions隨機骰子游戲使用功能

創建一個骰子游戲這樣做,但我有麻煩。它似乎應該比我問的前一個問題更容易,但事實並非如此。我無法通過函數傳遞三個隨機骰子。另外,如預期的那樣,我最後的陳述並不奏效,但我不知道爲什麼。這是我目前的看法。對不起,我不喜歡我的令人討厭的菜單名稱

#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()在初始化過程中只有一次完成:

+2

你不需要「返回」他們,只是不要*聲明他們在那個函數中 - 他們已經是全局的。 (另外,查看'srand'的一些文檔,因爲它沒有多大用處。) – usr2564301

+0

你是否已經瞭解了有關數組的知識?如果是這樣,請考慮'int dice [3]的優點;'作爲你的變量。 –

回答

0

如下更改功能。

+0

請參閱['srand()' - 爲什麼只調用一次?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/)。 –

+0

更新爲不調用srand() –

0

在C中,函數只能返回一個值。因爲diceOne的回報率第一,所以diceOne從rollTheDice()返回。如果你只是想讓它工作而不想打擾結構或任何東西,我會在你的變量聲明之前刪除'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)); 
}