Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Single User Dungeon
#1
Alright, so I am a total beginner at Python and I'm working on a project that should look something like this (with italic text being user input):

You realize you left your phone at home.
Look around the different rooms to find phone
You enter room Entry Way.
You see in this room: car keys.
Where do you want to go? (N, S, W, E) E

You enter room Living Room.

You see in this room: paintings, DVDs, nice couches.
Where do you want to go? (N, S, W, E) E

You enter room Kitchen.

You see in this room: a group of people talking.
Where do you want to go? (N, S, W, E) E

You can't go in that direction. Try another

direction (N, S, W, E): W

You enter room Living Room.
You see in this room: paintings, DVDs, nice couches.
Where do you want to go? (N, S, W, E) W

You enter room Entry Way.

You see in this room: car keys.
Where do you want to go? (N, S, W, E) [i]N

[/i]You enter room Dining Room.

You see this in the room: phone
Congrats, you found it!
Now go back to where you started.
Where do you want to go? (N, S, W, E) S

You enter room Entry Way.
You see this in the room: car keys.
Now you can go about the rest of your day!


So far I've got something like this with some notes explaining what I need to accomplish and what questions I have:


special_item = "phone"

#configuration looks like this:
#       dining_room
#           ||
#       entry_room = living_room = kitchen

entry_way = ["Entry Way", ["car keys"], []]
living_room = ["Living Room", ["paintings", "DVDs", "nice couches"], []]
dining_room = ["Dining Room", [special_item], []]
kitchen = ["Kitchen", ["a group of people talking"], []]

# connect rooms based on their relationship to
# North, South, West, East
entry_way[2] = [dining_room, 0, 0, living_room]
living_room[2] = [0, 0, entry_way, kitchen]
dining_room[2] = [0, entry_way, 0, 0]
kitchen[2] = [0, 0, living_room, 0]

# print_message takes in a room and based on what's
# inside the room, will print out the appropriate
# message to describe the room
def print_message(room):
   print("You enter " + room + ".")
# How would I print the next line ("You see in this room: car keys)?

# Get the player's choice.
# Validate the choice by making sure it's a room they can go to
# The parameter "rooms" is the list of possible connections for a room.
# Return a room based on the player's choice
# For example, rooms of entry_way would be:
# rooms = [dining_room, 0, 0, living_room]
# If the player chooses 'N', get_player_choice should return
# dining_room.
#
def get_player_choice(rooms):
   # get the players choice of what direction they want to go
   choice = input("Where do you want to go? (N, S, W, E) ")
   print(" ")
   # we're going to translate the string input to a

   # number that means a particular direction
   # -1 is an invalid direction
   # 0 is north
   # 1 is south
   # 2 is west
   # 3 is east
   direction = -1

   while direction == -1:
       if choice == "N" or choice == "n":
           direction = 0
       elif choice == "S" or choice == "s":
           direction = 1
       elif choice == "W" or choice == "w":
           direction = 2
       elif choice == "E" or choice == "e":
           direction = 3

       if direction == -1:
           choice = input("Please enter N, S, W, E to move. ")
       elif rooms[direction] == 0:
           choice = input("You can't go in that direction. Try another direction (N, S, W, E): ")
           direction = -1
   return rooms[direction]

def main():
   # start the adventure
   # and let the player know what they're looking for

   print("You realize you left your " + special_item + " at home.")
   print("Look around the different rooms to find " + special_item)

   # initialize the current_room to be the starting room
   # initialize the item_found boolean variable to False
   current_room = entry_way
   item_found = False
   # While the special item is being searched for,
   # print a description of the current_room and
   # check if the special item is in the current_room.
   # Otherwise ask the player to make a choice.
   # will need to use the functions print_message() and get_player_choice()
   #    and think about when you're in the room with the item or when you're not
   #
  while not item_found:
   if current_room != dining_room:
       #print_message(?)
       #get_player_choice(?)
#Do I have to write the code above for each room individually?
   print("Congrats you found it!")
   print("Now go back to where you started.")
   # After finding special item, player goes back to the room where they started.
   # use a while loop?
# call the main() function to start the program
main()
Any help from some of you Python wizards would be immensely appreciated. I'm not quite used to this kind of thinking just yet.
Reply
#2
Please use code tags in the future, it makes it much easier to read what's going on. I've edited your post to include them this time.

Ok, so after looking through your code, I think you should use a dict to hold your rooms, instead of individual variables.  There's a couple places (like print_message()) where you try to accept a room as a parameter, but since it's difficult to pass different variables without having massive if/else blocks, a dict would make your life so much easier.

For example:
# connect rooms based on their relationship to
# North, South, West, East
all_rooms = {
   "entry_way": ["Entry Way", ["car keys"], ["dining_room", None, None, "living_room"]],
   "living_room": ["Living Room", ["paintings", "etc..."], [None, None, "entry_way", "kitchen"]],
   "dining_room": ["Dining Room", [], [None, "entry_way", None, None]],
    "kitchen": ["Kitchen", [], [None, None, "living_room", None]]
}
Each room is just a key to that dict.  The third element of the dict's values are the adjoining room's keys.  This way, you can move from room to room very easily, and add features very easily.  For example, print_message() (since I already mentioned it) would look like this:
def print_message(room):
    global all_rooms
    current_room = all_rooms[room]
    print("You enter {}.".format(current_room[0]))
Reply
#3
Some points on your code. You ask about printing the room contents. The join method would be useful there:
>>> ', '.join(['spam', 'spam', 'eggs'])
'spam, spam, eggs'
You have a lot of if statements like:
if choice == 'N' or choice == 'n':
    direction = 0
Note that this can be simplified:
if choice.lower() == 'n':
    direction = 0
That's the standard way to do a case-insensitive comparison in Python. However, I note that you are mapping to indexes for another list. This can be done easily with the find method of strings:
direction = 'nswe'.find(choice.lower())
Find will return the index in 'nswe' of choice, which is lower cased to match 'nswe'. So, if choice is 's', find will return 1. If choice is not in 'nswe', find will return -1, which is what your code is expecting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Single User Dungeon tckay 1 3,050 Apr-30-2017, 09:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020