Question: Can anyone help me clean this up? ****UPDATE**** an expert answered the question but the answer provided did not fix all of the recommendations below
Can anyone help me clean this up?
****UPDATE**** an expert answered the question but the answer provided did not fix all of the recommendations below and created more recommendations when ran in a test.
Below is my original code and feedback from the test run The last part will be the experts code.
I have the following feedback for my code. Can anyone please help me with it?
Feedback
Please ensure that you've printed all the required information in the function that displays the game state. This function must be called show_status - and it must display the following details - 1) Which room the player is currently in. 2) What item is present in the room. 3) What items are in the inventory. 4) The directions he can move in from this room. And also remember to call this function from the driver loop that facilitates the gameplay routine. We've seen the work you've put it into this function. Kudos on how far you've come! :) PS. It wouldn't be a bad idea to check a little about how you can print neatly from this manual here - have fun!
Hint: I recommend adding a loop to keep the player instructions displayed while they are playing. (teacher feedback on previous version)
-combine or consolidate room dictionaries to ensure there are not conflicts.
-recommended dictionaries have matching keys and values.
-consolidate my print()functions into a custom function which allows for one event to be executed in the event a specific prompt needs to be made in the system.
- 1.) male sure to define a function that takes (direction, current_room) as parameters and returns new state based on the room directory.
2.) then make sure this function is called only once in a driving while loop -once a move has been made.
3.) take care of cases within your function when you're running outside the map.
#Code
# my name
# Function to show game instructions def show_instructions(): print("Text-Based Adventure Game") print("Collect all items to win the game.") print("Move commands: go South, go North, go East, go West") print("Add to Inventory: get 'item name'")
# Function to show player status def show_status(room, inventory): print("Current room:", room) print("Inventory:", inventory)
# Dictionary linking rooms to one another and linking items to rooms rooms = { 'Great Hall': { 'South': 'Bedroom', 'North': 'Dungeon', 'East': 'Kitchen', 'West': 'Library' }, 'Bedroom': { 'North': 'Great Hall', 'East': 'Cellar', 'item': 'Armor' }, 'Cellar': { 'West': 'Bedroom', 'item': 'Helmet' }, 'Dining Room': { 'South': 'Kitchen', 'item': 'Dragon' } }
# Main function def main(): # Initialize player status room = 'Great Hall' inventory = [] # Show game instructions show_instructions() # Start game loop while True: # Show player status show_status(room, inventory) # Get player command move = input("Enter command: ").split() # Check if player wants to move between rooms if move[0] == 'go': if move[1] in rooms[room]: room = rooms[room][move[1]] else: print("Invalid move") # Check if player wants to get an item elif move[0] == 'get': if 'item' in rooms[room]: item = rooms[room]['item'] inventory.append(item) print("You got the", item) del rooms[room]['item'] else: print("No item in this room") # Invalid command else: print("Invalid command")
# Run main function if __name__ == '__main__': main()
#Expert Answer
# Function to show game instructions
def show_instructions():
print("Text-Based Adventure Game")
print("Collect all items to win the game.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
# Function to show player status
def show_status(room, inventory):
print("Current room:", room)
if 'item' in rooms[room]:
print("Item in room:", rooms[room]['item'])
else:
print("No item in this room")
print("Inventory:", inventory)
print("Directions you can move: ", end="")
directions = [d for d in rooms[room] if d != 'item']
print(", ".join(directions))
# Function to consolidate dictionaries and ensure no conflicts
def consolidate_dict(dicts):
result = {}
for d in dicts:
result.update(d)
return result
# Initialize dictionaries
room1 = {'South': 'Bedroom', 'North': 'Dungeon', 'East': 'Kitchen', 'West': 'Library'}
room2 = {'North': 'Great Hall', 'East': 'Cellar', 'item': 'Armor'}
room3 = {'West': 'Bedroom', 'item': 'Helmet'}
room4 = {'South': 'Kitchen', 'item': 'Dragon'}
# Consolidate dictionaries
rooms = consolidate_dict([room1, room2, room3, room4])
# Function to check if room is in rooms dictionary
def room_exists(room):
return room in rooms
# Function to update current room based on move direction
def update_room(move, current_room):
if move in rooms[current_room]:
return rooms[current_room][move]
else:
return current_room
# Main function
def main():
# Initialize player status
room = 'Great Hall'
inventory = []
# Show game instructions
show_instructions()
# Start game loop
while True:
# Show player status
show_status(room, inventory)
# Get player command
move = input("Enter command: ").split()
# Check if player wants to move between rooms
if move[0] == 'go':
if room_exists(move[1]):
room = update_room(move[1], room)
else:
print("Invalid move")
# Check if player wants to get an item
elif move[0] == 'get':
if 'item' in rooms[room]:
item = rooms[room]['item']
inventory.append(item)
print("You got the", item)
del rooms[room]['item']
else:
print("No item in this room")
# Invalid command
else:
print("Invalid command")
# Run main function
if __name__ == '__main__':
main()
Feedback
You will need a show_status function to print out the game status for the player when he/she lands in a room. This is what you must print in the function - 1) Which room the player is currently in. 2) What item is present in the room. 3) What items are in the inventory. 4) The directions he can move in from this room. After you code this function - make sure you call it in a driver loop that facilitates the gameplay. Good luck!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
