Elegant command-parsing in an OOP-based text game

前端 未结 5 1404
情话喂你
情话喂你 2021-02-09 01:52

I\'m playing with writing a MUD/text adventure (please don\'t laugh) in Ruby. Can anyone give me any pointers towards an elegant, oop-based solution to parsing input text? <

5条回答
  •  时光取名叫无心
    2021-02-09 02:26

    Ok. So you need a semantic ? (turn is an action, light an object, on an argument... (I relate to your comment to dbemerlin)).

    Why not defining a grammar ? humm... I guess lex and yacc are not a option ? (since it's not OOP at all, but what you want to do is to "compile" user input to produce something - executing some code who change the data of the room, and output a result)

    You can have an OOP design for your object and its action (like, all objects have a .describeMe() method ..) , and beside that, an input parser+compiler.

    Am I completely off the subject?

    Edit : after looking to the interpreter pattern pointed out by Marc Seeman, it seems to be the way to go in you want it in OOP. (but you will somewhat re-invent the wheel)

提交回复
热议问题