Ostatnio zmodyfikowano wczoraj o godz. 13:14
tBane Temat założony przez niniejszego użytkownika |
System Dialogów v3 - czyli ostateczna wersja » 2025-04-27 17:10:15 Cześć. Piszę na nowo system dialogów w moim Edytorze i mam problem. Zamiast wyświetlić pierwszą linię, to wyświetla mi drugą opcję. "Jestem rybakiem i nazywam sie John." (option) . Pierwszy raz pracuję ze stosem i jakoś mi nie wychodzi :-/ struktura dialogu: "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)
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 ) ) ); } else { short tabs = line.find_first_not_of( "\t" ); short stack_tabs = stack.back()->text.find_first_not_of( L"\t" ); if( tabs == stack_tabs ) { DialogueOption::DialogueAnswer * answer = new DialogueOption::DialogueAnswer( ConvertUtf8ToWide( line ) ); answer->next_option = stack.back(); stack.back()->answers.push_back( answer ); } else if( tabs > stack_tabs ) { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); stack.push_back( option ); } else if( tabs < stack_tabs ) { options.push_back( stack.back() ); stack.pop_back(); } } } 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"; } short odp; std::cin >> odp; }; }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-04-27 18:03:05 Poprawiłem warunek z pustym stosem. Teraz wyświetla poprawnie pierwszą opcję dialogową ale tylko jedną odpowiedź "Witaj! :-)" (option) -Kim jestes? (answer)
a powinno być "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) -Zegnaj. (answer)
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 == '\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 ] == '-' ) { DialogueOption::DialogueAnswer * answer = new DialogueOption::DialogueAnswer( ConvertUtf8ToWide( line ) ); answer->next_option = stack.back(); stack.back()->answers.push_back( answer ); } } else if( tabs > stack_tabs ) { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); stack.push_back( option ); } else if( tabs < stack_tabs ) { options.push_back( stack.back() ); stack.pop_back(); } } } 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; } }; }
|
|
pekfos |
» 2025-04-28 01:51:21 else if( tabs < stack_tabs ) { options.push_back( stack.back() ); stack.pop_back(); } } Brakujące opcje są obsługiwane tym kodem i nie dodajesz ich nigdzie. Po poprawieniu stosu powinieneś wykonać kod taki jak dla tabs == stack_tabs. Piszę na nowo system dialogów w moim Edytorze i mam problem. A po co? Jak na razie wygląda identycznie z poprzednim systemem. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-04-28 10:19:59 Zrobiłem jak doradziłeś pekfos i tereaz wyświetla 3 z 4 odpowiedzi. "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) a powinno być: "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) -Zegnaj. (answer) #include <iostream> #include <vector> #include <string> #include <fstream> #include <windows.h> using namespace std;
std::wstring ConvertUtf8ToWide( const std::string & utf8Str ) { 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 ] == '-' ) { DialogueOption::DialogueAnswer * answer = new DialogueOption::DialogueAnswer( ConvertUtf8ToWide( line ) ); answer->next_option = stack.back(); stack.back()->answers.push_back( answer ); } } else if( tabs > stack_tabs ) { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); stack.push_back( option ); } else if( tabs < stack_tabs ) { options.push_back( stack.back() ); stack.pop_back(); 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; }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-04-28 12:23:29 Poprawiłem nieco kod i teraz dialogi działają lepiej, tylko odpowiedzi źle wczytuje tzn. nie wczytuje ostatniej odpowiedzi "-Zegnaj. (answer)" "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer)
0 "Jestem rybakiem i nazywam sie John." (option) "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer)
1 "Lowie ryby na tej plazy." (option) -"I jak biora?" (answer) -"W porzadku." (answer)
0 "Biora, biora. A co maja nie brac :P" (option) "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) #include <iostream> #include <vector> #include <string> #include <fstream> #include <windows.h> using namespace std;
std::wstring ConvertUtf8ToWide( const std::string & utf8Str ) { 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 ] == '-' ) { DialogueOption::DialogueAnswer * answer = new DialogueOption::DialogueAnswer( ConvertUtf8ToWide( line ) ); answer->next_option = stack.front(); stack.back()->answers.push_back( answer ); } else { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); if( !stack.back()->answers.empty() ) stack.back()->answers.back()->next_option = option; stack.push_back( option ); } } else if( tabs > stack_tabs ) { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); if( !stack.back()->answers.empty() ) stack.back()->answers.back()->next_option = option; stack.push_back( option ); } else if( tabs < stack_tabs ) { options.push_back( stack.back() ); stack.pop_back(); 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"; if( current_option->answers.empty() ) { current_option = dialogue->options.front(); continue; } 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; }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-04-28 12:55:51 Dobra. Napisałem i działa. :-) "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) -Zegnaj. (answer)
1 "Lowie ryby na tej plazy." (option) -I jak biora? (answer) -W porzadku. (answer)
1 "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) -Zegnaj. (answer)
2 "Tak. Jest w sumie jedna rzecz. Potrzebowalbym zanety na ryby, bo juz mi sie konczy." (option) -Przyniose Ci zanete. (answer) -Sam sobie idz po te zanete! :D (answer)
0 "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) -Zegnaj. (answer)
1 "Lowie ryby na tej plazy." (option) -I jak biora? (answer) -W porzadku. (answer)
0 "Biora, biora. A co maja nie brac :P" (option) "Witaj! :-)" (option) -Kim jestes? (answer) -Co tutaj robisz? (answer) -Czy jest cos co moglbym dla ciebie zrobic? (answer) -Zegnaj. (answer) #include <iostream> #include <vector> #include <string> #include <fstream> #include <windows.h> using namespace std;
std::wstring ConvertUtf8ToWide( const std::string & utf8Str ) { 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 ] == '-' ) { DialogueOption::DialogueAnswer * answer = new DialogueOption::DialogueAnswer( ConvertUtf8ToWide( line ) ); answer->next_option = stack.front(); stack.back()->answers.push_back( answer ); } else { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); if( !stack.back()->answers.empty() ) stack.back()->answers.back()->next_option = option; stack.push_back( option ); } } else if( tabs > stack_tabs ) { DialogueOption * option = new DialogueOption( ConvertUtf8ToWide( line ) ); if( !stack.back()->answers.empty() ) stack.back()->answers.back()->next_option = option; stack.push_back( option ); } else if( tabs < stack_tabs ) { options.push_back( stack.back() ); stack.pop_back(); DialogueOption::DialogueAnswer * answer = new DialogueOption::DialogueAnswer( ConvertUtf8ToWide( line ) ); answer->next_option = stack.front(); 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::wstring text = current_option->text; text.erase( 0, text.find_first_not_of( L'\t' ) ); std::wcout << text << L"\n"; if( current_option->answers.empty() ) { current_option = dialogue->options.front(); continue; } for( auto & answer: current_option->answers ) { std::wstring text = answer->text; text.erase( 0, text.find_first_not_of( L'\t' ) ); std::wcout << 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; }
|
|
« 1 » |