Touge 23 matches mini game

mission details

The task of this level: match game.

related information

In order to complete this task, you need to master: C language foundation.

match game

23 sticks game. Please write a simple 23 match game program to realize the program of playing this game between human and computer. In order to facilitate the automatic evaluation of the program, it is assumed that the number of matches moved by the computer is not random, but the remaining number of matches is calculated as the remainder of 3 and then 1 is added as the number of matches taken by the computer each time. If the number of remaining matches is less than 3, subtract 1 from the number of remaining matches as the number of matches removed by the computer. The computer has to take it away. When the number of matches left is 1, one match must be taken away. Suppose the game rules are as follows:

  1. Both players start with 23 matches;

  1. Each player takes turns removing 1, 2 or 3 matches;

  1. Whoever takes the last match is the loser.

For example 1, an example of the running result of the player winning is as follows:

这里是23根火柴游戏!!
注意:最大移动火柴数目为三根
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:21
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:20
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:18
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:17
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:15
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:14
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:12
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:11
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:9
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:8
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:6
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:5
请输入您移动的火柴数目:
1↙
您移动的火柴数目为:1
您移动后剩下的火柴数目为:4
计算机移动的火柴数目为:2
计算机移动后剩下的火柴数目为:2
请输入您移动的火柴数目:
1↙
您移动的火柴数目为:1
您移动后剩下的火柴数目为:1
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:0
恭喜您!您赢了!

For example 2, the running example of the player losing is as follows:

这里是23根火柴游戏!!
注意:最大移动火柴数目为三根
请输入您移动的火柴数目:
3↙
您移动的火柴数目为:3
您移动后剩下的火柴数目为:20
计算机移动的火柴数目为:3
计算机移动后剩下的火柴数目为:17
请输入您移动的火柴数目:
3↙
您移动的火柴数目为:3
您移动后剩下的火柴数目为:14
计算机移动的火柴数目为:3
计算机移动后剩下的火柴数目为:11
请输入您移动的火柴数目:
3↙
您移动的火柴数目为:3
您移动后剩下的火柴数目为:8
计算机移动的火柴数目为:3
计算机移动后剩下的火柴数目为:5
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:3
计算机移动的火柴数目为:1
计算机移动后剩下的火柴数目为:2
请输入您移动的火柴数目:
2↙
您移动的火柴数目为:2
您移动后剩下的火柴数目为:0
对不起!您输了!

My code is as follows:

#include<stdio.h>
int main(void)
{
    int g = 23;
    int k = 3;
    int b, c;
    printf("这里是23根火柴游戏!!\n");
    printf("注意:最大移动火柴数目为三根\n");
    do{
        printf("请输入您移动的火柴数目:\n");
        scanf("%d", &b);
        /*************** Begin ***************/
        c=0;
        printf("您移动的火柴数目为:%d\n",b);
        g=g-b;
        if(c>0)
            g=g-c;
        printf("您移动后剩下的火柴数目为:%d\n",g);
        if(!g||g<0)
            {printf("对不起!您输了!");
            break;}
            else
            {

                if(g>=3||!g)
                    c=g%k+1;
                else
                    c=g-1;
                if(g>1)
                  {printf("计算机移动的火柴数目为:%d\n",c);
                 g=g-c;
                printf("计算机移动后剩下的火柴数目为:%d\n",g);}
              else
                   {printf("计算机移动的火柴数目为:1\n");
                 printf("计算机移动后剩下的火柴数目为:0\n",g);
            printf("恭喜您!您赢了!");
             break;}
            }
        /*************** End ***************/
    }while (g > 0);
    return 0;
}

I just learned it soon, and the method may be relatively stupid. If there is a better method, I hope everyone can share it with each other and make progress together.

Guess you like

Origin blog.csdn.net/wyulian/article/details/128958016