diff --git a/ai_prompts/2.txt b/ai_prompts/2.txt index 79b5718..6216569 100644 --- a/ai_prompts/2.txt +++ b/ai_prompts/2.txt @@ -1,9 +1,28 @@ ############################################### Prompt to Kiro AI (Default): - +from now on, I will mostly be asking questions about approach, so please do not perform any actions or code edits unless explicitly requested. also, please be less verbose - I want nice, quick answers that provide the necessary context. How can I check if a file exists/isreadable for my loadSave function? Response: +Two clean options: + +os.path.exists(path) — checks the file is there before opening. + +python + +import os +if os.path.exists(path): + # open and read +try/except — just attempt to open and catch the failure. This is the more Pythonic approach and also handles the "exists but unreadable/corrupt" case in one shot: + +python + +try: + with open(path) as f: + ... +except (FileNotFoundError, IOError): + return defaultState() +For loadSave, the try/except is the better fit since you also want to catch bad JSON — wrap json.load in the same block and catch json.JSONDecodeError too, falling back to a fresh default state on any failure. ############################################### \ No newline at end of file diff --git a/main.py b/main.py index fe0662f..a812cc8 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ # ============================================================================= from cmu_graphics import * +import json # ============================================================================= @@ -44,19 +45,31 @@ def loadCreatures(path="creatures.tsv"): return creatures -def loadSave(path): +def loadSave(path="save.json"): # Load save.json and return the player's saved state (money, owned baits, # owned upgrades, current bait, caught log, etc.). If the file is missing # or unreadable, return a fresh default-state dict so a first-time player # starts cleanly. This is the only place that reads the save file. - pass + defaultState = { + "cash": 0, + "owned_baits": [], + "owned_upgrades": [], + "current_bait": "worm", + "catch_log": [] + } + try: + with open(path, "r") as f: + return json.load(f) + except (FileNotFoundError, IOError): + return defaultState -def writeSave(path, saveState): +def writeSave(saveState, path="save.json"): # Serialize the current player state to save.json. Call this at key moments # (after a sale, after a purchase, on quit) so progress persists between # sessions. Keep the written structure identical to what loadSave expects. - pass + with open(path, "w") as f: #overwrite existing + f.write(json.dumps(saveState)) # =============================================================================