man子手只得到秘密单词的第一个字母

时间:2019-03-05 19:40:05

标签: c++

我正在尝试编写一个执行任务的子手程序。我已经写了我认为可以正常工作的代码,但是用“不可能”这个秘密词测试它时,它只会读取“ I”,而没有其他内容。我尝试将所有字符串更改为字符列表,但我认为这不是问题。有人对我做错的事情有任何建议吗? 谢谢, 基思 这是代码:

/* CSCI 261 Assignment 5: Hang Man Game
 *
 * Author: Keith Danielson
 *
 * A program that runs a simple hang man game
 */

// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <string>

// We will (most of the time) use the standard library namespace in our programs.
using namespace std;

//Defining the secret word as a constant
//const string SECRET_WORD = "IMPOSSIBLE";

int main() {

    const char SECRET_WORD[10] = {'I','M','P','O','S','S','I','B','L','E'};
    const int SECRET_WORD_LENGTH = 10;

    //Defining the number of wrong guesses available, found letters, wrong guesses, and user choice.
    int guesses = 7;
    char foundLetters[SECRET_WORD_LENGTH];
    char wrongGuesses[guesses];
    char userChoice;


    //Filling foundLetters with underslashes based on the length of the secret word.
    for (int i = 0; i <= SECRET_WORD_LENGTH; i++) {
        foundLetters[i] = '_';
    }

    cout << "Welcome to hangman!" << endl;

    for (int i = 0; i <= 7; i++) {
        if (guesses == 0){
            break;
        }
        cout << "Take a guess: ";

        for (int j = 0; j <= SECRET_WORD_LENGTH; j++) {
            cout << foundLetters[j] << " ";
        }

        cout << "\n" << "Your guess: ";
        cin >> userChoice;

        //if the user input is lowercase it'll be made upper case.
        if (islower(userChoice)) {
            userChoice = toupper(userChoice);
        }

        for (int j = 0; j <= SECRET_WORD_LENGTH; j++) {

            //if (userChoice == foundLetters[j]) {
             //   cout << "You already guessed" << userChoice << "." << endl;
            //    break;
            //}
            if (userChoice == SECRET_WORD[j]) {
                cout << "There's a " << userChoice << "!" << endl;
                foundLetters[j] = userChoice;
                break;
            }
            else if (userChoice != SECRET_WORD[j]) {
                guesses = guesses - 1;
                cout << "Sorry. No " << userChoice << "'s." << endl;
                wrongGuesses[i] = userChoice;
                if (guesses == 0) {
                    cout << "You lose! Try again.";
                    break;
                }
                else {
                    cout << "You have " << guesses << " remaining." << endl;
                    break;
                }
            }

        }

    }

    return 0; // signals the operating system that our program ended OK.
}

1 个答案:

答案 0 :(得分:0)

请尝试以下类似操作:

// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <string>
#include <limits>

// We will (most of the time) use the standard library namespace in our programs.
using namespace std;

//Defining the secret word as a constant
const char SECRET_WORD[10] = {'I','M','P','O','S','S','I','B','L','E'};
const int SECRET_WORD_LENGTH = 10;

//Defining the number of wrong guesses available
const int WRONG_GUESSES = 7;

bool hasLetter(const char *letters, int numLetters, char ch) {
    for (int i = 0; i < numLetters; ++i) {
        if (letters[i] == ch) {
            return true;
        }
    }
    return false;
}

int main() {

    char foundLetters[SECRET_WORD_LENGTH];
    int foundLetters = 0;

    char wrongLetters[WRONG_GUESSES];
    int wrongLettersLength = 0;

    int wrongGuessesLeft = WRONG_GUESSES;
    char userChoice;
    int found;

    //Filling foundLetters with underslashes based on the length of the secret word.
    for (int j = 0; j < SECRET_WORD_LENGTH; ++j) {
        foundLetters[j] = '_';
    }

    cout << "Welcome to hangman!" << endl;

    while (true) {
        cout << "Take a guess: ";

        for (int j = 0; j < SECRET_WORD_LENGTH; ++j) {
            cout << foundLetters[j] << " ";
        }
        cout << "\n";

        cout << "Your guess: ";
        cin >> userChoice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        //if the user input is lowercase it'll be made upper case.
        userChoice = toupper(userChoice);

        if (hasLetter(foundLetters, SECRET_WORD_LENGTH, userChoice) ||
            hasLetter(wrongGuesses, wrongGuessesLength, userChoice))
        {
            cout << "You already guessed " << userChoice << "." << endl;
            continue;
        }

        found = 0;
        for (int j = 0; j < SECRET_WORD_LENGTH; ++j) {
            if (SECRET_WORD[j] == userChoice) {
                foundLetters[j] = userChoice;
                ++found;
            }
        }

        if (found > 0) {
            cout << "There's " << found << " " << userChoice << (found > 1 ? "'s" : "") << "!" << endl;

            foundLettersLength += found;

            if (foundLettersLength == SECRET_WORD_LENGTH) {
                cout << "You win!";
                break;
            }
        }
        else {
            cout << "Sorry. No " << userChoice << "'s." << endl;

            wrongLetters[wrongLettersLength++] = userChoice;

            if (--wrongGuessesLeft == 0) {
                cout << "You lose! Try again.";
                break;
            }

            cout << "You have " << wrongGuessesLeft << " guess" << (wrongGuessesLeft > 1 ? "es" : "") << " remaining." << endl;
        }
    }

    return 0; // signals the operating system that our program ended OK.
}

或者:

// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <string>
#include <limits>
#include <set>

// We will (most of the time) use the standard library namespace in our programs.
using namespace std;

//Defining the secret word as a constant
const string SECRET_WORD = "IMPOSSIBLE";

//Defining the number of wrong guesses available
const int WRONG_GUESSES = 7;

int main() {

    //Filling foundLetters with underslashes based on the length of the secret word.
    string foundLetters(SECRET_WORD.size(), '_');
    set<char> guessedLetters;

    int wrongGuessesLeft = WRONG_GUESSES; 
    char userChoice;
    int found;

    cout << "Welcome to hangman!" << endl;

    while (true) {
        cout << "Take a guess: ";

        for (int j = 0; j < foundLetters.size(); ++j) {
            cout << foundLetters[j] << " ";
        }
        cout << "\n";

        cout << "Your guess: ";
        cin >> userChoice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        //if the user input is lowercase it'll be made upper case.
        userChoice = toupper(userChoice);

        if (!guessedLetters.insert(userChoice).second)
        {
            cout << "You already guessed " << userChoice << "." << endl;
            continue;
        }

        string::size_type pos = SECRET_WORD.find(userChoice);
        if (pos != string::npos) {
            found = 0;
            do {
                foundLetters[pos] = userChoice;
                ++found;
                pos = SECRET_WORD.find(userChoice, pos+1);
            }
            while (pos != string::npos);

            cout << "There's " << found << " " << userChoice << (found > 1 ? "'s" : "") << "!" << endl;

            if (foundLetters == SECRET_WORD) {
                cout << "You win!";
                break;
            }
        }
        else {
            cout << "Sorry. No " << userChoice << "'s." << endl;

            if (--wrongGuessesLeft == 0) {
                cout << "You lose! Try again.";
                break;
            }

            cout << "You have " << wrongGuessesLeft << " guess" << (wrongGuessesLeft > 1 ? "es" : "") << " remaining." << endl;
        }
    }

    return 0; // signals the operating system that our program ended OK.
}