猜数字一个人玩多没意思,试试双人猜数字游戏吧

#include
#include
#include
int game(int player)
{
    printf("您当前的身份是player%d\n", player);
    int num = 0;
    int cont = 0;
    num = rand() % 100 + 1;
    int tmp = 0;
    switch (player)
    {
    case 1:
    {   
        int i = 0;
        while (1)
        {
            printf("请输入数字\n");
            scanf_s("%d", &cont);
            if (cont == num)
            {
                printf("恭喜,你猜的数字是对的\n");
                i++;
                break;
            }
            else if (cont < num)
            {
                printf("猜小了\n");
                i++;
            }
            else
            {
                printf("猜大了\n");
                i++;
            }
        }
        printf("您一共猜了%d次\n", i);
        tmp = i;
    }
    break;
    case 2:
    {
        int b = 0;
        while (1)
        {
            printf("请输入数字\n");
            scanf_s("%d", &cont);
            if (cont == num)
            {
                printf("恭喜,你猜的数字是对的\n");
                b++;
                break;
            }
            else if (cont < num)
            {
                printf("猜小了\n");
                b++;
            }
            else
            {
                printf("猜大了\n");
                b++;
            }
        }
        printf("您一共猜了%d次\n", b);
        cont = b;
    }

    if (tmp > cont)
    {
        printf("恭喜player2取得胜利\n");
    }
    else if (tmp == cont)
    {
        printf("平局\n");
    }
    else
    {
        printf("恭喜player1取得胜利\n");
    }
    break;
    }
}
int main()
{
    int choice=0;
    void srand(
        unsigned int seed
    );
    do
    {   
        printf("**************************************\n");
        printf("**********1. player1 2.player2   0.exit **********\n");
        printf("**************************************\n");
       printf("请输入选项\n");   
        scanf_s("%d", &choice);
        switch (choice)
        {
        case 1: game(choice);
            break;
        case 2: game(choice);
            break;
        case 0:
            break;
        default:
            printf("选择错误\n");
        }
    } while (choice);
    return 0;
}

你可能感兴趣的:(c语言基础)