UVa 489:Hangman Judge

题目传送门:UVa 489:Hangman Judge

#include <stdio.h>
#include <string.h>
char ans[100], gus[100];
int left, chance; // left剩余未被猜出的字符数,错误chance次就会输
bool win, lose;

void guess(char ch)
{
    bool right = false;
    for (int i = 0; i < ans[i]; ++i)
        if (ans[i] == ch) { left--; ans[i] = '*'; right = true; }
    if (!right) --chance;
    if (0 == chance) lose = true;
    if (0 == left) win = true;
}

int main()
{
    int rnd, lengus;
    while (scanf("%d%s%s", &rnd, ans, gus) == 3 && rnd != -1)
    {
        printf("Round %d\n", rnd);
        win = lose = false;
        chance = 7;
        left = strlen(ans);
        lengus = strlen(gus);
        for (int i = 0; i < lengus; ++i)
        {
            guess(gus[i]);
            if (win || lose) break;
        }
        if (win) printf("You win.\n");
        else if (lose) printf("You lose.\n");
        else printf("You chickened out.\n");
    }
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页