1

I am trying to create a MUD as fun project to practice JS (I am a total noob but have been inspired to learn more by codecademy!) and I was hoping that I could get some advice on how to go about representing my dungeon in JS.

The features I would like to have for my dungeon are:

  1. Dungeon should have a random number of rooms (within specified parameters)
  2. Rooms should be automatically populated with monsters
  3. (We can ignore how the rooms are connected for now)

Seeing as JS is OO language I thought that the best way to represent the dungeon would be as an object:

var dungeon = {
    var room1:roomA,
    var room2:roomB,
    var room3:roomC
}

//RoomX is an object created with the following constructor

function Room(){
    this.description: "Some text",
    this.monsters:[monster1,monster2,monster3],
    this.treasure:[treasure1,treasure2,treasure3]  
}

The plan is to then create methods for the Room object allowing a player to interact with the stuff in the room.

Questions:

1) Is this a good idea/will it work?

2) How do I generate a random number of properties for an object?

Thanks!

Textmode
  • 509
  • 3
  • 18

2 Answers2

3

Sounds like a good idea to me in general, but your description of dungeon doesn't really make sense. You can make dungeon to be an object. Your example is almost right but you need to get rid of the var, as such:

var dungeon = {
  'room1': roomA,
  'room2': roomB
  ...
}

This can be accessed through dungeon.room1, or dungeon['room1']

But it would be unnecessarily complicated to generate a random number of rooms for this. You would have to get the last element, get the number from the end, increase by one and only than add another room.

How about instead you define dungeon to be an array and than generating a random number of rooms is easy:

//This generates a random number between min and max
var rand = Math.floor(Math.random() * (max - min) + min);

var dungeon = new Array(rand);

for (var i = 0; i < dungeon.length; i++) {
  //This depends on how do you want to generate random rooms
  dungeon[i] = randRoom();
}

Your constructor for Room is also not fully correct. If I was to rewrite that in proper javascript it would be:

function Room(){
  this.description = "Some text";
  this.monsters = [monster1,monster2,monster3];
  this.treasure = [treasure1,treasure2,treasure3];
}

This is initialised like so:

var room = new Room();

And you can access all the fields:

room.description // returns "Some text"
room.monsters    // returns [monster1,monster2,monster3];

: are used when creating objects with object literals {} as in the first example I gave. For objects defined with constructors you use =.

mck
  • 2,040
  • 3
  • 19
  • 29
  • Aha yes I think I understand. I was mixing up the object literal and constructor notations. The reason I was using `var` was to make each room a private property that was only accessible using a getter function which I was still yet to make. That might well be overkill for this project. – Stanislav Beremski Feb 29 '12 at 23:48
  • In fact having done a bit more reading around the subject it seems that you can only create private vars using constructors and not object literals. – Stanislav Beremski Mar 01 '12 at 00:00
  • Yes, I believe that's the case. There is quite a bit of information about it at http://javascript.crockford.com/private.html. – mck Mar 01 '12 at 15:43
3

This is a fairly open-ended question - there are lots of ways to do this that could all be equally "right". But a couple of points:

Your object literal syntax is invalid - you need to remove the var keyword from your property names so that it looks like this:

var dungeon = {
     room1:roomA,
     room2:roomB,
     room3:roomC
} 

But I think you'd be better off with an array of rooms:

var dungeon = [ roomA, roomB, roomC ];

Because then you can easily vary the number of rooms. Assuming you are generating the rooms randomly somehow you can easily add new rooms to the array with dungeon.push(new Room()).

Further, if it so happens that your rooms will be in a regular grid you might like to use an array of arrays to represent this:

var dungeon = [ [ roomA, roomB, roomC ],
                [ roomD, roomE, roomF ],
                [ roomG, roomH, roomI ] ];

Such that the right-middle room would be accessed as dungeon[1, 2] (row 1, column 2, noting that array indexes are zero-based).

Your assignment syntax within your function is also incorrect: you need to say

this.propertyName = someValue;
// NOT
this.propertyName : someValue;

Colons : are used for property creation in object literal { property : value } syntax, but the equals sign = is used for other assignments variableOrProperty = value.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241