solution.pyPython"""Text Adventure — Branch + HP"""
import json
from pathlib import Path
#Scenario inline (actually from a JSON file)
SCENES = {
"start": {
"text": "Entered a dark forest. The path splits into two.",
"choices": {
"1": {"label": "To the left", "next": "left", "effect": {"hp": 0}},
"2": {"label": "To the right", "next": "right", "effect": {"hp": 0}},
},
},
"left": {
"text": "Encountered a wolf! Fight or run?",
"choices": {
"1": {"label": "Fight", "next": "fight", "effect": {"hp": -30}},
"2": {"label": "Run away", "next": "escape", "effect": {"hp": -10}},
},
},
"right": {
"text": "Found an old cabin. Go inside?",
"choices": {
"1": {"label": "Go inside", "next": "treasure", "effect": {"hp": 0, "item": "Sword"}},
"2": {"label": "Pass by", "next": "lost", "effect": {"hp": 0}},
},
},
"fight": {"text": "Defeated the wolf and found treasure! 🏆", "ending": True, "result": "GOOD"},
"escape": {"text": "Safely to the village. Ordinary ending.", "ending": True, "result": "NEUTRAL"},
"treasure": {"text": "Got a sword in the cabin and became a hero! 🏆", "ending": True, "result": "GOOD"},
"lost": {"text": "Lost in the forest and disappeared...", "ending": True, "result": "BAD"},
}
def main():
state = {"hp": 100, "items": []}
current = "start"
while True:
scene = SCENES[current]
print(f"\n{scene['text']}")
print(f"[HP: {state['hp']}, Items: {state['items']}]")
if scene.get("ending"):
print(f"\n=== Ending: {scene['result']} ===")
break
for k, ch in scene["choices"].items():
print(f" {k}. {ch['label']}")
pick = input("> ").strip()
if pick not in scene["choices"]:
print("Invalid selection"); continue
choice = scene["choices"][pick]
effect = choice.get("effect", {})
state["hp"] += effect.get("hp", 0)
if "item" in effect:
state["items"].append(effect["item"])
if state["hp"] <= 0:
print("\n=== HP 0! Game Over ===")
break
current = choice["next"]
if __name__ == "__main__":
main()