diff --git a/unitary/examples/quantum_rpg/final_state_preparation/classical_frontier.py b/unitary/examples/quantum_rpg/final_state_preparation/classical_frontier.py index 218828d3..d265b3ee 100644 --- a/unitary/examples/quantum_rpg/final_state_preparation/classical_frontier.py +++ b/unitary/examples/quantum_rpg/final_state_preparation/classical_frontier.py @@ -129,15 +129,30 @@ def _hole(state: GameState, world): ) -def _bridge(state: GameState, world) -> str: +def _examine_bridge(state: GameState, world) -> str: + location = world.get(state.current_location_label) + if "hbridgestate" in state.state_dict: + location.exits[Direction.NORTH] = "hadamard1" + if Direction.NORTH in location.exits: + return "You have already fixed the bridge." + return ( + "It looks as if this bridge has fallen into disrepair.\n" + "Parts have decayed and have fallen into the river. However,\n" + "there are plenty of logs around, and the supports are intact,\n" + "so it should be possible to repair it, for someone with the right skills." + ) + + +def _fix_bridge(state: GameState, world) -> str: + location = world.get(state.current_location_label) + if Direction.NORTH in location.exits: + return "You have already fixed the bridge." has_engineer = any(isinstance(qar, Engineer) for qar in state.party) if not has_engineer: return "You do not have the required skills to fix the bridge." else: - location = world.get(state.current_location_label) - if Direction.NORTH in location.exits: - return "You have already fixed the bridge." location.exits[Direction.NORTH] = "hadamard1" + state.state_dict["hbridgestate"] = "fixed" return "The engineer uses nearby logs to repair the bridge and provide a safe passage." @@ -146,17 +161,12 @@ def _bridge(state: GameState, world) -> str: ( EXAMINE, ["bridge"], - ( - "It looks as if this bridge has fallen into disrepair.\n" - "Parts have decayed and have fallen into the river. However,\n" - "there are plenty of logs around, and the supports are intact,\n" - "so it should be possible to repair it, for someone with the right skills." - ), + _examine_bridge, ), ( ["fix", "repair"], ["bridge"], - _bridge, + _fix_bridge, ), ] ) @@ -320,8 +330,8 @@ def _bridge(state: GameState, world) -> str: description=( "Here, at the southern shore of the river is a rickety bridge\n" "that leads to the northern side of the river. Pieces of the bridge\n" - "have collapsed and fallen apart, and there seems to be no way to\n" - "cross safely, given the condition the bridge is in now." + "have collapsed and fallen apart, and without further examination\n" + "it's unclear if the bridge is safe to cross, given its current condition." ), encounters=[red_foam(2, 0.3), green_foam(3, 0.2)], items=[BRIDGE], diff --git a/unitary/examples/quantum_rpg/final_state_preparation/final_state_world_test.py b/unitary/examples/quantum_rpg/final_state_preparation/final_state_world_test.py index b90a89f7..aa8fd028 100644 --- a/unitary/examples/quantum_rpg/final_state_preparation/final_state_world_test.py +++ b/unitary/examples/quantum_rpg/final_state_preparation/final_state_world_test.py @@ -131,22 +131,61 @@ def test_bridge(): state = game_state.GameState(party=[c], user_input=["Hamilton"], file=io.StringIO()) state.current_location_label = "classical12" test_world = go_directions("nnwnnnne") + bridge_location = test_world.get("classical12") + fix = test_world.current_location.get_action("fix bridge") + examine = test_world.current_location.get_action("examine bridge") + + # The world and the game state are at the location of the broken bridge, which can be fixed and examined. assert test_world.current_location.title == "At a Broken Bridge" - action = test_world.current_location.get_action("fix bridge") - assert callable(action) - msg = action(state, test_world) + assert ( + test_world.current_location.label + == state.current_location_label + == bridge_location.label + ) + assert callable(fix) + assert callable(examine) + + # The bridge is not yet fixed + assert Direction.NORTH not in bridge_location.exits + + # Examining the bridge doesn't fix it, nor can it be fixed without an engineer. + msg = examine(state, test_world) + assert Direction.NORTH not in bridge_location.exits + + msg = fix(state, test_world) assert msg == "You do not have the required skills to fix the bridge." - bridge_location = test_world.get("classical12") assert Direction.NORTH not in bridge_location.exits + + # The engineer is able to fix the bridge, + # and the fix is saved in the game state. c = classes.Engineer("Tesla") state.party.append(c) - assert callable(action) - msg = action(state, test_world) + msg = fix(state, test_world) assert ( msg == "The engineer uses nearby logs to repair the bridge and provide a safe passage." ) assert Direction.NORTH in bridge_location.exits - assert callable(action) - msg = action(state, test_world) + assert state.state_dict["hbridgestate"] == "fixed" + + # The bridge is already fixed, + # fixing it or examining it doesn't change that. + msg = fix(state, test_world) assert msg == "You have already fixed the bridge." + + msg = examine(state, test_world) + assert msg == "You have already fixed the bridge." + + assert Direction.NORTH in bridge_location.exits + + # Remove North direction to simulate restarting the game + # with bridge fix in the save file. + bridge_location.exits.pop(Direction.NORTH) + + # The bridge doesn't appear fixed, + # but on further examination we learn that it had been fixed, + # so now we know it can be used to exit North. + assert Direction.NORTH not in bridge_location.exits + msg = examine(state, test_world) + assert msg == "You have already fixed the bridge." + assert Direction.NORTH in bridge_location.exits