Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    38
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Data structures for a text adventure

    Hello, I'm a noob interested in learning how to make text adventures in javascript. This is a development of a previous post:
    Simple OOP applications?

    The two links below go about describing locations in their games differently.

    This one (a much larger application): http://williamedwardscoder.tumblr.com/post/21865366869/how-to-make-a-text-adventure uses object objecct literal notation.
    (Here's the file at github.)

    Whereas as this one: http://www.davidgunter.ca/portfolio/textgame/ uses an object prototype that accepts parameters. (Here's the file on github.)

    ... I'd like to hear thoughts and opinions on which is a better approach to creating locations for this type of game. Also I realize that some approaches may work better depending on the scale, that's a consideration I'm interested in.

    Also: I rather dislike the use of the numeric array "globalLocations" in the second example. It seems confusing in terms of the development of the game to have to remember/refer to an arbitrary number, when the point of a text-based game is to describe things with words.

    Thanks for your thoughts!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts
    ... I'd like to hear thoughts and opinions on which is a better approach to creating locations for this type of game.
    I’d prefer objects with a prototype, if there are methods that are to be shared between the objects. otherwise it does not matter.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Jun 2013
    Posts
    38
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    I’d prefer objects with a prototype, if there are methods that are to be shared between the objects. otherwise it does not matter.
    yeah that makes sense. or if large amounts of the code for each object are redundant in some other way I suppose...

  • #4
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    I would assume the literal method is faster to load, if everything needed is within the declaration.
    However, the constructor method, in addition to the prototype, allows you to create objects dynamically

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <div id="div1"></div>
    <script>
    function employee() {
    for (i = 0; i < arguments.length; i++) {
    	var theProp = arguments[i];
    	this[theProp] = arguments[++i];
    	}
    	
    }
    
    x = new employee('firstName', 'Joe', 'lastName', 'Doe');
    for (i in x) {
        var str = i + ' = ' + x[i]
    	document.getElementById('div1').innerHTML += str + "<br />";
    }
    
    </script>
    </body>
    </html>
    Where the world once stood
    the blades of grass
    cut me still

  • #5
    New Coder
    Join Date
    Jun 2013
    Posts
    38
    Thanks
    5
    Thanked 0 Times in 0 Posts
    @ Vincent Puglia: Cool! I'll have to play with that a little, bit it is new to me.

    Question: I am not sure how to locate objects when using the object prototype. With the literal syntax, for example, I can define an object:
    Code:
    locations = {
    	yard : {
    		title: "Yard",
    		description: "You are in the Yard. You can go east.",
    		exits : {
    			north : -1,
    			east : "foyar",
    			south : -1,
    			west : -1
    		},
    		items: ["lamp"],
    		points: 5
    	},
            // etc..
    }
    and locate info like this:
    Code:
    currentLocation = locations[currentLocation]["exits"][direction]
    ... but I'm not really sure how to arrange and locate objects when I define them from a prototype:

    Code:
    function item(){
        this.id = undefined;
        this.title = "";
        this.description = "";
    	this.points = 0;
    	this.canTake = false;
    }
    
    var glasses = new item();
    glasses.id = 1;
    glasses.title = "Glasses";
    glasses.description = "A scratched up pair of glasses. They aren't your prescription";
    glasses.canTake = true;
    Can you help me on that?
    Last edited by GregoryG; 12-19-2013 at 12:23 AM.

  • #6
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Glad you liked it. However, this is not a 'prototype'; it's an object constructor. (The prototype is demonstrated at the end of the code.

    Code:
    var items = [];
    function item(){
        this.id = arguments[0];
        this.title = arguments[1];
        this.description = arguments[2];
    	this.canTake = arguments[3];
    	this.points = 0;
    	items.push(this);
    }
    
    function getItemByProperty (prop, val) {
        for (i = 0; i < items.length; i++){
    	if (items[i][prop] == val) {
    		var theItem = items[i]
    		alert(theItem.description)
    	}
        }
    }	
    
    
    var glasses = new item(1,"Glasses","A scratched up pair of glasses. They aren't your prescription", true);
    var pin = new item(2, "Pin", "A very sharp pin, with a hint of dark red on the tip.", true);
    getItemByProperty ('id', 2) 
    
    
    item.prototype.worth = null;
    pin.worth = 100;
    getItemByProperty ('worth', 100) 
    alert(items[0].worth)
    not the best way to do this, but it demonstrates things relatively well
    Where the world once stood
    the blades of grass
    cut me still

  • #7
    New Coder
    Join Date
    Jun 2013
    Posts
    38
    Thanks
    5
    Thanked 0 Times in 0 Posts
    ok... lots to think about. thanks! I think for now I'm just going with literals to learn about functionality more deeply.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •