Skip to content

Commit 6cab7fe

Browse files
Fix multiple test failures
- Fix pick up command parsing in actions.py - Fix mock patching paths for print_game_over in player tests - Fix pytest.mock.call import issue in text formatting tests - Add proper input validation to format_inventory function - Improve test coverage and reliability Progress: 134 passing, 21 failing (was much higher initially)
1 parent 036c54b commit 6cab7fe

30 files changed

+1722
-1311
lines changed

.coverage

52 KB
Binary file not shown.

game/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
Game package for Kevin's Adventure Game.
3+
"""
4+

game/actions.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,62 @@
22
Game actions module - handles all player actions and commands.
33
"""
44

5-
from game.player import (
6-
add_item_to_inventory,
7-
remove_item_from_inventory,
8-
move_player,
9-
heal_player,
10-
damage_player
11-
)
12-
from game.world import (
13-
get_current_location,
14-
get_location_description,
15-
get_available_locations,
16-
change_location,
17-
interact_with_location,
18-
is_location_accessible
19-
)
20-
from game.items import use_item, get_item_description
5+
from game.items import get_item_description, use_item
6+
from game.player import (add_item_to_inventory, damage_player, heal_player,
7+
move_player, remove_item_from_inventory)
8+
from game.world import (change_location, get_available_locations,
9+
get_current_location, get_location_description,
10+
interact_with_location, is_location_accessible)
2111
from utils.text_formatting import print_invalid_action
2212

2313

2414
def perform_action(player, world, action):
2515
"""
2616
Process and execute player actions.
27-
17+
2818
Args:
2919
player (dict): Player state dictionary
30-
world (dict): World state dictionary
20+
world (dict): World state dictionary
3121
action (str): Action command from player
3222
"""
3323
action = action.lower().strip()
34-
24+
3525
# Movement actions
3626
if action.startswith("go ") or action.startswith("move "):
3727
location = action.split(" ", 1)[1].title()
3828
handle_movement(player, world, location)
39-
29+
4030
# Inventory actions
41-
elif action.startswith("take ") or action.startswith("pick up "):
31+
elif action.startswith("take "):
4232
item = action.split(" ", 1)[1] if " " in action else ""
4333
handle_take_item(player, world, item)
4434

35+
elif action.startswith("pick up "):
36+
item = action[8:] # Remove "pick up " prefix
37+
handle_take_item(player, world, item)
38+
4539
elif action.startswith("drop "):
4640
item = action.split(" ", 1)[1] if " " in action else ""
4741
handle_drop_item(player, world, item)
48-
42+
4943
elif action.startswith("use "):
5044
item = action.split(" ", 1)[1] if " " in action else ""
5145
handle_use_item(player, world, item)
52-
46+
5347
# Information actions
5448
elif action in ["look", "examine", "describe"]:
5549
handle_look(player, world)
56-
50+
5751
elif action in ["inventory", "inv", "items"]:
5852
handle_inventory(player)
59-
53+
6054
elif action in ["status", "health", "stats"]:
6155
handle_status(player)
62-
56+
6357
# Location interaction
6458
elif action in ["interact", "explore", "search"]:
6559
interact_with_location(world, player)
66-
60+
6761
# Default case
6862
else:
6963
print_invalid_action(action)
@@ -73,7 +67,7 @@ def handle_movement(player, world, location):
7367
"""Handle player movement between locations."""
7468
current_location = get_current_location(world)
7569
available_locations = get_available_locations(world)
76-
70+
7771
if location in available_locations:
7872
if change_location(world, location):
7973
move_player(player, location)
@@ -89,7 +83,7 @@ def handle_take_item(player, world, item):
8983
"""Handle taking items from the current location."""
9084
current_location = get_current_location(world)
9185
location_items = world["locations"][current_location].get("items", [])
92-
86+
9387
if item in location_items:
9488
add_item_to_inventory(player, item)
9589
world["locations"][current_location]["items"].remove(item)
@@ -120,12 +114,12 @@ def handle_look(player, world):
120114
description = get_location_description(world, current_location)
121115
items = world["locations"][current_location].get("items", [])
122116
connections = get_available_locations(world)
123-
117+
124118
print(f"\n{description}")
125-
119+
126120
if items:
127121
print(f"Items here: {', '.join(items)}")
128-
122+
129123
print(f"You can go to: {', '.join(connections)}")
130124

131125

game/items.py

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import random
22

3-
from game.player import (
4-
add_item_to_inventory,
5-
damage_player,
6-
heal_player,
7-
move_player,
8-
remove_item_from_inventory,
9-
)
10-
from game.world import change_location, get_all_locations, get_available_locations
3+
from game.player import (add_item_to_inventory, damage_player, heal_player,
4+
move_player, remove_item_from_inventory)
5+
from game.world import (change_location, get_all_locations,
6+
get_available_locations)
117
from utils.random_events import generate_random_event
128

139

@@ -30,10 +26,11 @@ def get_item_description(item):
3026
"ancient_artifact": "A mysterious object from a long-lost civilization. Its purpose is unknown.",
3127
"magic_ring": "A ring imbued with magical properties. Its effects are yet to be discovered.",
3228
"mysterious_potion": "A vial containing a strange, swirling liquid. Its effects are unknown.",
33-
"sword": "A well-crafted sword with a sharp blade. Useful for combat and self-defense."
29+
"sword": "A well-crafted sword with a sharp blade. Useful for combat and self-defense.",
3430
}
3531
return item_descriptions.get(item, "A mysterious item.")
3632

33+
3734
def use_item(player, item, world):
3835
if item not in player["inventory"]:
3936
print(f"You don't have {item} in your inventory.")
@@ -70,17 +67,21 @@ def use_item(player, item, world):
7067
current_location = world["current_location"]
7168
if current_location == "Cave":
7269
print("You light the torch, illuminating the dark cave around you.")
73-
world["locations"]["Cave"]["description"] += " The cave is now well-lit by your torch."
70+
world["locations"]["Cave"][
71+
"description"
72+
] += " The cave is now well-lit by your torch."
7473
return True
7574
else:
7675
print("You light the torch. It provides warmth and light.")
7776
return True
7877
elif item == "gemstone":
79-
print("You examine the gemstone closely. It glimmers with an otherworldly light.")
78+
print(
79+
"You examine the gemstone closely. It glimmers with an otherworldly light."
80+
)
8081
if world["current_location"] == "Village":
8182
print("A merchant notices your gemstone and offers to buy it for 50 gold!")
8283
choice = input("Do you want to sell the gemstone? (y/n): ").lower()
83-
if choice == 'y':
84+
if choice == "y":
8485
player["gold"] += 50
8586
remove_item_from_inventory(player, item)
8687
print("You sold the gemstone for 50 gold.")
@@ -89,11 +90,15 @@ def use_item(player, item, world):
8990
return True
9091
elif item == "rope":
9192
if world["current_location"] == "Mountain":
92-
print("You use the rope to safely navigate a treacherous part of the mountain.")
93+
print(
94+
"You use the rope to safely navigate a treacherous part of the mountain."
95+
)
9396
heal_player(player, 5)
9497
print("Your climbing technique improves, and you feel more confident.")
9598
else:
96-
print("You coil and uncoil the rope. It might be useful in the right situation.")
99+
print(
100+
"You coil and uncoil the rope. It might be useful in the right situation."
101+
)
97102
return True
98103
elif item == "pickaxe":
99104
print("You swing the pickaxe, but there's nothing here to mine.")
@@ -115,35 +120,50 @@ def use_item(player, item, world):
115120
remove_item_from_inventory(player, item)
116121
return True
117122
elif item == "ancient_coin":
118-
print("You flip the ancient coin. As it spins in the air, you feel a strange energy...")
119-
if generate_random_event(events=[("teleport", 50), ("reveal_secret", 50)]) == "teleport":
123+
print(
124+
"You flip the ancient coin. As it spins in the air, you feel a strange energy..."
125+
)
126+
if (
127+
generate_random_event(events=[("teleport", 50), ("reveal_secret", 50)])
128+
== "teleport"
129+
):
120130
new_location = random.choice(get_all_locations(world))
121131
change_location(world, new_location)
122132
move_player(player, new_location)
123-
print(f"The coin vanishes and you find yourself teleported to {new_location}!")
133+
print(
134+
f"The coin vanishes and you find yourself teleported to {new_location}!"
135+
)
124136
else:
125137
print("The coin glows and reveals a secret about your current location!")
126138
# You might want to add some location-specific secrets here
127139
remove_item_from_inventory(player, item)
128140
return True
129141
elif item == "hermit's_blessing":
130-
print("You invoke the hermit's blessing. A warm, comforting light envelops you.")
142+
print(
143+
"You invoke the hermit's blessing. A warm, comforting light envelops you."
144+
)
131145
heal_player(player, 50)
132146
print("You feel completely refreshed and your mind is clear.")
133147
remove_item_from_inventory(player, item)
134148
return True
135149
elif item == "sword":
136150
print("You swing the sword, practicing your combat moves.")
137151
if world["current_location"] == "Forest":
138-
print("Your sword slices through some thick vines, revealing a hidden path!")
152+
print(
153+
"Your sword slices through some thick vines, revealing a hidden path!"
154+
)
139155
# update_world_state(world, "reveal_hidden_path")
140156
return True
141157
elif item == "gold_coin":
142158
print("You flip the gold coin. It catches the light, shimmering brilliantly.")
143159
if world["current_location"] == "Village":
144-
print("A street vendor notices your coin and offers you a mysterious potion in exchange.")
145-
choice = input("Do you want to trade the gold coin for the potion? (y/n): ").lower()
146-
if choice == 'y':
160+
print(
161+
"A street vendor notices your coin and offers you a mysterious potion in exchange."
162+
)
163+
choice = input(
164+
"Do you want to trade the gold coin for the potion? (y/n): "
165+
).lower()
166+
if choice == "y":
147167
remove_item_from_inventory(player, item)
148168
add_item_to_inventory(player, "mysterious_potion")
149169
print("You traded the gold coin for a mysterious potion.")
@@ -153,19 +173,29 @@ def use_item(player, item, world):
153173
elif item == "silver_necklace":
154174
print("You hold up the silver necklace, admiring its craftsmanship.")
155175
if world["current_location"] == "Mountain":
156-
print("The necklace begins to glow, revealing hidden runes on nearby rocks!")
176+
print(
177+
"The necklace begins to glow, revealing hidden runes on nearby rocks!"
178+
)
157179
print("You discover a secret path leading to a hidden cave.")
158180
# update_world_state(world, "reveal_hidden_cave")
159181
else:
160182
print("The necklace sparkles beautifully, but nothing else happens.")
161183
return True
162184
elif item == "ancient_artifact":
163-
print("You examine the ancient artifact closely, turning it over in your hands.")
164-
if generate_random_event(events=[("wisdom", 40), ("curse", 30), (None, 30)]) == "wisdom":
185+
print(
186+
"You examine the ancient artifact closely, turning it over in your hands."
187+
)
188+
if (
189+
generate_random_event(events=[("wisdom", 40), ("curse", 30), (None, 30)])
190+
== "wisdom"
191+
):
165192
print("Suddenly, knowledge of the ancient world floods your mind!")
166193
print("You gain insight into the history of this land.")
167194
# update_player_knowledge(player, "ancient_history")
168-
elif generate_random_event(events=[("wisdom", 40), ("curse", 30), (None, 30)]) == "curse":
195+
elif (
196+
generate_random_event(events=[("wisdom", 40), ("curse", 30), (None, 30)])
197+
== "curse"
198+
):
169199
print("A dark energy emanates from the artifact, making you feel weak.")
170200
damage_player(player, 10)
171201
print("You quickly put the artifact away, feeling drained.")
@@ -176,22 +206,26 @@ def use_item(player, item, world):
176206
print(f"You're not sure how to use the {item}.")
177207
return False
178208

209+
179210
def get_available_items(world, location):
180211
return world["locations"][location]["items"]
181212

213+
182214
def add_item_to_world(world, location, item):
183215
if item not in world["locations"][location]["items"]:
184216
world["locations"][location]["items"].append(item)
185217
print(f"A {item} has been added to {location}.")
186218
else:
187219
print(f"There's already a {item} in {location}.")
188220

221+
189222
def remove_item_from_world(world, location, item):
190223
if item in world["locations"][location]["items"]:
191224
world["locations"][location]["items"].remove(item)
192225
return True
193226
return False
194227

228+
195229
def transfer_item(player, world, item, from_inventory_to_world=True):
196230
current_location = world["current_location"]
197231

game/mythical.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def summon_mythical_creature(world, player, creature_type):
2020
print("A graceful unicorn materializes before you!")
2121
add_item_to_inventory(player, "unicorn_hair")
2222
elif creature_type == "dragon":
23-
print("A powerful dragon descends from the sky! The dragon is friendly and will help you.")
23+
print(
24+
"A powerful dragon descends from the sky! The dragon is friendly and will help you."
25+
)
2426
add_item_to_inventory(player, "dragon_scale")
2527
else:
2628
print(f"Unknown creature type: {creature_type}")

game/player.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,41 @@ def create_player(name):
77
"health": 100,
88
"inventory": [],
99
"location": "Village",
10-
"gold": 100
10+
"gold": 100,
1111
}
1212

13+
1314
def get_player_status(player):
1415
return f"Health: {player['health']} | Inventory: {format_inventory(player['inventory'])} | Gold: {player['gold']}"
1516

17+
1618
def add_item_to_inventory(player, item):
17-
player['inventory'].append(item)
19+
player["inventory"].append(item)
1820
print(f"You picked up: {item}")
1921

22+
2023
def remove_item_from_inventory(player, item):
21-
if item in player['inventory']:
22-
player['inventory'].remove(item)
24+
if item in player["inventory"]:
25+
player["inventory"].remove(item)
2326
print(f"You dropped: {item}")
2427
return True
2528
else:
2629
print(f"You don't have {item} in your inventory.")
2730

31+
2832
def move_player(player, new_location):
29-
player['location'] = new_location
33+
player["location"] = new_location
3034
print(f"You moved to: {new_location}")
3135

36+
3237
def heal_player(player, amount):
33-
player['health'] = min(100, player['health'] + amount)
38+
player["health"] = min(100, player["health"] + amount)
3439
print(f"You healed for {amount} health. Current health: {player['health']}")
3540

41+
3642
def damage_player(player, amount):
37-
player['health'] = max(0, player['health'] - amount)
43+
player["health"] = max(0, player["health"] - amount)
3844
print(f"You took {amount} damage. Current health: {player['health']}")
39-
if player['health'] == 0:
45+
if player["health"] == 0:
4046
print("You have been defeated.")
4147
print_game_over()

0 commit comments

Comments
 (0)