嗨,这是我在stackoverflow的第一篇文章,我正在用C创建一个骰子游戏,我很难在每轮后添加一个退出功能。一旦用户做出预测,游戏循环再次运行并重新调整4个新的骰子值。
我想要包含一个函数,询问玩家是想继续进行还是通过(y / n)输入退出。
下面是我的骰子游戏的代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main ()
{
while(1)
{
srand ( time(NULL) );
int i;
int DiceR[4]; // Dice Array
char prediction; //prediction input for high low or equal
while (1)
{
for (i=0; i<1; i++)
{
DiceR[0] = (rand()%6) + 1;
printf("\n%d \n", DiceR[0]);
DiceR[1] = (rand()%6) + 1;
printf("%d \n", DiceR[1]);
DiceR[2] = (rand()%6) + 1;
printf("%d \n", DiceR[2]);
DiceR[3] = (rand()%6) + 1;
}
printf("\nDo you think the next dice roll will be higher or lower than %d", DiceR[2]);
printf("\nPlease enter L = Lower E = Equal H = higher or X = Exit:\t");
scanf("\n%c", &prediction);
switch(prediction)
{
case 'L' : case 'l':
if (DiceR[3] < DiceR[2]) //Lower
printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
else
printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
break;
case 'E' : case 'e':
if (DiceR[3] == DiceR[2]) //Equal
printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
else
printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
break;
case 'H' : case 'h':
if (DiceR[3] > DiceR[2]) //Higher
printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
else
printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
break;
default:
printf("that value is not recognized");
break;
}
}
}
}
答案 0 :(得分:0)
您可能需要在第一个while
之前添加一个标记,例如,bool running = true;
,在您要退出的位置将其设置为false,然后在while
中检查而不是无限循环。
答案 1 :(得分:0)
您可以创建一个标志并使用条件(flag==true);
来保持代码在用户需要时运行
答案 2 :(得分:0)
我想包含一个询问玩家是否需要的功能 通过(y / n)输入继续前进或退出。
在每次滚动开始时要求用户继续或停止的功能
int cont()
{
char ch;
printf("\n want to continue or stop? y/n : \n");
scanf(" %c",&ch); //note a space before %c to consume white spaces
if(ch=='y'||ch=='Y')
return 0; //if yes i.e, 'y' return 0
return 1; //else return 1
}
以这种方式检查每次掷骰开始时函数的返回值:
(按照评论理解)
//#include .......
int cont()
{
char ch;
printf("\n want to start/continue? y/n : \n");
scanf(" %c",&ch);
if(ch=='y'||ch=='Y')
return 0;
return 1;
}
int main ()
{
int flag=0; //initialize flag with a value of zero
while(1) //first while loop
{
//srand and other variables initialization
while (1) //second while loop
{
//don't use if(cont()) here....
//for loop
//printing and scanning prediction
switch(prediction)
{
//all other cases........
case 'X': case 'x': //you seem to have forgotten mentioning this case :)
flag=1;
break;
//default case
}
//use it here instead sso that you'd get it at the end of each turn
if(cont()) //function call : here you ask user for y/n input
{
//if user enter no i.e, 'n', then you enter the if block
flag=1;
break;
}
if (flag==1) //checking for flag; useful when user enters 'X'
break; //break out of first while loop
}
if(flag==1) //checking for flag
break; //break out of second while loop
}
}
答案 3 :(得分:0)
这是一个经过纠正的&#39;代码的版本。
请注意,将调用值打印到rand()
可能不是最佳方式,尤其是当用户玩游戏一段时间并识别模式时
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//#include <string.h>
//#include <math.h>
#include <time.h>
#define NUM_OF_DICE (4)
#define MAX_DICE_VALUE (6)
int main ()
{
srand ((unsigned) time(NULL) );
int DiceR[ NUM_OF_DICE ]; // Dice Array
char prediction; //prediction input for high low or equal
while (1)
{
for( size_t i=0; i<NUM_OF_DICE; i++)
{
DiceR[i] = (rand()%MAX_DICE_VALUE) + 1;
printf("\n%d \n", DiceR[i]);
}
printf("\nDo you think the next dice roll will be higher or lower than %d", DiceR[2]);
printf("\nPlease enter L = Lower E = Equal H = higher or X = Exit:\t");
if( 1 != scanf("\n%c", &prediction) )
{ // then scanf failed
perror( "scanf for inputting prediction failed");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
prediction = (char)toupper(prediction);
if( 'X' == prediction )
{
return 0;
}
switch(prediction)
{
case 'L' :
if (DiceR[3] < DiceR[2]) //Lower
printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
else
printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
break;
case 'E' :
if (DiceR[3] == DiceR[2]) //Equal
printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
else
printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
break;
case 'H' :
if (DiceR[3] > DiceR[2]) //Higher
printf("\ncongrats DiceRoll 4 is %d\n", DiceR[3]); //win
else
printf("\nYou lose, the last roll was %d\n", DiceR[3]); //Lose
break;
default:
printf("input must be 'L' or 'H' or 'E' or 'X'\n");
break;
} // end switch
}
} // end function: main