我怎样才能让这个刽子手代码与 wxDev-C++ 一起工作?
How can I get this hangman code to work with wxDev-C++?
我有这个 hangman 项目作为我们教授的 C++ 指令出现。为此,我得到了 YouTube 的帮助(归功于 NVitanovic),并基于他用 C++ 编写的刽子手游戏。
执行 visual studio 中的代码后,ifstream reader(path);查找文件 words.txt 并从中加载随机词的部分代码显示了一个错误,它不允许我在 wxDev 中编译代码,但是它在 visual studio 中编译得很好。是否有 header 我遗漏了,或者我应该整体修改路径查找器?以下是完整代码:
#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;
}
非常感谢您!
编辑:没关系,我自己发现了问题,只需要将代码 ifstream reader(path);
中的 "path" 更改为 words.txt
。以后我会把它留在这里供其他人使用 :D
我有这个 hangman 项目作为我们教授的 C++ 指令出现。为此,我得到了 YouTube 的帮助(归功于 NVitanovic),并基于他用 C++ 编写的刽子手游戏。
执行 visual studio 中的代码后,ifstream reader(path);查找文件 words.txt 并从中加载随机词的部分代码显示了一个错误,它不允许我在 wxDev 中编译代码,但是它在 visual studio 中编译得很好。是否有 header 我遗漏了,或者我应该整体修改路径查找器?以下是完整代码:
#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;
}
非常感谢您!
编辑:没关系,我自己发现了问题,只需要将代码 ifstream reader(path);
中的 "path" 更改为 words.txt
。以后我会把它留在这里供其他人使用 :D