3

I'm very new to Java and am not sure if I'm even allowed to ask questions of this nature here but I'll give it a shot. I want/need practice programming Java so I've decided on a rather large project that should be within my capabilities I've started building it and have created a player object with some properties like level,name,xp,health and items. The items are and array of 5 object objects (bear with me XD) now I'd like to know if I should create the rooms as objects as well or as seperate classes or functions?

The rooms will be only rooms in an abstract sense the game is fully text-based they will need to contain puzzles and objects a user can pick up or examine as well as enemies and such the narrative will also be contained in each one. The player will be able to "pick up" items in the room and add them to their inventory. As well as participate in textual comabt with enemies like : you hit monster for 2 damage, monster hits back for 3 etc.

Remember that this isn't going to be pretty it's for practice and I need to do it within Java. Thanks in advance.

6
  • Can you give more details? What is the purpose of a room and how will it be interacting with your other elements?
    – CHawk
    Nov 25, 2011 at 3:05
  • hmmm in a typical dungeon crawler the user will go through a series of challanges and puzzles to get somewhere or to a goal look here for an example homestarrunner.com/dman3.html though this one is done in flash. The rooms will conatain objects and enemies that the player cna interact with as well as a narrative. Nov 25, 2011 at 3:06
  • You've given no information that can be used to answer your question. As @M.H said, what is a "room", and how do you intend to use whatever it is? Please edit your question to provide more details - we can't read your screen (or your mind) from here. :) And a link to an external site and saying "browse around here for an example of what I mean" isn't a suitable detail.
    – Ken White
    Nov 25, 2011 at 3:08
  • haha sorry I'm working on it here :( tunnel-vision Nov 25, 2011 at 3:09
  • This may not be what you want to hear, but if you're struggling with the difference between objects, classes and functions then going ahead and doing a project may not be within your capabilities yet and you should consider doing some reading or some beginner tutorials first. The phrase "this isn't going to be pretty, it's for practice" doesn't bode well for your future as a programmer imho... even (or especially) if it's for practice, don't make excuses, try to get it right, make it pretty/clean/elegant/readable... ;-) Nov 25, 2011 at 3:22

2 Answers 2

4

Your problem is related to the OOP concepts not to being a beginner in java, you should know more about OOP to think correctly before starting coding to put the correct OOP design for your program. since you are building this program in java you can start here.

For what I've understand from you question, you may want to have a Room Class, Containing the Room Properties and Functions.

4
  • oop design is not a one-way train, once you read and understand the concepts so you can put any design you think it is "logical" and will solve your problem. Nov 25, 2011 at 3:25
  • OK so mind taking a look at this? package game1; public class Player { int playerLevel =1; int playerLevelCompletion =0; int playerHealth=100; String playerName=null; Object[] playerItems = {null, null,null,null,null}; Player(int playerLevel, int playerLevelCompletion, int playerHealth, String playerName, Object playerItems[]) { this.playerLevel = playerLevel; this.playerLevelCompletion = playerLevelCompletion; this.playerHealth = playerHealth; this.playerName = playerName; this.playerItems = playerItems; } } Nov 25, 2011 at 3:29
  • is there anyway I can get into more depth with this with you? Nov 25, 2011 at 3:39
  • @Suavelizard yes, by asking another question :) , try to write a clear design after reading the concepts deeply and ask a question with clear code and a clear explanation for your idea and logic, and every one will help you to improve it :). Nov 25, 2011 at 12:06
1

From a high level, a good idea would be having a "Room" class, this would contain the Room Title, Description and any other room specific information (such as terrain type etc).

You could then create a multidimensional array of Room objects. One dimension being your X coordinate while the other your Y coordinate.

This way to "move" around all you need to do is track your location and either increment/decrement your X or Y based on the direction you moved.

You could extend it further doing something like generating a map of your surroundings as well. Each terrain type could be displayed as a different symbol (@ for forest, ~ for water etc). It would be a simple approach to add something like this because all you need to do is get your top right/bottom left coordinate and loop through those coordinates building your map.

4
  • I might be way off base with this answer but I used to play a lot of text based games "back in the day".
    – savageguy
    Nov 25, 2011 at 3:16
  • No this is more or less the right track, each room or area would be best as a class rather than an object? I have considered making an actual gameboard style movement but I figured that would be wayy down the line for now its going to be pretty linear Nov 25, 2011 at 3:17
  • An object is an instance of a class. So you would have 1 room class which defines your variables (Room Title, Room Description etc) and constructor for any room. You would then create your rooms, ie roomList[x][y] = new Room("Room at X, Y","This is a room") You'll probably want to read up more on OOP as the other responder mentioned before digging in too much. It might not feel as satisfying as jumping right into code but it will more than make up for it after you get a firm understanding.
    – savageguy
    Nov 25, 2011 at 3:41
  • Thank you so much though and I will... the way my teacher has described classes is... insufficient to say the least Nov 25, 2011 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.