刚开始学习函数并且很好地掌握它们,这要归功于我发布的帖子Passing variable through switch statement with functions。
创建骰子游戏这样做我遇到了麻烦。看起来它应该比我提出的上一个问题更容易,但事实并非如此。我无法通过函数传递三个随机骰子。另外,正如预期的那样,我最后的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
截至目前,我处于迷失状态。我相信每个函数我只能返回一个结果。那么也许我需要为每个骰子创建三个独立的函数?
答案 0 :(得分:0)
按如下方式更改您的功能:
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()
进行一次调用。
答案 1 :(得分:0)
在C中,函数只能返回一个值。因为diceOne的返回首先出现,所以从rollTheDice()返回diceOne。如果你只是想让它工作并且不想打扰结构或任何东西,我会在你的变量声明之前删除“int”以将值赋给全局变量而不是创建新的本地变量vaiables,导致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));
}