C语言实现双人猜数游戏
程序说明: 编写一个程序实现猜数字大小的游戏。由程序随机生成一个数字。玩家不断程序的提示下输入猜测的数字,然后游戏者在猜数,程序可实现连续猜数,直到游戏者退出。
程序输出要求:
设计任务要求一: 1)游戏界面。游戏的开始当按下‘S’,则游戏开始产生并一个随机正整数[1~100]。该随机
数需要用库函数random()生成。然后用户不断输入猜测的数字,直到才对为止。
2)若输入的数偏小,则输出提示:“The answer is bigger,try again!”,否则输出提示:
“The answer is smaller,try again!”。直到猜对输出“You are right!”。
3)最高得分统计功能。用玩家猜对一个数所需次数来计算成绩,第一次猜对为100分,每
多一次减去4分。当n>=80时,给出“Congratulations!”字样;当60<=n<80时,给出
“You can do it better!”。当n<60时,提示You failed!。通过多次游戏的进行,每次得分后
总是提示游戏总次数和最高得分。
设计任务要求二:
4)双人游戏功能。增加双人比赛模式,三局两胜制为一个回合。一回合后系统自动计算每位得分,并提示得胜方。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int x , y , score , max , i ,ret, mode;
char start, judge;
int num,score1,score2,winner1,winner2;
max=0;
while(1)
{
printf(" ********* Guessing games *********\n");
printf(" ***** *****\n");
printf(" *** ***\n");
printf(" * *\n\n");
printf(" >> ONE PLAYERS(Enter '1')<<\n");
printf(" >> TEO PLAYERS(Enter '2')<<\n");
ret=scanf("%d",&mode);
while(ret!=1)
{
while(getchar()!='\n');
printf("please enter a number!\n");
ret=scanf("%d",&mode);
}
if(mode==2)
{
printf(" >> Enter 'S' to start the game<< \n\n");
while(start!='E')
{
printf(" ");
scanf("%s",&start);
while(start=='S'&&start!='E')
{
winner1=0,winner2=0,score1=0,score2=0,num=0,i=1;
while(i=1)
{
system("cls");
score=100;
num++;
if(num%2==1)
{
printf(" * Player 1 *\n");
printf(" *** ***\n");
printf(" ***** *****\n");
printf(" ********* The game beging *********\n");
printf(" ***** *****\n");
printf(" *** ***\n");
printf(" * Please enter a number from 1 to 100 *\n\n");
}
else if(num%2==0)
{
printf(" * Player 2 *\n");
printf(" *** ***\n");
printf(" ***** *****\n");
printf(" ********* The game beging *********\n");
printf(" ***** *****\n");
printf(" *** ***\n");
printf(" * Please enter a number from 1 to 100 *\n\n");
}
else
{
printf("error\n");
}
srand((unsigned)time(NULL));
x=rand() % 100+1;
while(score>0)
{
printf(" Please enter your guess: ");
ret=scanf("%d",&y);
printf("\n");
while(ret!=1)
{
while(getchar()!='\n');
printf("please enter a number!\n");
ret=scanf("%d",&y);
}
if(y==999)
printf("answer:%d\n",x);
if(y<x)
{
printf(" The answer is smaller,try again!\n");
printf("\n");
score=score-4;
}
else if(y>x)
{
printf(" The answer is bigger,try again!\n");
printf("\n");
score=score-4;
}
else if(y==x)
{
printf(" You are right!\n\n");
printf(" Your score:%d\n\n\n",score);
if(score>max)
max=score;
if(num%2==1)
score1+=score;
else if(num%2==0)
score2+=score;
score=0;
printf("Enter any key to continue the game: ");
scanf("%s",&judge);
}
if(score>=80)
{
printf(" Congratulations!\n");
printf("\n");
}
else if(score<80&&score>=60)
{
printf(" You can do it better!\n");
printf("\n");
}
else if(score<60)
{
printf(" You failed!\n");
printf("\n");
}
printf(" Total number of games:%d\n Highest score:%d\n\n\n\n",num,max);
}
if(score1>score2)
{
winner1++;
printf("Player 1 wins");
}
else if(score<score2)
{
winner2++;
}
if((winner1-winner2==2)||num==6||(winner2-winner1==2))
{
i=0;
printf("The game is over");
if(winner1>winner2)
{
printf("Player 1 wins\n");
}
else if(winner2>winner1==2)
{
printf("Player 2 wins\n");
}
else
{
printf("it ends in a draw\n");
}
printf("Player 1's score:%d\n",score1);
printf("Player 2's score:%d\n\n",score2);
printf("Enter any key to continue the game or Enter 'E' to exit the game: ");
scanf("%s",&judge);
if(judge!='E')
{
start='E';
}
else
{
return 0;
}
}
}
}
printf("\n");
}
}
else if (mode==1)
{
printf(" >> Enter 'S' to start the game<< \n\n");
while(start!='E')
{
printf(" ");
scanf("%s",&start);
while(start=='S'&&start!='E')
{
printf("\n");
while(1)
{
system("cls");
num=0,score=100;
num++;
srand((unsigned)time(NULL));
x=rand() % 100+1;
printf(" * *\n");
printf(" *** ***\n");
printf(" ***** *****\n");
printf(" ********* The game beging *********\n");
printf(" ***** *****\n");
printf(" *** ***\n");
printf(" * Please enter a number from 1 to 100 *\n\n");
while(score>0)
{
printf(" Please enter your guess: ");
ret=scanf("%d",&y);
printf("\n");
while(ret!=1)
{
while(getchar()!='\n');
printf("please enter a number!\n");
ret=scanf("%d",&y);
}
if(y==999)
printf("answer:%d\n",x);
if(y<x)
{
printf(" The answer is smaller,try again!\n");
printf("\n");
score=score-4;
}
else if(y>x)
{
printf(" The answer is bigger,try again!\n");
printf("\n");
score=score-4;
}
else if(y==x)
{
printf(" You are right!\n\n");
printf(" Your score:%d\n\n\n",score);
if(score>max)
max=score;
printf("Enter any other key to continue the game or Enter 'E' to exit the game: \n");
printf(" ");
scanf("%s",&judge);
if(judge!='E')
{
score=0,start='E';
}
else
{
return 0;
}
}
if(score>=80)
{
printf(" Congratulations!\n");
printf("\n");
}
else if(score<80&&score>=60)
{
printf(" You can do it better!\n");
printf("\n");
}
else if(score<60)
{
printf(" You failed!\n");
printf("\n");
}
printf(" Total number of games:%d\n Highest score:%d\n\n\n\n",num,max);
}
}
}
printf("\n");
}
}
}
}
|