C ++ Hangman Code无法正常工作

时间:2014-12-08 18:40:06

标签: c++

我的代码编译但它不允许用户正确猜出单词,当它显示正确的单词时,它只是一串废话。

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void    manual();
void    file(char*);
int letterFill(char, char*, char*);
void    Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
    bool done = false;
    char word[MAX_LENGTH];
    char unknown[MAX_LENGTH];
    char letter;
    char name[MAX_LENGTH];
    int wrong_guesses = 0;
    int MAX_TRIES;
    char ans;


    while (!done)
    {
        switch (instructions())
        {
        case 1:
        {
            manual();
            break;
        }
        case 2:
        {
            file(word);
            break;
        }
        }

        cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
        cin >> MAX_TRIES;
        Unknown(word, unknown);
        cout << endl << endl << "HANGMAN";
        cout << endl << endl << "Each letter is represented by a star." << endl;
        cout << "You have " << MAX_TRIES << " tries left.";
        cout << "ENTER GUESS WHEN READY: ";
            cin >> letter;

        while (letter != 'N' && letter != 'n')
        {

            while (wrong_guesses < MAX_TRIES)
            {


                cout << unknown << endl;
                cout << "Guess a letter: " << flush;
                cin >> letter;


                if (letterFill(letter, word, unknown) == 0)
                {
                    cout << endl << "You got it wrong! You lose a guess" << endl;
                    wrong_guesses++;
                }
                else
                {
                    cout << endl << "Pfft, you got lucky" << endl;
                }

                cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;


                if (strcmp(word, unknown) == 0)
                {
                    cout << word << endl;
                    cout << "You got it!" << endl;
                    exit(0);
                }
                cout << "You've been hanged." << endl;
                cout << "The word was : " << word << endl;
            }
        }

        cout << "Try again? (y/n):  " << flush;
        cin >> ans;
        if (ans == 'y' || ans == 'Y')
            done = true;
        else
            done = false;
    }
    system("pause");
}

int instructions()
{
    int select = 0;
    cout << endl << "HANGMAN" << endl << endl;
    cout << "     PROGRAM MENU" << endl;
    cout << "  Select option 1  or  2" << endl << endl;
    cout << "      1. INPUT WORD MANUALLY" << endl;
    cout << "      2. PLAY AGAINST THE COMPUTER" << endl;
    cout << "      3. EXIT PROGRAM BY INPUTING:  N  or  n" << endl << endl;
    cin >> select;
    return select;
}

void manual()
{
    string word;

    cout << endl << "INPUT WORD: " << endl;
    cin >> word;
    return;
}
void file(char *roc)
{
    ifstream fin("word.txt");
    int x;
    int count = 1;
    int word;
    int i = 0;


    srand(time(0));

    word = rand() % 20;

    while (count < word)
    {
        fin >> x;
        if (x == 0)
        {
            count++;
        }
    }


    do
    {
        fin >> x;
        roc[i++] = char(x);
    } while (x);
    return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
    int i;
    int matches = 0;

    for (i = 0; i<MAX_LENGTH; i++)
    {

        if (secretword[i] == 0)
        {
            break;
        }


        if (guess == guessword[i])
        {
            return 0;
        }

        if (guess == secretword[i])
        {
            guessword[i] = guess;
            matches++;
        }
    }
    return matches;
}

void Unknown(char *word, char *unknown)
{
    int i;
    int length = strlen(word);

    for (i = 0; i<length; i++)
    {
        unknown[i] = '*';
    }

    unknown[i] = 0;
}

再次 我的代码编译但它不允许用户正确猜出单词,当它显示正确的单词时,它只是一串废话。

0 个答案:

没有答案