[英]Quit function for C dice game
嗨这是我的第一篇文章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;
}
}
}
}
你可能想在第一之前,此标志while
一样, bool running = true;
,当你想要退出时将它设置为false,然后在while
s中检查它while
不是无限循环。
您可以创建一个标志并使用条件(flag==true);
在用户需要时保持代码运行
我想要包含一个函数,询问玩家是否想要继续或通过(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
}
}
这是代码的“更正”版本。
注意,打印调用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.