dimanche 12 juillet 2015

Random Dice Game using functions

Just started learning functions and have a good grasp on them thanks to this thread I posted Passing variable through switch statement with functions.

Creating a dice game doing this I am having trouble with, however. It seems like it should be easier than the previous question I asked, but it is not. I am having trouble passing the three random dice through the functions. Also, as expected, my if statement at the end is not working, but I have no clue why. Here is my current as stands. Sorry in advance for my obnoxious menu name

    #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

As of now I am at a lost. I believe that I can only return one result per function. So perhaps I need to create three separate functions for each dice?




Aucun commentaire:

Enregistrer un commentaire