zoukankan      html  css  js  c++  java
  • C语言之猜数字游戏

    猜数字游戏

    猜数字游戏是以前功能机上的一款益智游戏,计算机会根据输入的位数随机分配一个符合要求的数据,计算机输出guess后便可以输入数字,注意数字间需要用空格或回车符加以区分,计算机会根据输入信息给出相应的提示信息:A表示位置与数字均正确的个数,B表示位置不正确但数字争取的个数,这样便可以根据提示输入,直到正确为止,并且根据输入次数给出相应评价。

    源代码如下:

    1. #include<stdio.h>
    2. #include<time.h>
    3. #include<stdlib.h>
    4. void guess(int);
    5. int main(void)
    6. {
    7.  int i,n;
    8.   system("cls");                                              //清屏
    9.   printf("1.start game?(y/n) ");
    10.   printf("2.Rule ");
    11.   printf("3.exit ");
    12.   printf("please choose:");
    13.   scanf("%d",&i);
    14.  while(i!=3)                                                   //循环结构
    15.  {
    16.   switch(i)
    17.   {
    18.    case 1:
    19.                    printf("please input n: ");
    20.                    scanf("%d",&n);
    21.                    guess(n);
    22.                    break;
    23.             case 2:
    24.                    printf(" The rules of the game ");
    25.                    printf("step1:input the number of digits ");
    26.                    printf("step2:input the number,separated by a space between two numbers. ");
    27.                    break;
    28.             default:
    29.                        break;
    30.   }
    31.   scanf("%d",&i);
    32.  }
    33. }
    34. void guess(int n)
    35. {
    36.  int acount,bcount,i,j,k=0,flag,a[10],b[10];
    37.  do
    38.  {
    39.   flag=0;
    40.   srand((unsigned long)time(0));
    41.   for(i=0;i<n;i++)
    42.   a[i]=rand()%10;
    43.   for(i=0;i<n-1;i++)
    44.   {
    45.    for(j=i+1;j<=n;j++)
    46.    if(a[i]==a[j])                                        //不能出现相同的数字
    47.    {
    48.     flag=1;
    49.     break;
    50.    }
    51.   }
    52.  }while(flag==1);
    53.  do
    54.  {
    55.   k++;
    56.   acount=0;
    57.   bcount=0;
    58.   printf("guess:");
    59.   for(i=0;i<n;i++)
    60.   scanf("%d",&b[i]);                              //输入测试数据
    61.   for(i=0;i<n;i++)
    62.       for(j=0;j<n;j++)
    63.       {
    64.        if(a[i]==b[i])                                 //位置和数字都相同
    65.        {
    66.         acount++;
    67.         break;
    68.        }
    69.        if(a[i]==b[j]&&i!=j)                      //数字相同,位置不同
    70.        {
    71.         bcount++;
    72.         break;
    73.        }
    74.       }
    75.   printf("clue on:%dA%d B ",acount,bcount);
    76.   if(acount==n)                                    //给出评价
    77.   {
    78.    if(k==1)
    79.    printf("you are the topmose ");
    80.    else if(k<=5)
    81.    printf("you are genius! ");
    82.    else if(k<=10)
    83.    printf("you are cleaver! ");
    84.    else
    85.    printf("you need try hard! ");
    86.    break;
    87.   }
    88.  }while(1);
    89. }

    效果图:

  • 相关阅读:
    微信小程序开发(十)获取手机的经纬度
    微信小程序开发(九)获取手机连接的wifi信息
    微信小程序开发(八)获取手机ip地址
    微信小程序开发(七)获取手机网络类型
    微信小程序开发(六)获取手机信息
    关于JPype报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误的解决
    微信小程序开发(五)数据绑定
    微信小程序开发(四)页面跳转
    微信小程序开发(三)点击事件
    微信小程序开发(二)创建一个小程序页面
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3155682.html
Copyright © 2011-2022 走看看