2

I'll try to be as clear as I can about this so that there's no confusion. Also, I'm not looking for a how-to process, just some tips on how to get started and head in the right direction.

I'm relatively new to programming in general (programming in Java for barely 5 months) and I'm barely learning a lot of things about programming. I understand a few concepts already, but I've been trying to understand the concept of MVC (model, view, controller). I've done some research and wanted to apply it to a visual text-based game.

In the game, a player can navigate through a series of "rooms", some that are meant for exploration and some that are what I called "death rooms", where the player ends up dying and has to restart at the first room. The point of the game is to escape and find the exit room, and navigation is controlled using four buttons for each of the four general directions; nothing to difficult for now. However, I'm having trouble understanding how to make the room objects interact with each other. For example, if the player clicks a button to go left, they should be able to go into the room to the left of the room they are currently in. The Model would be in charge updating what room the player is in based on what direction the player wants to go.

In other words, what tips does anyone have to model a series of rooms that interact with other rooms north, south, east, and west of the room the player is currently in? (Hope I am clear enough. If you need clarification, I'll respond to any questions.) Again, I'm looking for advice that will focus my thinking, not a complete how-to. Also, I already have the classes and interfaces set up for the rooms.

LMAR1093
  • 23
  • 4

1 Answers1

3

This is an interesting use case. Model-View-Controller pattern is most often seen in web applications, but you can certainly make it work for text-based games.

Model

In the model layer we would have the entities -- User and Room.

Controller

Controller parses the input, updates the model and passes necessary data for the view. So upon the input 'east' an example controller code could be:

User user = User.current();
Room east = user.getRoom().getEast();
user.move(east);
render(east);

View

Render method would take the new room as the argument and would probably parse some kind of template that displays the name and the description of the new room.

Hope this helps. I personally would use command pattern in this particular case.

Mirko Adari
  • 5,083
  • 1
  • 15
  • 23
  • I think I see what you're getting at. From what I understand, the Controller would be the Listener for the buttons in the game. Depending on which button was pressed, the Controller would execute accordingly and then the new room would be displayed in the View, or GUI. I was going to start out by having a set path to the exit, then adding the other "rooms" once the set path works. I think I have a good idea of where to start now, thanks. – LMAR1093 Feb 28 '13 at 02:23
  • Also, the command pattern looks fairly interesting and seems a bit easier to understand in my opinion. I'll look into that a bit more and see if that would be the best approach for this. – LMAR1093 Feb 28 '13 at 02:29