发布于2021-07-24 21:52 阅读(988) 评论(0) 点赞(12) 收藏(1)
程序说明:
编写一个程序实现猜数字大小的游戏。由程序随机生成一个数字。玩家不断程序的提示下输入猜测的数字,然后游戏者在猜数,程序可实现连续猜数,直到游戏者退出。
程序输出要求:
设计任务要求一:
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,NUM;
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')
{
//玩家输入S开始游戏 ,E返回上一层
printf(" ");
scanf("%s",&start);
while(start=='S'&&start!='E')
{
//各玩家分数,胜场数,游戏总次数清零
winner1=0,winner2=0,score1=0,score2=0,num=1,i=1,NUM=1;
while(i=1)
{
//清屏命令
system("cls");
//预输出初始分数设置
score=100;
if(num%2==1)
{
//玩家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)
{
//玩家2开始游戏界面打印
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");
}
//随机获取一个1到100的数字
srand((unsigned)time(NULL));
x=rand() % 100+1;
//玩家输入数值y与答案x比较的循环,分数小于0或输入值y与答案x相同时循环结束
while(score>0)
{
printf(" Please enter your guess: ");
//用户输入y
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) //若输入的数偏小,则输出提示:“The answer is bigger,try again!”
{
printf(" The answer is smaller,try again!\n");
printf("\n");
score=score-4;
}
else if(y>x) //若输入偏大输出提示:“The answer is smaller,try again!”
{
printf(" The answer is bigger,try again!\n");
printf("\n");
score=score-4;
}
else if(y==x) //猜对时输出“You are right!”
{
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;
//分数置零,游戏回合+1,跳出当前循环,切换玩家
score=0,num++;
printf("Enter any key to continue the game: ");
scanf("%s",&judge);
}
//根据分数给予相应的提示
if(score>=80) //当score>=80时,给出“Congratulations!”
{
printf(" Congratulations!\n");
printf("\n");
}
else if(score<80&&score>=60) //当60<=score<80时,给出“You can do it better!”
{
printf(" You can do it better!\n");
printf("\n");
}
else if(score<60) //当score<60时,提示You failed!
{
printf(" You failed!\n");
printf("\n");
}
//游戏总次数,最高分打印
printf(" Total number of games:%d\n Highest score:%d\n\n\n\n",NUM,max);
NUM++;
}
if(score1>score2)
{
winner1++;
printf("Player 1 wins\n");
}
else if(score<score2)
{
winner2++;
}
if((winner1-winner2==2)||num==6||(winner2-winner1==2))
{
i=0;
printf("The game is over\n");
if(winner1>winner2)
{
printf("Player 1 wins\n");
}
else if(winner2>winner1)
{
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')
{
//玩家输入S开始游戏 ,E返回上一层
printf(" ");
scanf("%s",&start);
while(start=='S'&&start!='E')
{
printf("\n");
while(1)
{
//清屏命令
system("cls");
//预打印次数,分数赋值
num=0,score=100;
//随机获取一个1到100的数字
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");
}
num++;
//游戏总次数,最高分打印
printf(" Total number of games:%d\n Highest score:%d\n\n\n\n",num,max);
}
}
}
printf("\n");
}
}
}
}
作者:小飞刀你有点飘
链接:http://www.pythonpdf.com/blog/article/372/b452d9c90c93c5710aeb/
来源:编程知识网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!