点击(此处)折叠或打开
- #include <stdio.h>
- #define SIZE 4
-
-
- /* function prototype */
- void introduction(void); /* game introduction */
- void judgement(int[], int[]);
-
- int main(void){
- int goal[SIZE] = { 1, 2, 3, 4 };
- int guess[SIZE];
-
- introduction(); /* title: illustrate how to play the game */
-
-
- if ( getchar() == 'y' ){
- printf("Okey! Let`s start\n");
-
- int input_num;
- while (scanf_s("%d", &input_num) != EOF){
- /* Use the every bits of four digits, assigned
- to the array guess`s element, respectively */
- guess[0] = input_num / 1000;
- guess[1] = (input_num - guess[0] * 1000) / 100;
- guess[2] = (input_num - (guess[0] * 1000 + guess[1] * 100)) / 10;
- guess[3] = input_num - (guess[0] * 1000 + guess[1] * 100 + guess[2] * 10);
- /* print the number just input */
- printf("%d%d%d%d", guess[0], guess[1], guess[2], guess[3]);
- /* call the judgement function */
- judgement(goal, guess);
- }
- }
- else{
- printf("Okey! See you next time, Bye!\n");
- }
-
- return 0;
- }
-
- /* only print the illustrate of the game on screen */
- void introduction(void){
- printf("********************* GAME INTRODUCTION *********************\n\n"
- "There is a number composed by 4 figures(0~9), and without repetition.\n"
- "Guess the number. If the figure and the position all correct, then\n"
- "output A, if the figure is correct but the positon is incorrect,\n"
- "then output B, the goal is 4A0B. OKey!! Come on baby!!!\n"
- "Are you ready?(y/n):");
- }
-
- /* judge the goal number and the guess number matching or not */
- void judgement(int judge_goal[SIZE], int judge_guess[SIZE]){
- int a = 0, b = 0;/* counter */
- /* count the number and the position all correct */
- for (int i = 0; i < SIZE; i++){
- if (judge_goal[i] == judge_guess[i]){
- a++;
- }
- /* count the number correct (if the number matching, then b+1)*/
- for (int j = 0; j < SIZE; j++){
- if (judge_goal[i] == judge_guess[j])
- b++;
- }
- }
- /* b include a, so b-a */
- b = b - a;
- printf("------%d数字、位置都正确; %d数字正确、但位置不正确\n", a, b);
- }
相关热门文章
给主人留下些什么吧!~~
评论热议