Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99338
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1011
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-15 13:58
个人简介

普普通通一个人

文章分类

全部博文(102)

文章存档

2018年(1)

2015年(13)

2014年(88)

我的朋友

分类: C/C++

2014-02-05 09:46:32


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. #define SIZE 4

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

  8. int main(void){
  9.     int goal[SIZE];
  10.     int guess[SIZE];
  11.     /* title: illustrate how to play the game */
  12.     introduction();

  13.     
  14.     if ( getchar() == 'y' ){
  15.         srand(time(NULL));
  16.         /* assign the array goal`s elem */
  17.         for (int i = 0; i < SIZE; i++){
  18.             goal[i] = rand() % 10;
  19.             /* judge the duplicate elem */
  20.             for (int j = 0; j < i; j++){
  21.                 while (goal[i] == goal[j]){
  22.                     goal[i] = rand() % 9;
  23.                     j = 0;
  24.                 }
  25.             }
  26.         }
  27.         printf("Okey! The number has been ready!\n");
  28. //        printf("%d%d%d%d", goal[0], goal[1], goal[2], goal[3]); /* debug */
  29.         
  30.         int input_num;
  31.         while (scanf_s("%d", &input_num) != EOF){
  32.             /* Use the every bits of four digits, assigned
  33.             to the array guess`s element, respectively */
  34.             guess[0] = input_num / 1000;
  35.             guess[1] = (input_num - guess[0] * 1000) / 100;
  36.             guess[2] = (input_num - (guess[0] * 1000 + guess[1] * 100)) / 10;
  37.             guess[3] = input_num - (guess[0] * 1000 + guess[1] * 100 + guess[2] * 10);
  38.             /* print the number just input */
  39.             printf("%d%d%d%d", guess[0], guess[1], guess[2], guess[3]);
  40.             /* call the judgement function */
  41.             judgement(goal, guess);
  42.         }
  43.     }
  44.     else{
  45.         printf("Okey! See you next time, Bye!\n");
  46.     }
  47.     
  48.     return 0;
  49. }

  50. /* only print the illustrate of the game on screen */
  51. void introduction(void){
  52.      printf("********************* GAME INTRODUCTION *********************\n\n"
  53.          "There is a number composed by 4 figures(0~9), and without repetition.\n"
  54.          "Guess the number. If the figure and the position all correct, then\n"
  55.          "output A, if the figure is correct but the positon is incorrect,\n"
  56.          "then output B, the goal is 4A0B. OKey!! Come on baby!!!\n"
  57.          "Are you ready?(y/n):");
  58. }

  59. /* judge the goal number and the guess number matching or not */
  60. void judgement(int judge_goal[SIZE], int judge_guess[SIZE]){
  61.     int a = 0, b = 0;/* counter */
  62.     /* count the number and the position all correct */
  63.     for (int i = 0; i < SIZE; i++){
  64.         if (judge_goal[i] == judge_guess[i]){
  65.             a++;
  66.         }
  67.         /* count the number correct (if the number matching, then b+1)*/
  68.         for (int j = 0; j < SIZE; j++){
  69.             if (judge_goal[i] == judge_guess[j])
  70.                 b++;
  71.         }
  72.     }
  73.     /* b include a, so b-a */
  74.     b = b - a;
  75.     printf("------%d数字、位置都正确; %d数字正确、但位置不正确\n", a, b);
  76. }

阅读(252) | 评论(0) | 转发(0) |
0

上一篇:猜数字小游戏 v1

下一篇:猜数字小游戏 v3

给主人留下些什么吧!~~