编写函数模拟掷骰子的游戏(两个骰子)。第一次掷的时候,如果点数之和为7或11则获胜;如果点数之和为2、3或12则落败;其他情况下的点数之和称为“目标”,游戏继续。在后续的投掷中,如果玩家再次掷出“目标”点数则获胜,掷出7则落败,其他情况都忽略,游戏继续进行。每局游戏结束时,程序询问用户是否再玩一次,如果用户输入的回答不是y或Y,程序会显示胜败的次数然后终止。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int winCount, lostCount;
int first, second;
int target;
char input;
winCount=lostCount=0;
srand(time(0));
first=rand()%6+1;
second=rand()%6+1;
printf("%d %d\n",first,second);
target=first+second;
if(target==7 || target==11)
{
printf("you win\n");
winCount++;
}
else
{
if(target==2 || target==3 || target==12)
{
printf("you lost\n");
lostCount++;
}
else
{
printf("proceed or not?");
input=getchar();
while(input=='Y' || input=='y')
{
first=rand()%6+1;
second=rand()%6+1;
printf("%d %d\n",first,second);
if(target==first+second){
printf("you win\n");
winCount++;
}
else
if (first+second==7){
printf("you lost\n");
lostCount++;
}
printf("proceed or not?");
getchar();
input=getchar();
}
}
}
printf("win=%d, lost=%d\n",winCount, lostCount);
return 0;
}