Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top