• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating rooms for Text Based Adventure Game  RSS feed

 
Scott Giles
Greenhorn
Posts: 4
Chrome Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So recently I've been making a text based adventure game and so far I'm not doing too bad... considering I've no experience with any type of programming. I have basic combat system, EXP and level system. But thats all it is, attack enemy, kill enemy, drink health potion, etc, die and repeat.

What I'd like to do it implement a map and rooms, so the player can use commands like go east, west and encounter different problems. I just have no idea of how to go about this and don't learn to well from reading books. Better at actually doing.. trial and error;D

Any help on this would be greatly appreciated!

I can post the code if thats required

Thanks
SCOTT

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags (←click that link to see how) if you post code. Also, don't post all your code (I imagine there's quite a bit of it by now), just part(s) that are relevant to your specific question(s).
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by laying out how I would interact with a Room object, if that's what you're going to try to do. I'm a big proponent for test-first programming, better known as test-driven development, so I'd actually write some unit tests to start figuring out a good API for Room.

For example, I might first ask questions like, "How do I want to set up a Room? How do I make one Room adjacent to another such that someone exiting one Room enters the adjacent Room?"

To explore the answer to these questions, I'd try to see what the code might look like by writing a test, which is kind of like a hypothesis of a science experiment.


This test outlines a mini "story" for how you'd interact with a Room object and it also documents important aspects of the behavior of the program with respect to Rooms and moving around between them. This story will already give you an idea of how you might write the code in your main program:

The test will fail to even compile at first but that's easily fixed by creating the Room class and adding empty methods to it. When I have a skeleton that will actually compile, I will then slowly add code to make the tests pass. When the tests pass, I build up another part of my "story" about Room behavior. Maybe my next step would be to see what behavior a Room should have if you tried to exit a room in a direction where no adjacent room has been specified.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you go with the test code that I gave earlier. It should lead you to wondering, "Well, now that I'm in the adjacent room, wouldn't it be logical that if I exited it in the opposite direction, I'd end up in the same room that I was in before?" That implies that you need to do something extra in the exit() implementation. I would write a test like this:
 
Scott Giles
Greenhorn
Posts: 4
Chrome Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, really, I've just learnt more about how this kind of stuff works than I have in the past week researching OOP;D I guess I will start to try and create a room class myself. I may come back to ask how I can actually implement that into my main game. I think that I need to separate some of my main code into different classes's too:/

Again, thank you!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above tests would lead me to write code for Room that would look something like this:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please remember that writing good code requires you spend about 80% of your time THINKING, and only 20% TYPING.

If you don't have a very clear idea of what you want to type into your code editor, you're probably not ready to start it up.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Giles wrote:Thank you so much, really, I've just learnt more about how this kind of stuff works than I have in the past week researching OOP

No problem. Good to see that you get it.

I guess I will start to try and create a room class myself.

Well, maybe you get it. Like I said, (hint, hint) I start by writing tests first. The tests help me organize my thoughts about how these objects should behave. Later, the tests provide an important safety net that protects me from screwing up what already works as I add new behaviors and functionality.
 
Scott Giles
Greenhorn
Posts: 4
Chrome Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for taking the time not only to answer but to explain!

Maybe I should invest in a tutor these books just are not sinking in!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:That implies that you need to do something extra in the exit() implementation.

And this shows how your thoughts can be shown to be wrong when you implement your code. The "something extra" that needed to be done, as you can see, was actually in the addAdjacent() method. Good test code will tend to steer you the right way, even when your initial thinking was off track
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Giles wrote:Maybe I should invest in a tutor these books just are not sinking in!

I'm actually working on a book (and maybe some online video tutorials, too) with this kind of content. Let me know if you're interested in reviewing it when I have some of it ready.
 
Scott Giles
Greenhorn
Posts: 4
Chrome Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Scott Giles wrote:Maybe I should invest in a tutor these books just are not sinking in!

I'm actually working on a book (and maybe some online video tutorials, too) with this kind of content. Let me know if you're interested in reviewing it when I have some of it ready.


Great idea, you clearly have a nack for explaining these things in a such a way that a beginner can understand what on earth your talking about! Let me know if and when!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another tip for beginners: you should always think about how you might make your program clearer. Taking a break and coming back to the code and re-reading it out loud can often help you realize there's something "off" about what you've written or see a way to make it even more expressive. Changing your code to clarify it is part of a process called Refactoring.

There are three refactoring techniques that you should think about always and they're often enough to get you 90% of the way to writing and maintaining clear, expressive code. The first two are: Rename and Extract. The third one is Composed Method.

Coming back and re-reading the code I suggested earlier, I started to not like the name addAdjacent. It's "smelly" now to me because it hints at the fact that I have a Collection of adjacent Rooms to which I'm adding another Room. This is called the smell of a "Leaky Abstraction". The name hints at the implementation of establishing the relationship between two Room objects. When an implementation detail leaks out like that you should ask yourself, "How can I convey the INTENT of this method without giving away too much about the details of HOW it works." You want to guard against being in the mindset of HOW instead of WHY or WHAT because the former often leads to code that's not as clear as it could be.

It's a subtle smell but it's there. Now that I've mentioned it and you're aware that it's there, you might even start to smell it, too. That's the funny thing about code smells: you may not notice it at first but when someone tells you about it, then it's hard to not smell it.

I would try different alternatives, like maybe setAdjacentWith(), setAdjacentTo(), makeAdjacentWith(), or makeAdjacentTo(). I find that making the change then reading the new code out loud will help me settle on the one that sounds the most conversational, as though the code is telling a story in a way that's very close to natural, everyday spoken language. Speaking of everyday spoken language, who uses "adjacent" anyway, right? How about setNextTo() then? Yeah, that sounds a lot more conversational.

If I'm going to have a setNextTo() method, then the code might read better if I had a symmetrical isNextTo() method as well.

So, using my IDE's Refactor - Rename capability, I'd refactor the previous code to end up with something like this:

And the test code would become:

Looking at that if condition in setNextTo(), it's kind of clever but my eyes always get stuck on it even when I know what it's doing. To clarify its intent, I'll extract it to its own method and leave a intent-revealing name in its place:

Using Extract refactoring, the setNextTo() method is now a Composed Method, the third refactoring technique.

See how choosing the right names and simple refactoring works to make your code clearer and better?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!