Witam.
Piszę system dialogów. Program ma działać tak, że wyświetla daną opcję dialogową i odpowiedzi do nie, następnie można wybrać jedną z odpowiedzi i wtedy wyświetla się następna opcja dialogowa z odpowiedziamij. Niestety mój program wypisuje tylko 3 z 4 odpowiedzi i nie wiem dlaczego. Dodam, że to pierwszy mój program gdzie operuję na stosie (co nie jest takie łatwe :-/). Pomożecie mi ?
Co wyświetla:
"Witaj! :-)" (option)
-Kim jestes? (answer)
-Co tutaj robisz? (answer)
-Czy jest cos co moglbym dla ciebie zrobic? (answer)
Co powinno wyświetlać:
"Witaj! :-)" (option)
-Kim jestes? (answer)
-Co tutaj robisz? (answer)
-Czy jest cos co moglbym dla ciebie zrobic? (answer)
-Zegnaj. (answer)
Dialog
"Witaj! :-)" (option)
-Kim jestes? (answer)
"Jestem rybakiem i nazywam sie John." (option)
-Co tutaj robisz? (answer)
"Lowie ryby na tej plazy." (option)
-"I jak biora?" (answer)
"Biora, biora. A co maja nie brac :P" (option)
-"W porzadku." (answer)
-Czy jest cos co moglbym dla ciebie zrobic? (answer)
"Tak. Jest w sumie jedna rzecz. Potrzebowalbym zanety na ryby, bo juz mi się konczy." (option)
-"Przyniose Ci zanete." (answer)
-"Sam sobie idz po te zanete! :D" (answer)
-Zegnaj. (answer)
Algorytm
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
std::wstring ConvertUtf8ToWide(const std::string& utf8Str) {
// TO-DO
short wideCharCount = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), utf8Str.size(), nullptr, 0);
if (wideCharCount == 0) {
throw std::runtime_error("Error in MultiByteToWideChar");
}
std::wstring wideStr(wideCharCount, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), utf8Str.size(), &wideStr[0], wideCharCount);
return wideStr;
}
short count_tabs(const std::string& line) {
short count = 0;
for (char c : line) {
if (c == '\t') count++;
else break;
}
return count;
}
short count_tabs(const std::wstring& line) {
short count = 0;
for (char c : line) {
if (c == L'\t') count++;
else break;
}
return count;
}
void test_dialogues_system() {
class DialogueOption {
public:
class DialogueAnswer {
public:
std::wstring text;
DialogueOption* next_option;
DialogueAnswer(std::wstring text) {
this->text = text;
next_option = nullptr;
}
};
std::wstring text;
std::vector < DialogueAnswer* > answers;
DialogueOption(std::wstring text) {
this->text = text;
answers.clear();
}
};
class Dialogue {
public:
short id;
std::vector < DialogueOption* > options;
Dialogue(short id) {
this->id = id;
load();
}
~Dialogue() { }
void load() {
std::vector < DialogueOption* > stack;
std::string line;
std::ifstream file("dialogues\\new\\000.txt");
while (std::getline(file, line)) {
if (line.empty())
continue;
if (stack.empty()) {
stack.push_back(new DialogueOption(ConvertUtf8ToWide(line)));
options.push_back(stack.back());
}
else {
short tabs = count_tabs(line);
short stack_tabs = count_tabs(stack.back()->text);
std::wcout << L"t: " << tabs << " st: " << stack_tabs << L"\n";
if (tabs == stack_tabs) {
if (line[tabs] == '-') {
// is Dialogue Answer
DialogueOption::DialogueAnswer* answer = new DialogueOption::DialogueAnswer(ConvertUtf8ToWide(line));
answer->next_option = stack.back();
stack.back()->answers.push_back(answer);
}
}
else if (tabs > stack_tabs) {
// is new Dialogue Option
DialogueOption* option = new DialogueOption(ConvertUtf8ToWide(line));
stack.push_back(option);
}
else if (tabs < stack_tabs) {
// undo
options.push_back(stack.back());
stack.pop_back();
// dopisane jak radził pekfos
DialogueOption::DialogueAnswer* answer = new DialogueOption::DialogueAnswer(ConvertUtf8ToWide(line));
answer->next_option = stack.back();
stack.back()->answers.push_back(answer);
}
}
}
file.close();
options.push_back(stack.back());
}
};
Dialogue* dialogue = new Dialogue(0);
DialogueOption* current_option = dialogue->options.front();
while (true) {
std::wcout << current_option->text << L"\n";
for (auto& answer : current_option->answers) {
std::wcout << answer->text << "\n";
}
std::wcout << L"\n";
short choice = 0;
std::cin >> choice;
if (choice >= 0 && choice < current_option->answers.size()) {
current_option = current_option->answers[choice]->next_option;
}
};
}
int main() {
test_dialogues_system();
return 0;
}
