UVa489 侩子手游戏

这个就没什么需要注意的了,比较简单。

主要是模块化函数。

//UVa 489 start:2025 5.2. end 5.2
#include <stdio.h>
#include <string.h>
#define maxn 200

char r_word[maxn];
char t_word[maxn];
char text[maxn];
// 该函数作用:通过判断字母与单词重复性更新可显示单词,并更新命的数量;返回单词是否全部猜出来;
int check_word(char *r_word, char *t_word, char a, int *chance)
{

    int lenr = strlen(r_word);
    //x指r和t是否相同;y指chance是否要-1;
    int x, y = 0;
    //判断该字符是否出现在rword中
    for (int i = 0; i < lenr; i++)
    {
        if (a == r_word[i])
        {
            t_word[i] = r_word[i];
            y = 1;
        }
    }
    //x指是否全对
    x = strcmp(t_word, r_word);

    if (!y && x == 0)
    {
        (*chance)--;
        return 1;
    }
    if (!y && x != 0)
    {
        (*chance)--;
        return 0;
    }
    if (y && x == 0)
    {
        return 1;
    }

    if (y && x != 0)
    {
        return 0;
    }
}
int main()
{
    int a = 0;
    
    while (1)
    {
        memset(r_word, '\0', sizeof(r_word));    
        memset(text, '\0', sizeof(text));
        memset(t_word, '\0', sizeof(t_word));
        scanf("%d", &a);
        printf("%d\n",a);
        if (a == -1)
            break;
        // 样例输入
        scanf("%s", r_word);
        scanf("%s", text);
        int lent = strlen(text), chance = 7;

        // 逐个字母判断,并更新状态;
        int i = 0;
        for (int j = 0; i < lent; i++)
        {
            //猜测的字符串中当出现重复字符时,直接-1,不需要执行后面的检测了,while中的条件 i指代的是,保证第一个(i = 0时)不需要执行该操作
            while (i && !(memchr(text, text[i], i) == NULL))
            {
                chance--;
                i++;
            }
            if (i < lent)
            {
                j = check_word(r_word, t_word, text[i], &chance);
                if (j && chance)
                {
                    printf("You win.\n");
                    break;
                }
                if (!chance)
                {
                    printf("You lose.\n");
                    break;
                }
            }
        }
        if (chance && i == lent)
            printf("You chickened out.\n");
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值