猜数字小游戏 v1


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #define SIZE 4


  3. /* function prototype */
  4. void introduction(void); /* game introduction */
  5. void judgement(int[], int[]);

  6. int main(void){
  7.     int goal[SIZE] = { 1, 2, 3, 4 };
  8.     int guess[SIZE];

  9.     introduction(); /* title: illustrate how to play the game */

  10.     
  11.     if ( getchar() == 'y' ){
  12.         printf("Okey! Let`s start\n");
  13.         
  14.         int input_num;
  15.         while (scanf_s("%d", &input_num) != EOF){
  16.             /* Use the every bits of four digits, assigned
  17.             to the array guess`s element, respectively */
  18.             guess[0] = input_num / 1000;
  19.             guess[1] = (input_num - guess[0] * 1000) / 100;
  20.             guess[2] = (input_num - (guess[0] * 1000 + guess[1] * 100)) / 10;
  21.             guess[3] = input_num - (guess[0] * 1000 + guess[1] * 100 + guess[2] * 10);
  22.             /* print the number just input */
  23.             printf("%d%d%d%d", guess[0], guess[1], guess[2], guess[3]);
  24.             /* call the judgement function */
  25.             judgement(goal, guess);
  26.         }
  27.     }
  28.     else{
  29.         printf("Okey! See you next time, Bye!\n");
  30.     }
  31.     
  32.     return 0;
  33. }

  34. /* only print the illustrate of the game on screen */
  35. void introduction(void){
  36.      printf("********************* GAME INTRODUCTION *********************\n\n"
  37.          "There is a number composed by 4 figures(0~9), and without repetition.\n"
  38.          "Guess the number. If the figure and the position all correct, then\n"
  39.          "output A, if the figure is correct but the positon is incorrect,\n"
  40.          "then output B, the goal is 4A0B. OKey!! Come on baby!!!\n"
  41.          "Are you ready?(y/n):");
  42. }

  43. /* judge the goal number and the guess number matching or not */
  44. void judgement(int judge_goal[SIZE], int judge_guess[SIZE]){
  45.     int a = 0, b = 0;/* counter */
  46.     /* count the number and the position all correct */
  47.     for (int i = 0; i < SIZE; i++){
  48.         if (judge_goal[i] == judge_guess[i]){
  49.             a++;
  50.         }
  51.         /* count the number correct (if the number matching, then b+1)*/
  52.         for (int j = 0; j < SIZE; j++){
  53.             if (judge_goal[i] == judge_guess[j])
  54.                 b++;
  55.         }
  56.     }
  57.     /* b include a, so b-a */
  58.     b = b - a;
  59.     printf("------%d数字、位置都正确; %d数字正确、但位置不正确\n", a, b);
  60. }

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(4) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值