Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_play_generated_games():
NB_GAMES = 10
rng = np.random.RandomState(1234)
for i in range(NB_GAMES):
# Sample game specs.
world_size = rng.randint(1, 10)
nb_objects = rng.randint(0, 20)
quest_length = rng.randint(2, 5)
quest_breadth = rng.randint(3, 7)
game_seed = rng.randint(0, 65365)
with make_temp_directory(prefix="test_play_generated_games") as tmpdir:
options = textworld.GameOptions()
options.nb_rooms = world_size
options.nb_objects = nb_objects
options.quest_length = quest_length
options.quest_breadth = quest_breadth
options.seeds = game_seed
game_file, game = textworld.make(options, path=tmpdir)
# Solve the game using WalkthroughAgent.
agent = textworld.agents.WalkthroughAgent()
textworld.play(game_file, agent=agent, silent=True)
# Play the game using RandomAgent and make sure we can always finish the
# game by following the winning policy.
env = textworld.start(game_file)
def test_playing_a_game():
with make_temp_directory(prefix="test_tw-play") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 5
options.nb_objects = 10
options.quest_length = 5
options.quest_breadth = 2
options.seeds = 1234
game_file, _ = textworld.make(options)
command = ["tw-play", "--max-steps", "100", "--mode", "random", game_file]
assert check_call(command) == 0
command = ["tw-play", "--max-steps", "100", "--mode", "random-cmd", game_file]
assert check_call(command) == 0
command = ["tw-play", "--max-steps", "100", "--mode", "walkthrough", game_file]
def test_sample_quests():
with make_temp_directory(prefix="test_sample_quests") as tmpdir:
game_file = pjoin(tmpdir, "game.ulx")
command = ["tw-make", "custom", "--seed", "20181004", "--output", game_file]
check_output(command).decode()
script = pjoin(SCRIPTS_PATH, "sample_quests.py")
command = ["python", script, "--nb-quests", "10", "--quest-length", "10",
"--quest-breadth", "5", "--output", tmpdir, game_file]
stdout = check_output(command).decode()
assert len(stdout) > 0
assert os.path.isfile(pjoin(tmpdir, "sample_world.png"))
assert os.path.isfile(pjoin(tmpdir, "sample_tree.svg"))
assert os.path.isfile(pjoin(tmpdir, "sample_graph.svg"))
def test_playing_generated_games():
NB_GAMES = 10
rng = np.random.RandomState(1234)
for i in range(NB_GAMES):
# Sample game specs.
world_size = rng.randint(1, 10)
nb_objects = rng.randint(0, 20)
quest_depth = rng.randint(2, 5)
quest_breadth = rng.randint(3, 7)
game_seed = rng.randint(0, 65365)
with make_temp_directory(prefix="test_play_generated_games") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = world_size
options.nb_objects = nb_objects
options.chaining.max_depth = quest_depth
options.chaining.max_breadth = quest_breadth
options.seeds = game_seed
game_file, game = textworld.make(options)
# Solve the game using WalkthroughAgent.
agent = textworld.agents.WalkthroughAgent()
textworld.play(game_file, agent=agent, silent=True)
# Play the game using RandomAgent and make sure we can always finish the
# game by following the winning policy.
env = textworld.start(game_file)
def test_making_challenge_game():
settings = {
"tw-treasure_hunter": ["--level", "1"],
"tw-coin_collector": ["--level", "1", "--force-entity-numbering"],
"tw-simple": ["--rewards", "dense", "--goal", "brief"],
}
with make_temp_directory(prefix="test_tw-challenge") as tmpdir:
for challenge in textworld.challenges.CHALLENGES:
output_folder = pjoin(tmpdir, "gen_games")
game_file = pjoin(output_folder, challenge + ".ulx")
command = ["tw-make", challenge, "--seed", "1234", "--output", game_file, "--silent"] + settings[challenge]
assert check_call(command) == 0
assert os.path.isdir(output_folder)
assert os.path.isfile(game_file)
# Solve the game using WalkthroughAgent.
agent = textworld.agents.WalkthroughAgent()
textworld.play(game_file, agent=agent, silent=True)
def test_making_game_is_reproducible_with_seed():
with make_temp_directory(prefix="test_render_wrapper") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 2
options.nb_objects = 20
options.chaining.max_depth = 3
options.chaining.max_breadth = 2
options.seeds = 123
game_file1, game1 = textworld.make(options)
options2 = options.copy()
game_file2, game2 = textworld.make(options2)
assert game_file1 == game_file2
assert game1 == game2
# Make sure they are not the same Python objects.
assert id(game1) != id(game2)
def test_making_a_game_using_basic_theme():
with make_temp_directory(prefix="test_tw-make") as tmpdir:
output_folder = pjoin(tmpdir, "gen_games")
game_file = pjoin(output_folder, "game_1234.ulx")
command = ["tw-make", "custom", "--theme", "basic", "--seed", "1234", "--output", game_file]
assert check_call(command) == 0
# Solve the game using WalkthroughAgent.
agent = textworld.agents.WalkthroughAgent()
textworld.play(game_file, agent=agent, silent=True)
def test_third_party():
with make_temp_directory(prefix="test_tw-make_third_party") as tmpdir:
challenge_py = pjoin(tmpdir, "my_challenge.py")
with open(challenge_py, "w") as f:
f.write(textwrap.dedent("""\
import argparse
from typing import Mapping, Optional
import textworld
from textworld.challenges import register
from textworld import Game, GameOptions
def build_argparser(parser=None):
parser = parser or argparse.ArgumentParser()
group = parser.add_argument_group('This challenge settings')
def set_quest_from_commands(self, commands: List[str], ask_for_state: bool = False) -> Quest:
""" Defines the game's quest using predefined text commands.
This launches a `textworld.play` session.
Args:
commands: Text commands.
ask_for_state: If true, the user will be asked to specify
which set of facts of the final state are
should be true in order to consider the quest
as completed.
Returns:
The resulting quest.
"""
with make_temp_directory() as tmpdir:
try:
game_file = self.compile(pjoin(tmpdir, "record_quest.ulx"))
recorder = Recorder()
agent = textworld.agents.WalkthroughAgent(commands)
textworld.play(game_file, agent=agent, wrappers=[recorder], silent=True)
except textworld.agents.WalkthroughDone:
pass # Quest is done.
# Skip "None" actions.
actions = [action for action in recorder.actions if action is not None]
# Ask the user which quests have important state, if this is set
# (if not, we assume the last action contains all the relevant facts)
winning_facts = None
if ask_for_state and recorder.last_game_state is not None:
winning_facts = [user_query.query_for_important_facts(actions=recorder.actions,
def compile_inform7_game(source: str, output: str, verbose: bool = False) -> None:
with make_temp_directory(prefix="tmp_inform") as project_folder:
filename, ext = os.path.splitext(output)
story_filename = filename + ".ni"
# Save story file.
with open(story_filename, 'w') as f:
f.write(source)
# Create the file structure needed by Inform7.
source_folder = pjoin(project_folder, "Source")
build_folder = pjoin(project_folder, "Build")
if not os.path.isdir(source_folder):
os.makedirs(source_folder)
shutil.copy(story_filename, pjoin(source_folder, "story.ni"))
# Write mandatory uuid.txt file