May assignment is asking me to build a program in PyChart that makes it possible for the player to visit each room and select the items in that room to fight a giant at the end. In order to win the game you have to collect all of the items from all of the rooms. When I run the code this is the result: Text Adventure Game *************************** Welcome to the Giant text game, Collect all of the items to defeat the Giant! To move: South, North, East, West To get items: get ’item name’ *************************** You are in the Basement Item: You see Armor Enter a move: When I input the direction to go north the next result comes up correctly. However when I tell it to get and item (which is the command written into the code I get this: Invalid direction! Please enter a valid direction. *************************** You are in the Basement Item: You see Armor Enter a move: This is my complete program code, can someone please tell me what I am doing wrong. #Functions showing goal of game and move commands def show_instructions(): #print a main menu and the commands print(" Welcome to the Giant Text Game") print("Collect 7 items to win the game, or be eaten by the giant.") print("Move commands: go South, go North, go East, go West") print("Add to Inventory: get 'item name'") #A dictionary to link a room to other rooms rooms = { 'Basement': {'West': 'Entertainment Room', 'North': 'Living Room', 'East': 'Bedroom', 'North': 'Bathroom', 'South': 'Bedroom', 'West': 'Living Room', 'West': 'Study', 'East': 'Living Room', 'North': 'Guest Room', 'East': 'Attic'}, 'Basement': {'West': 'Entertainment Room', 'item': 'Armor'}, 'Entertainment Room': {'North': 'Living Room', 'item': 'Shield'}, 'Living Room': {'East': 'Bedroom', 'item': 'Potion'}, 'Bedroom': {'North': 'Bathroom', 'item': 'Sword'}, 'Bathroom': {'South': 'Bedroom', 'West': 'Living Room', 'West': 'Study', 'item': 'Elixir'}, 'Study': {'East': 'Living Room', 'North': 'Guest Room', 'item': 'Bow & Arrow'}, 'Guest Room': {'East': 'Attic', 'item': 'Giant'} #villian } room = 'Basement' # Function for getting location def get_new_room(room, direction): # Set new location new_room = room # Loop and if valid direction # assign new room for i in rooms: if i == room: if direction in rooms[room]: new_room = rooms[room][direction] # Return new location return new_room def get_item(room): items = { 'Bedroom': 'Potion', 'Entertainment Room': 'Armor', 'Living Room': 'Shield', 'Study': 'Elixir', 'Bathroom': 'Sword', 'Guest Room': 'Bow & Arrow', 'Attic': 'Giant', 'Basement': 'Empty', } return items[room] def instructions(): # Output main menu and instructions print('Text Adventure Game\n' ' ***************************\n' 'Welcome to the Giant text game, ' 'Collect all of the items to defeat the Giant!\n' 'To move: South, North, East, West\n' 'To get items: get ’item name’') # call instructions function instructions() # set blank inventory item = [] # Start gameplay loop while (1): # Output current position and inventory print(' ***************************') print('You are in the', room) print('Item: ', '') if 'item' in rooms[room]: print('You see ' + rooms[room]['item']) if len(item) != 6 and 'item' == 'Giant!': print("If you come at the Giant, you best not miss...YOU LOSE!") exit(0) if len(item) == 6 and 'item' == 'Giant!': print("Congratulations! You have defeated the Giant!") exit(0) # Prompt user to enter direction direction = input('Enter a move: \n'). lower() directions = ['North', 'East', 'South', 'West'] directions = ['get Armor', 'get Shield', 'get Potion', 'get Elixir', 'get Bow & Arrow', 'get Sword'] # Initialize loop to check for valid movement between rooms if direction == 'East' or direction == 'West' or direction == 'North' or direction == 'South': new_room = get_new_room(room, direction.capitalize()) # If invalid direction, prompt for new direction if new_room == room: print('There is no doorway, please enter a new direction!') # if valid direction change room else: room = new_room elif direction == str('Get' + 'item'): if rooms[room]['item'] in inventory: print('You have already retrieved this item!! Pick another direction!') else: inventory.append(rooms[room]['item']) # If invalid command, prompt user to enter new direction else: print('Invalid direction! Please enter a valid direction.')
May assignment is asking me to build a program in PyChart that makes it possible for the player to visit each room and select the items in that room to fight a giant at the end. In order to win the game you have to collect all of the items from all of the rooms. When I run the code this is the result:
Text Adventure Game
***************************
Welcome to the Giant text game, Collect all of the items to defeat the Giant!
To move: South, North, East, West
To get items: get ’item name’
***************************
You are in the Basement
Item:
You see Armor
Enter a move:
When I input the direction to go north the next result comes up correctly. However when I tell it to get and item (which is the command written into the code I get this:
Invalid direction! Please enter a valid direction.
***************************
You are in the Basement
Item:
You see Armor
Enter a move:
This is my complete program code, can someone please tell me what I am doing wrong.
#Functions showing goal of game and move commands
def show_instructions():
#print a main menu and the commands
print(" Welcome to the Giant Text Game")
print("Collect 7 items to win the game, or be eaten by the giant.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
#A dictionary to link a room to other rooms
rooms = {
'Basement': {'West': 'Entertainment Room', 'North': 'Living Room', 'East': 'Bedroom', 'North': 'Bathroom', 'South': 'Bedroom', 'West': 'Living Room', 'West': 'Study', 'East': 'Living Room', 'North': 'Guest Room', 'East': 'Attic'},
'Basement': {'West': 'Entertainment Room', 'item': 'Armor'},
'Entertainment Room': {'North': 'Living Room', 'item': 'Shield'},
'Living Room': {'East': 'Bedroom', 'item': 'Potion'},
'Bedroom': {'North': 'Bathroom', 'item': 'Sword'},
'Bathroom': {'South': 'Bedroom', 'West': 'Living Room', 'West': 'Study', 'item': 'Elixir'},
'Study': {'East': 'Living Room', 'North': 'Guest Room', 'item': 'Bow & Arrow'},
'Guest Room': {'East': 'Attic', 'item': 'Giant'} #villian
}
room = 'Basement'
# Function for getting location
def get_new_room(room, direction):
# Set new location
new_room = room
# Loop and if valid direction
# assign new room
for i in rooms:
if i == room:
if direction in rooms[room]:
new_room = rooms[room][direction]
# Return new location
return new_room
def get_item(room):
items = {
'Bedroom': 'Potion',
'Entertainment Room': 'Armor',
'Living Room': 'Shield',
'Study': 'Elixir',
'Bathroom': 'Sword',
'Guest Room': 'Bow & Arrow',
'Attic': 'Giant',
'Basement': 'Empty',
}
return items[room]
def instructions():
# Output main menu and instructions
print('Text Adventure Game\n'
' ***************************\n'
'Welcome to the Giant text game, '
'Collect all of the items to defeat the Giant!\n'
'To move: South, North, East, West\n'
'To get items: get ’item name’')
# call instructions function
instructions()
# set blank inventory
item = []
# Start gameplay loop
while (1):
# Output current position and inventory
print(' ***************************')
print('You are in the', room)
print('Item: ', '')
if 'item' in rooms[room]:
print('You see ' + rooms[room]['item'])
if len(item) != 6 and 'item' == 'Giant!':
print("If you come at the Giant, you best not miss...YOU LOSE!")
exit(0)
if len(item) == 6 and 'item' == 'Giant!':
print("Congratulations! You have defeated the Giant!")
exit(0)
# Prompt user to enter direction
direction = input('Enter a move: \n'). lower()
directions = ['North', 'East', 'South', 'West']
directions = ['get Armor', 'get Shield', 'get Potion', 'get Elixir', 'get Bow & Arrow', 'get Sword']
# Initialize loop to check for valid movement between rooms
if direction == 'East' or direction == 'West' or direction == 'North' or direction == 'South':
new_room = get_new_room(room, direction.capitalize())
# If invalid direction, prompt for new direction
if new_room == room:
print('There is no doorway, please enter a new direction!')
# if valid direction change room
else:
room = new_room
elif direction == str('Get' + 'item'):
if rooms[room]['item'] in inventory:
print('You have already retrieved this item!! Pick another direction!')
else:
inventory.append(rooms[room]['item'])
# If invalid command, prompt user to enter new direction
else:
print('Invalid direction! Please enter a valid direction.')
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images