From cdc9b636443b3f862dba217b33447943026390ec Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 28 Nov 2019 12:26:31 -0700 Subject: [PATCH 1/6] Develop (#8) removes final lines with "you say" or "you ask" adds back death checking allows reverting actions --- generator/ctrl/model/fastBPE | 1 + .../training_utils/action_results.tfrecords | 0 generator/gpt2/gpt2_generator.py | 2 +- play.py | 31 ++++++++++++++++--- story/utils.py | 20 ++++++++++-- 5 files changed, 47 insertions(+), 7 deletions(-) create mode 160000 generator/ctrl/model/fastBPE create mode 100644 generator/ctrl/training_utils/action_results.tfrecords diff --git a/generator/ctrl/model/fastBPE b/generator/ctrl/model/fastBPE new file mode 160000 index 0000000..1fd3318 --- /dev/null +++ b/generator/ctrl/model/fastBPE @@ -0,0 +1 @@ +Subproject commit 1fd33189c126dae356b9e187d93d93302fa45cef diff --git a/generator/ctrl/training_utils/action_results.tfrecords b/generator/ctrl/training_utils/action_results.tfrecords new file mode 100644 index 0000000..e69de29 diff --git a/generator/gpt2/gpt2_generator.py b/generator/gpt2/gpt2_generator.py index f64deef..c52814d 100644 --- a/generator/gpt2/gpt2_generator.py +++ b/generator/gpt2/gpt2_generator.py @@ -86,7 +86,7 @@ class GPT2Generator: def generate(self, prompt, options=None, seed=1): - debug_print = False + debug_print = True prefix = self.prompt_replace(prompt) if debug_print: diff --git a/play.py b/play.py index 07d59e2..75dc75e 100644 --- a/play.py +++ b/play.py @@ -24,7 +24,7 @@ def select_game(): if choice == len(settings): console_print("Enter a sentence or two that describes the context of who your character is. Ex. ' " + - "You are a knight living in the king of Larion. You have a sword and shield. '") + "You are a knight living in the king of Larion. You have a sword and shield.'") context = input("Context: ") console_print("Enter the first couple sentences to start your adventure off. Ex. " + "'You enter the forest searching for the dragon and see' ") @@ -53,7 +53,8 @@ def select_game(): def instructions(): text = "\nAI Dungeon 2 Instructions:" text += '\n* Enter actions starting with a verb ex. "go to the tavern" or "attack the orc."' - text += '\n* If you want to say something then enter \'say "(thing you want to say)"\'' + text += '\n* To speak enter \'say "(thing you want to say)"\' or just "(thing you want to say)" ' + text += '\n* Enter "revert" for any action if you want to undo the last action and result.' text += '\n* Finally if you want to end your game and start a new one just enter "restart" for any action. ' return text @@ -94,9 +95,27 @@ def play_aidungeon_2(): break elif action == "quit": exit() - + elif action == "revert": - if action != "": + if len(story_manager.story.actions) is 0: + console_print("You can't go back any farther. ") + continue + + story_manager.story.actions = story_manager.story.actions[:-1] + story_manager.story.results = story_manager.story.results[:-1] + console_print("Last action reverted. ") + if len(story_manager.story.results) > 0: + console_print(story_manager.story.results[-1]) + else: + console_print(story_manager.story.story_start) + continue + elif action == "": + action = "" + + elif action[0] == '"': + action = "You say " + action + + else: action = action.strip() action = action[0].lower() + action[1:] @@ -115,6 +134,10 @@ def play_aidungeon_2(): if player_won(result): console_print(result + "\n CONGRATS YOU WIN") break + elif player_died(result): + console_print(result) + console_print("YOU DIED. GAME OVER") + break else: console_print(result) diff --git a/story/utils.py b/story/utils.py index ba5a46e..e936512 100644 --- a/story/utils.py +++ b/story/utils.py @@ -37,8 +37,16 @@ def get_num_options(num): def player_died(text): + reg_phrases = ["You[a-zA-Z ]* die.", "you[a-zA-Z ]* die.", "You[a-zA-Z ]* die ", "you[a-zA-Z ]* die ",] + + for phrase in reg_phrases: + reg_expr = re.compile(phrase) + matches = re.findall(reg_expr, text) + if len(matches) > 0: + return True + dead_phrases = ["you die", "You die", "you died", "you are dead", "You died", "You are dead", "You're dead", - "you're dead", "you have died", "You have died"] + "you're dead", "you have died", "You have died", "finish you off", "Your death", "your death"] for phrase in dead_phrases: if phrase in text: return True @@ -78,6 +86,12 @@ def split_first_sentence(text): return text[0:split_point], text[split_point:] +def cut_trailing_action(text): + lines = text.split("\n") + last_line = lines[-1] + if "you ask." in last_line or "You ask." in last_line or "you say." in last_line or "You say." in last_line: + text = "\n".join(lines[0:-1]) + return text def cut_trailing_sentence(text): text = standardize_punctuation(text) @@ -94,7 +108,9 @@ def cut_trailing_sentence(text): if last_punc > 0: text = text[0:last_punc+1] - return cut_trailing_quotes(text) + text = cut_trailing_quotes(text) + text = cut_trailing_action(text) + return text def replace_outside_quotes(text, current_word, repl_word): From ea829087b8f74ad1b74ab1ae374d17ea53612ac4 Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 28 Nov 2019 12:31:16 -0700 Subject: [PATCH 2/6] Update gpt2_generator.py --- generator/gpt2/gpt2_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/gpt2/gpt2_generator.py b/generator/gpt2/gpt2_generator.py index c52814d..f64deef 100644 --- a/generator/gpt2/gpt2_generator.py +++ b/generator/gpt2/gpt2_generator.py @@ -86,7 +86,7 @@ class GPT2Generator: def generate(self, prompt, options=None, seed=1): - debug_print = True + debug_print = False prefix = self.prompt_replace(prompt) if debug_print: From 7f79d675f6ac8383d52c645adb8495a2ed49242b Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 28 Nov 2019 12:42:27 -0700 Subject: [PATCH 3/6] Update play.py --- play.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/play.py b/play.py index 75dc75e..171b750 100644 --- a/play.py +++ b/play.py @@ -135,8 +135,11 @@ def play_aidungeon_2(): console_print(result + "\n CONGRATS YOU WIN") break elif player_died(result): - console_print(result) - console_print("YOU DIED. GAME OVER") + console_print(result): + died = input("Did you die? (y/N)") + if died.lower() in ["yes", "y"] + console_print("YOU DIED. GAME OVER") + break break else: console_print(result) From ebca0110cf496e4faa5f95cfa4e35bceb72338bd Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 28 Nov 2019 14:06:04 -0700 Subject: [PATCH 4/6] Update play.py --- play.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/play.py b/play.py index 171b750..bc54dba 100644 --- a/play.py +++ b/play.py @@ -135,9 +135,9 @@ def play_aidungeon_2(): console_print(result + "\n CONGRATS YOU WIN") break elif player_died(result): - console_print(result): + console_print(result) died = input("Did you die? (y/N)") - if died.lower() in ["yes", "y"] + if died.lower() in ["yes", "y"]: console_print("YOU DIED. GAME OVER") break break From fe994b022bb2109d3a59c9c92169b226106e3404 Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 28 Nov 2019 14:06:40 -0700 Subject: [PATCH 5/6] Update play.py --- play.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/play.py b/play.py index bc54dba..1115ae7 100644 --- a/play.py +++ b/play.py @@ -136,10 +136,10 @@ def play_aidungeon_2(): break elif player_died(result): console_print(result) - died = input("Did you die? (y/N)") - if died.lower() in ["yes", "y"]: - console_print("YOU DIED. GAME OVER") - break + died = input("Did you die? (y/N)") + if died.lower() in ["yes", "y"]: + console_print("YOU DIED. GAME OVER") + break break else: console_print(result) From 9c08ff631f07d2f6eee9f637c0e5d833d1497622 Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 28 Nov 2019 14:16:19 -0700 Subject: [PATCH 6/6] Update play.py --- play.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/play.py b/play.py index 1115ae7..89be297 100644 --- a/play.py +++ b/play.py @@ -140,7 +140,7 @@ def play_aidungeon_2(): if died.lower() in ["yes", "y"]: console_print("YOU DIED. GAME OVER") break - break + else: console_print(result)