Simple Text Adventure help

This is the place for queries that don't fit in any of the other categories.

Simple Text Adventure help

Postby XndrK » Tue Nov 05, 2013 6:47 pm

So I'm making a very simple text adventure, so when I make a complex one, I know where to start. I have a few classes defined as follows.

Code: Select all
class Room(object):
   
    def __init__(self, name, doors_tuple, items_list):
        self.name = name
        self.doors_dict = doors_tuple
        self.items_list = items_list
        # Add more attributes as needed

    # Add functions as needed

class Item(object):
   
    def __init__(self, name, location):
        self.name = name
        self.location = location
        # Add more attributes as needed, which you probably will

    # Add functions as needed

class Door(object):
   
    def __init__(self, rooms_tuple):
        self.rooms_tuple = rooms_tuple


Now, I want to make a Room instance called living_room. That references some doors, which also reference living_room. That's a problem, because I can't make the living_room reference the door which is referencing living_room, because I can't reference them before they exist.

In other words, I can't do this.

Code: Select all
living_room = Room("living room", (north_door, west_door), [key1])
key1 = Item("closet key", living_room)


Help.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Simple Text Adventure help

Postby Yoriz » Tue Nov 05, 2013 7:35 pm

Maybe something like this
Note door removed to simplify it for now
Code: Select all
class Room(object):

    def __init__(self, name):
        self.name = name
        self.items_list = []


class Item(object):

    def __init__(self, name, location):
        self.name = name
        self.location = location


living_room = Room("living room")
living_room.items_list.append(Item("closet key", living_room.name))
print living_room.items_list[0].name, living_room.items_list[0].location

Output
Code: Select all
closet key living room
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Simple Text Adventure help

Postby XndrK » Wed Nov 06, 2013 11:40 pm

New prob. Is there a way to assign a variable to it "remotely"? I tried it directly, like this:

Code: Select all
my_list = []
my_list.append("Hello World!")
my_string = my_list[0]
my_string = "Moo!"
print my_list[0]


and it printed:

Code: Select all
Hello World!


which isn't what I wanted.

Is there a way I can do it so I can put the same one (for example, I'm going to need to put the doors in multiple places) somewhere else and have the same variable?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Simple Text Adventure help

Postby ochichinyezaboombwa » Thu Nov 07, 2013 1:15 am

XndrK wrote:Is there a way to assign a variable to it "remotely"?

What do you mean?
XndrK wrote:which isn't what I wanted.
-- then what is wanted?
XndrK wrote:Is there a way I can do it so I can put the same one (for example, I'm going to need to put the doors in multiple places) somewhere else and have the same variable?

What do you mean?

Give a clear example or/and be more clear / explicit while describing your problem.

Code: Select all
l = ["door", "wall", "door", "ceiling", "fireplace", "door", "wall"]
print l
l[2] = "chair"
print l

Is this what you wanted?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Simple Text Adventure help

Postby Kebap » Thu Nov 07, 2013 9:19 am

XndrK wrote:New prob. Is there a way to assign a variable to it "remotely"?


Hi XndrK, please watch out, as you might run into one of the problems mentioned in my sig, for example

Describe the goal, not the step

If you are trying to find out how to do something (as opposed to reporting a bug), begin by describing the goal. Only then describe the particular step towards it that you are blocked on.

Often, people who need technical help have a high-level goal in mind and get stuck on what they think is one particular path towards the goal. They come for help with the step, but don't realize that the path is wrong. It can take substantial effort to get past this.

Stupid: How do I get the color-picker on the FooDraw program to take a hexadecimal RGB value?

Smart: I'm trying to replace the color table on an image with values of my choosing. Right now the only way I can see to do this is by editing each table slot, but I can't get FooDraw's color picker to take a hexadecimal RGB value.

The second version of the question is smart. It allows an answer that suggests a tool better suited to the task.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Simple Text Adventure help

Postby XndrK » Thu Nov 07, 2013 3:51 pm

Well excuuuuuuuse me! >:(

I'm trying to make a text adventure game (I already mentioned that) and I'm trying to figure out how to give two rooms a door, or a door two rooms. I tried using the list.append() method you suggested with items to doors, but if I try to assign a variable to the item in the object's list, I get a clone instead. Is there a way to assign a variable to the object in the list?

Seriously, though. You didn't have to be brats about it. I'm tempted to leave the forum after what ochichinyezaboombwa (whatever that means) did.

Kudos to Kebap, though, for being civil. I'm sorry about the badly worded question.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Simple Text Adventure help

Postby Mekire » Thu Nov 07, 2013 4:11 pm

XndrK wrote:Seriously, though. You didn't have to be brats about it. I'm tempted to leave the forum after what ochichinyezaboombwa (whatever that means) did.

I doubt ochichinyezaboombwa's response was meant nearly as offensively as you may be taking it. We are a rather brusque bunch. If you read the entirety of the site to which Kebap pointed you, you will confirm this.

[H]ackers have a reputation for meeting simple questions with what looks like hostility or arrogance. It sometimes looks like we're reflexively rude to newbies and the ignorant. But this isn't really true.

The A:B problem (asking about a specific implementation you have tried, rather than asking about how to accomplish your goal) is of specific annoyance to many of us.

Rest assured that everyone does indeed want to see you grow in to a competent programmer.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Simple Text Adventure help

Postby Kebap » Thu Nov 07, 2013 5:31 pm

XndrK wrote:I'm trying to figure out how to give two rooms a door, or a door two rooms. I tried using the list.append() method you suggested with items to doors

I don't quite understand this. Sorry I can't help currently.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Simple Text Adventure help

Postby Yoriz » Thu Nov 07, 2013 6:57 pm

Say a room could have doors in the North, South, East & West.
The doors connecting rooms together would have 2 rooms.
A Livingroom with a Kitchen to the South of it would have a South door that has a South room(the Kitchen) & a North room(the Livingroom.
John Doe gets placed in the Livingroom and goes through the South door, he would end up in the Kitchen.

Heres some code that creates the Livingroom & Kitchen & a door that connects between them in a North/South relationship.
John Doe starts in the kitchen and goes through the South door, Joe Bloggs just hangs out in the Kitchen.
Code: Select all
class Room(object):

    def __init__(self, name):
        self.name = name
        self.people = set()
        self.north_door = None
        self.south_door = None
        self.east_door = None
        self.west_door = None

    def person_enters_room(self, person):
        self.people.add(person)
        person.room = self
        print '{} has entered the {}'.format(person.name, self.name)

    def enter_south_door(self, person):
        if not self.south_door:
            print 'Sorry {} there is no south door in this room'.format(
                person.name)
            return
        entered_room = self.south_door.enter(self)
        self.people.discard(person)
        print '{} has left the {} and entered the {}'.format(person.name,
            self.name, self.south_door.south_room.name)
        entered_room.person_enters_room(person)

    def whos_in_the_room(self):
        people = ', '.join(person.name for person in self.people)
        print 'The following people are in the {}: {}'.format(self.name,
            people)


class NorthSouthDoor(object):
    def __init__(self, north_room=None, south_room=None):
        self.north_room = north_room
        self.south_room = south_room
        if north_room:
            north_room.south_door = self
        if south_room:
            south_room.north_door = self

    def enter(self, room):
        if room == self.north_room:
            return self.south_room
        return self.north_room


class Person(object):
    def __init__(self, name):
        self.name = name
        self.room = None

    def what_room_im_in(self):
        print '{} is in the {}'.format(self.name, self.room.name)

    def enter_south_door(self):
        self.room.enter_south_door(self)

living_room = Room("livingroom")
Kitchen = Room('Kitchen')
NorthSouthDoor(living_room, Kitchen)
john_doe = Person('John Doe')
joe_bloggs = Person('Joe Bloggs')
living_room.person_enters_room(john_doe)
Kitchen.person_enters_room(joe_bloggs)
john_doe.what_room_im_in()
joe_bloggs.what_room_im_in()
living_room.whos_in_the_room()
Kitchen.whos_in_the_room()
print ''
john_doe.enter_south_door()
john_doe.what_room_im_in()
joe_bloggs.what_room_im_in()
living_room.whos_in_the_room()
Kitchen.whos_in_the_room()
print ''
joe_bloggs.enter_south_door()

Output
Code: Select all
John Doe has entered the livingroom
Joe Bloggs has entered the Kitchen
John Doe is in the livingroom
Joe Bloggs is in the Kitchen
The following people are in the livingroom: John Doe
The following people are in the Kitchen: Joe Bloggs

John Doe has left the livingroom and entered the Kitchen
John Doe has entered the Kitchen
John Doe is in the Kitchen
Joe Bloggs is in the Kitchen
The following people are in the livingroom:
The following people are in the Kitchen: John Doe, Joe Bloggs

Sorry Joe Bloggs there is no south door in this room


Note: Theres obviously various ways of doing this and you could decide that you dont actually need to model the doors themself and just have the rooms themself connect to each other
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Crimson King, tnknepp, Tosp, Yoriz and 4 guests