• Post Reply Bookmark Topic Watch Topic
  • New Topic

Text Adventure Command Parser Exercise - Trying to do it the OOP way.  RSS feed

 
Derrik Curran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is below. It works, but I don't like it. What is the "best" way to create a text command parser for a text adventure game, from an object-oriented standpoint? Obviously opinions will differ, but opinions are what I'm looking for. I have had no classes or formal training and I don't want to develop bad habits. Also, note, none of these classes are finished, of course. Thanks in advance. If anyone needs to see the rest of the code, it is located here:

http://www.derrikc.com/java/SimpleGame/GameServer.java <-- this is the file the code below is from, but it won't compile without CharacterManager
and Character, and it's not really worth running without also using the client.
http://www.derrikc.com/java/SimpleGame/CharacterManager.java
http://www.derrikc.com/java/SimpleGame/Character.java
http://www.derrikc.com/java/SimpleGame/GameClient.java

 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Derrik,

if the syntax or grammar of your game "language" is rather complex, it's usually easier and more robust to define a syntax and grammar and let tools like Antlr, Beaver, JFlex etc. auto-generate a lexer and parser class you can use in your code. Of course there are alse straight forward ways to implement lexers and parser "by hand" but it can get a bit messy very quickly if your language to parse grows!

As an advice: In any case it would be a good starting point to clearly define and write down the game language you want to support!

Marco
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

That question is too difficult for the beginner's forum. Moving.
 
Derrik Curran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let me apologize for the late reply. I have been extremely sick and I spent some time in the hospital. At any rate, thanks for the replies, and sorry about posting in the wrong section. I guess I misjudged the difficulty of my question.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Derrik,

that's no problem at all. Hope in good health again

And in this case I think the forum wasn't completely wrong but it would be hard to expect good answers in a beginners forum. That said I'd still be glad to help you with any questions. In fact that's a really interesting topic. At least if you don't plan to implement an ugly "parser logic" with multiple nested if-else statements

Just let me know when you've decided how to proceed.

Marco
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!