Skip to content

Commit

Permalink
refactor:adding getter and setter for class Game
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathanliao02 committed Dec 20, 2023
1 parent 9f6175c commit 213c400
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
59 changes: 33 additions & 26 deletions ChatGPT/include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,39 @@ class Story {
};

class Game {
public:
int count;
int current_count = 0;
std::vector<int> story_ids;
void addPrompt(OpenAI::ChatGPT& chatGpt, const OpenAI::Message& prompt,bool isLast);
OpenAI::ChatCompletion sendToChatGPT(OpenAI::ChatGPT& chatGpt, bool isEnding);
std::vector<int> picked_story_ids;
void parseGPTResponse(OpenAI::ChatCompletion& chatCompletion, int story_id);
void parseEndingResponse(OpenAI::ChatCompletion& chatCompletion);
Game();
~Game(); // destructor
std::vector<System::Story> stories;
std::vector<int> getRandStoryIds(int num);
System::Story* getStoryPtrById(int id);
OpenAI::Message generateStoryPrompt(int id);
OpenAI::Message generateEndingPrompt();
void setOptions(int id, const std::vector<System::Option>& choices);
void printOptions(int id);
void print(const std::string& str, const std::string& color = "w",bool bold = false);
void printWelcome();
void checkStatus(const std::string& key);
bool verifyOpenAiKey(const std::string& key);
bool checkConnection();
bool setUserChoice(int story_id, std::string user_choice_id);
public:
//setter

private:
std::vector<System::Story> readTextFile(const std::string& filename);
void set_count(int new_count);
void set_story_id(std::vector<int> new_ids);
void add_current_count(int adding_num);


void addPrompt(OpenAI::ChatGPT& chatGpt, const OpenAI::Message& prompt,bool isLast);
OpenAI::ChatCompletion sendToChatGPT(OpenAI::ChatGPT& chatGpt, bool isEnding);
std::vector<int> picked_story_ids;
void parseGPTResponse(OpenAI::ChatCompletion& chatCompletion, int story_id);
void parseEndingResponse(OpenAI::ChatCompletion& chatCompletion);
Game();
~Game(); // destructor
std::vector<System::Story> stories;
std::vector<int> getRandStoryIds(int num);
System::Story* getStoryPtrById(int id);
OpenAI::Message generateStoryPrompt(int id);
OpenAI::Message generateEndingPrompt();
void setOptions(int id, const std::vector<System::Option>& choices);
void printOptions(int id);
void print(const std::string& str, const std::string& color = "w",bool bold = false);
void printWelcome();
void checkStatus(const std::string& key);
bool verifyOpenAiKey(const std::string& key);
bool checkConnection();
bool setUserChoice(int story_id, std::string user_choice_id);

private:
std::vector<System::Story> readTextFile(const std::string& filename);
int count;
int current_count = 0;
std::vector<int> story_ids;
};
} // namespace System
10 changes: 10 additions & 0 deletions ChatGPT/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#include <chrono>
#include <cstdlib> // for connection check

void System::Game::set_count(int new_count){
this->count=new_count;
}
void System::Game::set_story_id(std::vector<int> new_ids){
this->story_ids=new_ids;
}
void System::Game::add_current_count(int adding_num){
this->current_count+=adding_num;
}


System::Game::Game(){
try {
Expand Down
6 changes: 3 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ int main(){
game.checkStatus(key);
std::cout<<std::endl;

game.count=STORY_NUM;
game.set_count(STORY_NUM);
OpenAI::ChatGPT chatGpt{key};
game.printWelcome();
// get random story ids
std::vector<int> story_ids = game.getRandStoryIds(STORY_NUM);
game.story_ids = story_ids;
game.set_story_id(story_ids);

try {
for (int i=0; i<STORY_NUM; i++){
Expand All @@ -54,7 +54,7 @@ int main(){
std::cout<<std::endl;
game.print("你選擇了 "+userInput, "w");
game.setUserChoice(story_id, userInput);
game.current_count += 1;
game.add_current_count(1);
std::cout<<std::endl;

std::this_thread::sleep_for(std::chrono::seconds(1));
Expand Down

0 comments on commit 213c400

Please sign in to comment.