1

Okay, so this may be a repeat, but I personally haven't seen anything on the internet or in Stackoverflow about this.

I am working on a game project and I have been trying to make a text-based game.

In this game, I have a switch statement, for when the user enters a command.

So far I have things for Inventory and Look (Look around the environment), but how do I work with specific things in a switch statement?

For example:

submit = function(input) {
    switch(input) {
        case "LOOK":
            lookaround();
            break;
        case "LOOK AT" + item:
            look();
    }
}

It is the LOOK AT line I am having issues with. I do not know how I can make a string work in that format, unless I had a case for every single item individually, example case "LOOK AT ORANGE" or case "LOOK AT TREE".

I hope I am explaining this thoroughly enough. Can anyone give me some advice?

Thanks

EDIT

I think it is important to note that the user is typing the input into an input box, so the value of the input is going to be a string.

If it will help to see the code I have made, please let me know in the comments below.

EDIT

THANKS FOR YOU HELP GUYS!

I used a regular expression (Thanks @red-devil) and a mixture of slicing. It works perfectly now!

  • 2
    You're trying to define a parser, so you need a way of splitting up your input into tokens. A more advanced approach is to write a full-on lexer [as you might in this example](http://stackoverflow.com/questions/1823612/lexer-written-in-javascript). The idea here is you can define a grammar that can be understood by your program and handle cases like `look at orange` or `look up`. You may want to look at the [Z-Machine](https://en.wikipedia.org/wiki/Z-machine) as an example of how this has been done before. – tadman Sep 30 '15 at 21:55
  • 1 issue is how to deal with an infinite number of cases. but to start, if it's possible, get rid of `"LOOK AT" + item` and just use `case item`. if the case is just LOOK, deal with that before going into the switch. is there an infinite number of cases? – wazz Sep 30 '15 at 22:05
  • Thanks @RedDevil! This helped a lot! – chrisrosewood Oct 01 '15 at 17:45

2 Answers2

0

Switch works with constants, not expressions like 'LOOK AT' + anything.

You could define an object for map any of your cases to your own functions. Like that:

var looks = {
  'lookat-something' : function() {
    alert('something');
  },
  'lookat-other-thing' : function() {
    alert('other thing');
  },
};


var x = 'lookat-other-thing';

looks[x]();

It much more flexible than using switch in any way.

Vlad DX
  • 4,200
  • 19
  • 28
-1

If I understood you right, you want the user to be able to input LOOK AT and then any item name. The problem here is that you have this ominous item variable that could stand for anything and this is not going to work. I would suggest one of these two ways: Going along the lines of your example:

submit = function (input) {
    switch (true) {
        case input == "LOOK":
            alert("Look")
            break;
        case input.startsWith("LOOK AT"):
            alert(input)
            break;
    }
}

if (typeof String.prototype.startsWith != 'function') {
    String.prototype.startsWith = function (str) {
        return this.slice(0, str.length) == str;
    };
}

And secondly, and this is the method I would recommend, you implement a way to parse any input into a command and parameters. A way to do this is to split the input at every space character and then the first value is the command and the rest would be the parameters. This would require you to use a one word command like LookAt and not LOOK AT. So something like this:

function submit(input) {
    var parts = input.split(" ");
    var cmd = parts[0];
    var args = parts.slice(1);

    switch (cmd) {
        case "Look":
            lookAround();
            break;

        case "LookAt":
            lookAt(args[0]);
            break;
    }
}
Patrick Ziegler
  • 787
  • 1
  • 7
  • 20
  • -1 for `switch (true)`. Just because the OP used a switch statement doesn't mean the answers should. That's a chain of `if-else` blocks. – Ian McLaird Oct 01 '15 at 17:52
  • I don't see how `switch(true)` is bad in any way. Every `switch` statement is basically a chain of `if-else` blocks, if you use `break` at the end of every `case`. Also it isn't hurting performance in any way, it may even be faster than `if-else` (https://jsperf.com/if-else-chain-vs-switch-true) – Patrick Ziegler Oct 01 '15 at 19:54
  • It hurts readability. The purpose of a switch statement is to compare a variable to a set of mutually exclusive constants and evaluate which one it matches. You've inverted that relationship (comparing a single constant against a set of evaluations, more than one of which may match). It's simply the wrong control structure to express the idea of checking that value. – Ian McLaird Oct 01 '15 at 21:16