UVa489 - Hangman Judge

#include 
#include 
using namespace std;

string a[3] = {"You win.", "You lose.", "You chickened out."};
int chance, lef;
string s1, s2;

void hangman(char c)
{
    int len = s1.length();
    bool flag = true;
    for (int i = 0; i < len; i++)
    {
        if (s1[i] == c)
        {
            s1[i] = ' ';
            flag = false;
            lef -= 1;
        }
    }
    if (flag)
    {
        chance--;
        int len1 = s2.length();
        for (int i = 0; i < len1; i++)
        {
            if (s2[i] == c)
                s2[i] = ' ';
        }
    }
    return;
}

int main()
{
    int n;
    while (cin >> n && n != -1)
    {
        cout << "Round " << n << endl;
        cin >> s1 >> s2;
        int len = s2.length();
        lef = s1.length();
        chance = 7;
        for (int i = 0; i < len && lef; i++)
        {
            if (s2[i] == ' ')
                continue;
            hangman(s2[i]);
        }
        if (chance <= 0)
        {
            cout << a[1] << endl;
        }
        else
        {
            bool flag = true;
            int len1 = s1.length();
            for (int i = 0; i < len1; i++)
            {
                if (s1[i] != ' ')
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                cout << a[0] << endl;
            }
            else
                cout << a[2] << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(c++,算法,开发语言)