So I'm a bit stumped with this particular problem in making a text-based game

My friend and I are making a text-based game. We have currently settled on not using XML to store all the information until we've finished the project as an assignment (saving time) so we're storing all of the game information inside object literals (JavaScript, var = {variable1: value, variable2: value} etc)

Trouble is, we have most of the game nailed now I think. But we are stumped completely on one thing:
How do we get JavaScript to recognise user commands?

The program can recognise the user input "look" and respond with "you are looking" but I need to make the program able to understand commands like "look at table" or "pick up key" or "open cupboard". I... have no idea how to implement this.

Please help!

is this a web based game?

Will the commands be laid out aka always the same?

Make an if then statements with each command

if (var1.value == "look at table") { 
        do this;
}else if (var1.value == "look at chest"){
        do this;

disclaimer: i am not a programer, this may not be the best way to do this.

The only trouble with that is there could be 100s of objects in the game, and 1000s of combinations of commands. I'm looking for a more elegant solution to the problem than having to make an if statement with 3000 "else if"s

Yeah, it's web based. So JavaScript and some HTML elements.

If you're going to have a bunch of options beyond look you should probably use some short method to split and then have nested code or other methods that handle the remaining functionality (I assume you're not just making a single type of command). It's usually better practice to go with a method then a huge block of code for readabilities sake.

I'm not very familiar with javascript but you should be able to look up a split method for this. I'd assume something like this from a quick google search of javascript methods:

//Split the code along any spaces so that individual words can be checked.
var res = var.split(" ");
// Check if the look command is used, make sure to handle the case where the user makes weird choices with capitalisation.
if (res[0].toUpperCase() === "LOOK" ) {
// Either nested code to handle all instances (probably not the best way of doing this) or some new method.
} else if (check for all possible commands) {
} else {
// Handle improper input

Edit: Forgot to actually address an index of the array.

In response to your reply to Urworstnit3m3r

This really depends on what you need the code to do in response to look commands. If you just need it to output some line of text then there are a number of key:value pair data structures that are quickly searchable that you could populate a number of ways.

In this case you would write something like

// Some method to strip the command part of the input, seems like this might be a way of doing that in javascript
var focus = userinput.toUpperCase().replace("LOOK AT ", "");

From here you'd pick some type of key:value object (seems like a lot of guides of hash tables are around for javascript) and then use that to get the output you wanted by matching your key, the object, to some value that holds its description. We'll assume get(key) returns the value from our hypothetical key:value object and contains returns a boolean of if this object is available in our object.

if({our_hash_object}.contains(focus.toUpperCase())) {
} else {
Some boilerplate text to inform the user that the object they want to look at is not available

I think the .split() function was exactly what I needed!

I think B1gbadwo1f is right, I will need to keep track of a lot of things in the game... I'm going to start working on that.
I think i might need a bigger white board!


I'm not really clear on how your system is designed. However from the sounds of things you probably want to be using callbacks/closures in this case.

For example:

function Action(name) { = name;

Action.prototype.onLook = function(callback, objStr) {, objStr);

function logEvent(objStr) {
console.log( + " at " + objStr);

var a = new Action('Looked');
a.onLook(logEvent, 'table');

You can parse the events as they come in then feed them appropriately to your callbacks.