如何获得此hangman代码以与wxDev-C ++一起使用?

时间:2019-03-27 07:38:19

标签: c++ path finder

我有一个子手项目,是我们教授的C ++指令。为此,我得到了YouTube的帮助(归功于NVitanovic),并以他的C ++ hangman游戏为基础。

在Visual Studio中执行代码后,ifstream reader(path);查找文件words.txt并从中加载随机单词的代码的一部分显示了错误,但不允许我在wxDev中编译代码,但是在Visual Studio中可以正常编译。是否有我缺少的标头,或者应该整体上修饰路径查找器?下面是完整的代码:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <fstream>
#include <string>
#include <time.h>

using namespace std;

void sign(string message, bool printTop = true, bool printBottom = true) {
    if (printTop) {
        cout << "+---------------------------------+" << endl;
        cout << "|";
    }
    else {
        cout << "|";
    }

    bool front = true;
    for (int i = message.length(); i < 33; i++) {
        if (front) {
            message = " " + message;
        }
        else {
            message = message + " ";
        }
        front = !front;
    }
    cout << message.c_str();

    if (printBottom) {
        cout << "|" << endl;
        cout << "+---------------------------------+" << endl;
    }
    else {
        cout << "|" << endl;
    }
}

void hangman(int guess = 0) {
    if (guess >= 1) {
        sign("|", false, false);
    }
    else {
        sign("", false, false);
    }

    if (guess >= 2) {
        sign("|", false, false);
    }
    else {
        sign("", false, false);
    }

    if (guess >= 3) {
        sign("O", false, false);
    }
    else {
        sign("", false, false);
    }

    if (guess >= 4) {
        sign("/|\\", false, false);
    }
    else {
        sign("", false, false);
    }

    if (guess >= 5) {
        sign("|", false, false);
        sign("/ \\", false, false);
    }
    else {
        sign("", false, false);
        sign("", false, false);
    }

    if (guess >= 6) {
        sign("+---------+", false, false);
        sign("|         |", false, false);
    }
    else {
        sign("", false, false);
        sign("", false, false);
    }
}

void showLetters(string input, char from, char to) {
    string s;

    for (char i = from; i <= to; i++) {
        if (input.find(i) == string::npos) {
            s += i;
            s += " ";
        }
        else {
            s += "  ";
            //Two spaces to fix the padding between the letters.
        }
    }

    sign(s, false, false);

}

void availableLetters(string taken) {
    showLetters(taken, 'A', 'M');
    showLetters(taken, 'N', 'Z');
}

bool wordWin(string word, string guessed) {
    bool won = true;

    string s;

    for (int i = 0; i < word.length(); i++) {
        if (guessed.find(word[i]) == string::npos) {
            won = false;
            s += "_ ";
        }
        else {
            s += word[i];
            s += " ";
        }
    }
    sign(s, false);
    return won;
}

string getWord(string path) {
    int lineCount = 0; 
    string word; 
    vector<string> v; 

    ifstream reader(path);

    if (reader.is_open()) {
        while (std::getline(reader, word)) {
            v.push_back(word);
        }

        int randomLine = rand() % v.size();
        //This will set the range from 0 to the number of lines.

        word = v.at(randomLine);
        reader.close();
    }
    return word;
}

int main() {
    srand(time(0));

    string mistakes = "";
    string guessWord;

    guessWord = getWord("words.txt");

    cout << guessWord << endl << endl;

    sign("HANG MAN");

    hangman(6);

    sign("AVAILABLE LETTERS");

    availableLetters(mistakes);

    sign("WORD TO GUESS");

    wordWin(guessWord, mistakes);

    cout << endl;
    system("pause");
    return 0;
}

以下是路径查找器错误的片段:

string getWord(string path) {
    int lineCount = 0; 
    string word; 
    vector<string> v; 

    ifstream reader(path);

    if (reader.is_open()) {
        while (std::getline(reader, word)) {
            v.push_back(word);
        }

        int randomLine = rand() % v.size();
        //This will set the range from 0 to the number of lines.

        word = v.at(randomLine);
        reader.close();
    }
    return word;
}

非常感谢您!

1 个答案:

答案 0 :(得分:0)

编辑:没关系,我自己发现了问题,只需要将代码bind {replace with machine ip}中的“路径”更改为ifstream reader(path);。以后我会留给别人的:D