• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rooms containing 0, 1, or 2 objects of different types  RSS feed

 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an assignment in which I'm supposed to try to create a small text based game similar to the old Zork game.
I have to create a dungeon that can have 5 - 10 rooms (chosen at random) and each room except the start room has a 50% chance of having a monster in it. There's also a chance of finding a weapon in any of the rooms regardless of whether or not it already has a monster. A weapon can only be found in one of the rooms, if one exists at all, and there's a 50/50 chance of it being either a stick or a sword.

I have a driver class, a game manager class designed to make the game function, a sword and a stick class each of which inherits from a weapon class, and a monster and a player class each of which inherits from a participant class. (I'm required to set it up this way)

The problem I'm having is trying to figure out how to create rooms that could contain either nothing, a monster, or a monster and a weapon. Since an array of rooms can only contain one thing per index I'm not sure how to have the potential to have both a monster and a weapon in the same room. If both are found in the same room the user has to fight the monster first before they can get the weapon and they can't leave a room with a monster in it. If they revisit a room then nothing should happen - it's already been cleared out. The rooms have to be designed in such a way as to leave all those possibilities open as well. I've heard of hash maps, but have no idea what they are or even how to use them, and wouldn't be allowed to use something we haven't 'actually' learned about. Any suggestions as to how to tackle this?
 
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
Why don't you have a room class?  Also, the Random class has a nextBoolean() method that has about a 50-50 chance of being true (a boolean, after all, can only one of two values).  A Map would be appropriate if you had a key that you mapped to a value. In this case, what would be the key that will uniquely identify a room? Note that you can use an array as a Map, if you consider the index of an element as its key.
 
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 Random nextBoolean() method can be used to simulate something like a coin toss:

Something like this should be useful in your program.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Why don't you have a room class?
Generally we can't make additional classes if they weren't specified as part of the assignment and we weren't told otherwise on this one, so I'm just sticking with what's specified.

Also, the Random class has a nextBoolean() method  that has about a 50-50 chance of being true (a boolean, after all, can only one of two values).
The 50% chance isn't the problem, just setting up the rooms so they can contain both a monster and a weapon as well as a player, but since the player and monster are both derived from  Participants class that's not as much of an issue I don't think...

A Map would be appropriate if you had a key that you mapped to a value. In this case, what would be the key that will uniquely identify a room? Note that you can use an array as a Map, if you consider the index of an element as its key.
Use an array as a map? But how would that allow me to hold two totally different types of objects? I'm not sure I fully understand that. Would that require having another class specifically designed for Rooms? It's limitations like being required to work within very tight guidelines that make some of these projects a pain.
 
Rajith Pemabandu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose you can have a Room class and a Hash map of room objects. but if you want to stick on the guidelines consider an array. you can store different types of objects in an array. This link explains about a approach. http://stackoverflow.com/questions/33181391/array-to-store-objects-of-two-different-classes
 
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
Nikki Smith wrote:
Junilu Lacar wrote:Why don't you have a room class?
Generally we can't make additional classes if they weren't specified as part of the assignment and we weren't told otherwise on this one, so I'm just sticking with what's specified.

This really gets my goat (not you, but the way you're being taught and constrained). It's like you're being put through elephant crushing and now you're so afraid to get docked points that you won't even think about doing what would be obvious to anyone else who knows better in this kind of situation. Why don't you run the idea of a Room class by your instructor first, to see if that was just overlooked or something that he/she thought was implied by the instructions.
 
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
Nikki Smith wrote:Use an array as a map? But how would that allow me to hold two totally different types of objects?

It won't, unless you have an array of objects that can hold references to other objects. Let me see... like a Room object.

I'm not sure I fully understand that.

Because you're now starting to think like a crushed elephant.

Would that require having another class specifically designed for Rooms?

But there's still hope for you because this is what I've been saying.

It's limitations like being required to work within very tight guidelines that make some of these projects a pain.

I think you should just keep asking your instructor questions that explore the limits to what he/she will allow. Keep in mind that, based on what you've said, many of the limitations you're being given are artificial and are only for the sake of keeping your solution in line with what you've been taught so far. It's not the best way, in my opinion (in fact I think it's a pretty bad way to teach), but that's the situation you're in and you're just going to have to deal with it.

Good luck.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're constrained to using an array for this, then you can use a grid representation that looks something like this:

rooms[row]rooms[row][0]rooms[row][1]...rooms[row][N]
MONSTER----
PLAYER----
WEAPON----

Java doesn't have true multi-dimensional arrays, only nested arrays, but for most intents and purposes you can treat the above like a 2D array.

Basically, you'd declare an Object[][] rooms. You'd designate each row to hold a specific type of object. In the array depicted above, your first row (rooms[0]) holds only Monster objects. The second row will hold only Player objects and the third row only Weapon objects.

This will force you to cast your objects and makes you write code that's not ideal. But then again, this solution is not ideal. It's what you are forced to do when you're programming under unreasonable constraints.

You'd have to keep track of which row is which and write code that casts objects based on this organization. I would define symbolic constants like the ones in the first column above. That is,

It's just like adding another "dimension" to a flat array so that it can accommodate different types of objects. It's not the prettiest but if you are truly constrained to using arrays for this instead of a proper Room object, then I think this is the best you can do.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By contrast, if you were allowed to define a Room class, then you'd have something like this:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!