我尝试使用以下规则制作骰子游戏:
我能够做到这一点,但现在我必须通过调用函数来运行它。我已经陷入困境 - 我的代码在下面,但我不确定如何继续。
#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");
}
答案 0 :(得分:1)
您需要将问题移至用户的滚动功能。您的主要功能不能独立于滚动功能运行。
你的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)
除此之外,您还需要从滚动功能返回。
答案 1 :(得分:0)
请尝试以下方法:
它被编写为在Linux终端上运行,因此它使用fgets()进行用户输入
我们的想法是在main()中有一个循环继续运行,直到用户想要停止或用完钱。在这个循环中调用你的骰子滚动功能。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int diceRoll(int moneyinput);
int diceRoll(int moneyinput)
{
int dice1 = (rand() % 6 + 1);
int dice2 = (rand() % 6 + 1);// from 1 to 6
int dice3 = (rand() % 6 + 1);
moneyinput -= 10;
if (dice1 == 6 && dice2 == 6 && dice3 == 6)
{
printf("you have won 100\n ");
moneyinput += 90;
}
else if (dice1 == dice2 && 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. good luck next time \n");
}
return( moneyinput);
}
int main()
{
int money = 100;
char choice[2];
srand((int)time(NULL));
printf("welcome to the game! \n \n");
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");
do
{
printf("you have %d kr, if you want to play press (y): ", money);
fgets( choice, sizeof(choice)+1, stdin ); // read a char then LF
if( 'y' == choice[0] )
{
money = diceRoll( money );
}
}while( (money >= 10) && choice[0] == 'y' );
printf("Quitting game with %dkr!\n", money);
}