如何让这个hangman代码与wxDev-C ++一起使用?

问题描述 投票:0回答:1

我将这个刽子手项目作为我们教授的C ++指令。要做到这一点,我得到了YouTube的帮助(归功于NVitanovic),并以他在C ++中的刽子手游戏为基础。

在visual studio中执行代码时,ifstream阅读器(路径);查找文件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;
}

非常感谢你提前!

c++ path finder
1个回答
0
投票

编辑:没关系,我自己发现了问题,只需将代码ifstream reader(path);中的“路径”更改为words.txt。我会留在这里为未来的其他人:D

© www.soinside.com 2019 - 2024. All rights reserved.