- #include <stdio.h>
- #include <stdlib.h>
- #include <process.h>
- #include <time.h>
- int main()
- {
- printf("\\t\\t * *\\n");
- printf("\\t\\t * *\\n");
- printf("\\t\\t * *\\n");
- printf("\\t\\t *\\n");
- printf("\\t\\t *\\n");
- printf("\\t\\t *\\n");
- printf("\\t\\t *\\n");
- printf("\\n");
- printf("\\t\\t *\\n");
- printf("This is a number guessing game.\\nJust guess and enjoy it!\\nYou should input a number like 1234\\n");
- system("pause");
- //游戏提示
- int answer[4],a=0,b=0,i=0,j=0,guess,g[4],times=0,times_limit=12,win=0,lose=0,best_grade=13,round=0,alter1=1,alter2=1,rank=0,score=0;
- char face;
- //变量声明
- do{
- round++;
- times=0;
- system("cls");
- printf("Round:%d\\tRank:%d\\tScore:%d\\tBest grade:%d\\n",round,rank,score,best_grade);
- printf("You will have %d chances to give your answer in this round\\n",times_limit);
- //round初始化
- for(i=0;i<4;i++)
- {
- srand((unsigned)time(NULL));
- answer[i]=rand()%10;
- for(j=0;j<i;j++)
- {
- if(answer[j]==answer[i])
- {
- answer[i]=rand()%10;
- j=-1;
- }
- }
- }
- //随机生成4位answer
- //测试行printf("The answer is %d%d%d%d\\n",answer[0],answer[1],answer[2],answer[3]);
- do{
- times++;
- printf("%2d\\t",times);
- a=0;
- b=0;
- //计次,a,b单次初始化
- scanf("%d",&guess);
- g[0]=guess/1000;
- g[1]=(guess-g[0]*1000)/100;
- g[2]=(guess-g[0]*1000-g[1]*100)/10;
- g[3]=guess-g[0]*1000-g[1]*100-g[2]*10;
- //输入求guess各位数字
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- b=b+(answer[i]==g[j]&&i!=j);
- a=a+(answer[i]==g[j]&&i==j);
- }
- }
- if(a!=4) face=1;
- else face=2;
- printf("\\t%dA%dB\\t%c\\n",a,b,face);
- //比较求a,b并输出比较结果
- if(a<4&×==times_limit&&score>0)
- {
- printf("Do you want to get an extra chance with one score?\\ny(1)/n(0)\\n");
- scanf("%d",&alter1);
- if(alter1==1)
- {
- times--;
- score--;
- }
- }
- //用分数换次数
- }while(a!=4&×<times_limit);
- //处理给出答案直至答案正确或机会用完
- if(a==4)
- {
- printf("Congratulations!\\nYou are right!\\n");
- win++;
- if(times<best_grade) best_grade=times;
- rank++;
- times_limit--;
- score++;
- }
- else
- {
- printf("Sorry.\\nYou are wrong!\\nThe answer is %d%d%d%d\\n",answer[0],answer[1],answer[2],answer[3]);
- lose++;
- }
- printf("Round:%d\\tWin:%d\\tLose:%d\\tScore:%d\\tRank:%d\\tBest grade:%d\\n",round,win,lose,score,rank,best_grade);
- //结果处理
- if(rank<10)
- {
- printf("Continue?\\ny(1)/n(0)\\n");
- scanf("%d",&alter2);
- }
- }while(alter2==1&&rank<10);
- //等级至顶前选择是否继续
- system("cls");
- printf("Game over!\\n");
- printf("Win:%d\\tLose:%d\\tRank:%d\\tBest grade:%d\\n",win,lose,rank,best_grade);
- system("pause");
- return 0;
- //结束处理
- }
- //该片段来自于http://www.codesnippet.cn/detail/1808201410219.html
来源: http://www.codesnippet.cn/detail/1808201410219.html