基于OOP的文字游戏中的优雅命令解析 [英] Elegant command-parsing in an OOP-based text game

查看:79
本文介绍了基于OOP的文字游戏中的优雅命令解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Ruby编写MUD/文本冒险游戏(请不要笑).谁能给我任何指向基于oop的优雅解析输入文本的解决方案的指针?

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?

在这里,我们谈论的没有比将魔杖放到桌子上"更复杂的了.但是一切都必须是软的.我想稍后轻松地扩展命令集.

We're talking about nothing more complex than "put wand on table", here. But everything needs to be soft; I want to extend the command set painlessly, later.

我目前的想法,略有简化:

My current thoughts, slightly simplified:

  1. 每个项目类(盒子,桌子,房间,玩家)都知道如何识别属于"它的命令.

  1. Each item class (box, table, room, player) knows how to recognise a command that 'belongs' to it.

游戏类理解一种特定于领域的语言,涉及诸如在对象Y内移动对象X",显示对象X的描述"等动作.

The game class understands a sort of a domain-specific language involving actions such as "move object X inside object Y", "show description of object X", etc.

游戏类询问房间中的每个项目是否识别出输入命令.首先说是胜利.

The game class asks each item in the room if it recognises the input command. First to say yes wins.

然后将控制权传递给处理命令的item类中的方法.此方法在DSL中改写命令,然后将其传递回游戏对象以使其发生.

It then passes control to a method in the item class that handles the command. This method rephrases the command in the DSL, passes it back to the game object to make it happen.

必须有做旧的优雅方法.不过,似乎无法用Google搜索任何东西.

There must be well-worn, elegant ways of doing this stuff. Can't seem to google anything, though.

推荐答案

解释器设计模式是我所知道的最面向对象的解析方法,但是我确信编译器专家会指出更强大的算法.

The Interpreter design pattern is the most object-oriented approach to parsing that I'm aware of, but I'm sure compiler experts will point out algorithms that are more powerful.

这篇关于基于OOP的文字游戏中的优雅命令解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆