Skip to content

Commit

Permalink
fix: revise rand id generating func
Browse files Browse the repository at this point in the history
  • Loading branch information
jschang19 committed Dec 16, 2023
1 parent 00794f2 commit 164d13f
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions ChatGPT/src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "../include/Game.h"
#include "../include/Error.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm> // std::shuffle
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock
#include <tuple>
#include <iostream>

System::Game::Game() {
// get data from file

System::Game::Game(){
try {
this->stories = readTextFile("ChatGPT/data/stories.txt");
} catch (std::runtime_error& e) {
Expand All @@ -24,11 +27,25 @@ System::Story::~Story() {
this->choices.clear();
}

std::vector< int > System::Game::getRandStoryIds(int num) {
std::vector< int > ids;
for (int i = 0; i < num; i++) {
ids.push_back(rand() % this->stories.size());
std::vector<int> System::Game::getRandStoryIds(int num) {
std::vector<int> ids;

// 創建一個有所有可能索引的向量
for (int i = 0; i < this->stories.size(); ++i) {
ids.push_back(i);
}

// 使用當前時間作為隨機數生成器的種子
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

// 打亂向量
std::shuffle(ids.begin(), ids.end(), std::default_random_engine(seed));

// 只保留所需數量的元素
if (num < ids.size()) {
ids.resize(num);
}

return ids;
}

Expand Down Expand Up @@ -64,4 +81,52 @@ std::vector<System::Story> System::Game::readTextFile(const std::string& filenam
}

return data;
}

void System::Game::setOptions(int id, const std::vector< System::Option > &choices) {
System::Story* story_ptr = this->getStoryPtrById(id);
if (story_ptr == nullptr) {
std::cout<<"story_ptr is nullptr"<<std::endl;
return;
}
story_ptr->choices = choices;
}

void System::Game::printOptions(int id) {
System::Story* story_ptr = this->getStoryPtrById(id);
if (story_ptr == nullptr) {
return;
}
std::cout << "現在你在" << story_ptr->place << "" << story_ptr->content << std::endl;
for (auto& choice : story_ptr->choices) {
std::cout << choice.id << ": " << choice.text << std::endl;
}
}

bool System::Game::setUserChoice(int story_id, const std::string &user_choice_id) {
System::Story* story_ptr = this->getStoryPtrById(story_id);
if (story_ptr == nullptr) {
return false;
}
for (auto& choice : story_ptr->choices) {
if (choice.id == user_choice_id) {
story_ptr->user_choice = choice.text;
story_ptr->is_answered = true;
return true;
}
}
return true;
}

OpenAI::Message System::Game::generateStoryPrompt(int story_index) {
std::string prompt = "";
// get previous story
if(this->current_count == 0){
prompt = "我在" + this->stories[story_index].place + ",我" + this->stories[story_index].content;
}else{
int previous_id = this->story_ids[story_index - 1];
prompt = "我在「" + this->stories[story_index].place + "" + this->stories[story_index].content + "";
}

return OpenAI::Message({"user", prompt});
}

0 comments on commit 164d13f

Please sign in to comment.