我正在尝试使用以下规则构建骰子游戏:
你掷三个骰子
如果掷骰子且每个骰子为6,您将赢得100kr
如果掷骰子且每个骰子相同(但不是6个),则赢得50kr
如果掷骰子且两个骰子相同,则赢得10 kr
玩家每轮必须下注至少10kr
如果玩家获胜,询问他们是否要再次玩
当玩家的钱少于10kr时,玩家会被告知他们不能再玩了。
我已经能够做到这一点,但是现在我必须通过调用一个函数来运行它。我现在被卡住了-我的代码在下面,但是我不确定如何继续。
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<stdbool.h>
int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling ); // prototype
keep_rolling = 'y';
int main()
{
int d1, d2, d3, money, r;
char keep_rolling = 'n';
money = 100;
r = rolling(d1, d2, d3, money,keep_rolling);
srand((int)time(NULL));
printf("welcome to the game! \n \n");
//gameroll
printf("1.it cost you 10kr to play \n");
printf("2.if all the dicies are sixes you win 100kr \n");
printf("3. if all the dicies are alike except number (6) you win 50kr \n");
printf("4. if you get at least two alike you 1 \n");
printf("5. otherwise you win nothing\n");
printf("you have %d kr, if you wnat to play press (n) \n\n", money);
fflush(stdin);
keep_rolling = ((getchar()));
d1 = (rand() % 6 + 1);
d2 = (rand() % 6 + 1);
d3 = (rand() % 6 + 1);
}
int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling) // def my function
{
keep_rolling = 'y';
do {
dice1 = (rand() % 6 + 1);
dice2 = (rand() % 6 + 1);// from 1 to 6
dice3 = (rand() % 6 + 1);
if (moneyinput < 10)
{
printf("you do not have enough money \n");
break; // exit the program
}
moneyinput -= 10;
if (dice1 == 6 && dice2 == 6 & dice3 == 6)
{
printf("you have won 100\n ");
moneyinput += 90;
}
else if (dice1 == dice2 == dice3)
{
printf("you have won 50kr \n");
moneyinput += 40;
}
else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3)
{
printf("you have won 10kr \n");
moneyinput += 10;
}
else
{
printf("Sorry! you have lost. god luck next time \n");
}
} while (keep_rolling == 'y');
system("pause");
}
最佳答案
您需要将您的问题移至用户的滚动功能。您的主要功能并非独立于滚动功能运行。
另外,如果if语句中存在一些逻辑错误,这些错误会使您的结果与所需的结果有所不同。
if (dice1 == 6 && dice2 == 6 & dice3 == 6)
应该
if (dice1 == 6 && dice2 == 6 && dice3 == 6)
和
else if (dice1 == dice2 == dice3)
应该
else if (dice1 == dice2 && dice2 == dice3)
除此之外,您还需要从滚动功能中获得回报。