Click to See Complete Forum and Search --> : Strike:
TheLinuxDuck
01-22-2001, 12:10 PM
hey dood. have you finished the Map Class of the java 'hunt the Wumpus'? Im curious to see how you implemented the Room class in it. I tried rewriting mine from scratch using a player class and a map class, but I can't seem to figure out how to do an array of the room class for a map.. if that's even how it's supposed to be done...
So, at any rate, I'd like to see what you've got, or even some insight into building a map class containing multiple rooms of the room class. Thingie. Stuff. And Deals.
------------------
TheLinuxDuck
I have a belly button.
:wq
nanode
01-22-2001, 03:03 PM
TLD:
in case you don't already know, use an ArrayList or Vector to hold a collection of Objects. If you are just storing int, float etc. the old fashioned array still works.
private Vector v = new Vector();
...
//later on
v.add(new WumpusRoom());
...
//whenever you 'get' it, cast it back to the original type
for(int i=0;i<v.size();i++){
map.addRoom((WumpusRoom)v.get(i));
}
[This message has been edited by nanode (edited 22 January 2001).]
TheLinuxDuck
01-22-2001, 03:33 PM
Originally posted by nanode:
in case you don't already know, use an ArrayList or Vector to hold a collection of Objects. If you are just storing int, float etc. the old fashioned array still works.
Ok, that makes sense.. I was trying to make an array of the class.. which didn't work.. well, at least, it let me make the array, but when I tried to access array slices, it buggered out on me.
Your example doesn't totally make sense to me, but I'm also kinda dense when it comes to java.. http://www.linuxnewbie.org/ubb/smile.gif
If you don't mind babying me through this http://www.linuxnewbie.org/ubb/smile.gif, here is the (simplified) room classes:
public class Room {
// private data, functions, blah blah
private String description;
public static void printDescription() {
System.out.println(description);
}
}
So, what I want to do is to create the class for a map, which contains an array (or ArrayList, or whatnot) of rooms. What do I need to do to set this up? (Or am I thinking of this the wrong way?)
public class Map {
private ArrayList map=new ArrayList();
//
// Basic map fill constructor
//
public Map(int sizeOfMap) {
for(int i=0;i<sizeOfMap;++i)
map.add(new Room());
}
public static void displayRoomInfo(int roomNumber) {
(Room)map.get(roomNumber).printDescription());
}
}
I realize I've probably got this all wrong, since I'm not used to this whole 'OOD' mindset.. http://www.linuxnewbie.org/ubb/smile.gif Thanks for your patience and assistance!!
------------------
TheLinuxDuck
I have a belly button.
:wq
Strike
01-22-2001, 04:06 PM
No, sorry, I haven't yet. But thanks to nanode, now it ought to be easier http://www.linuxnewbie.org/ubb/wink.gif
Yeah, I was out of town for a bit this weekend, and then I had some personal stuff to take care of. And then I spent all day Sunday doing homework (Adv Calculus, Fluid Mechanics, and Theoretical CS... yippee), not to mention doing stuff for work.
I don't know when I'll get this done, but I hope what I did post gave you some good ideas or insight or something.
TheLinuxDuck
01-22-2001, 04:15 PM
Originally posted by Strike:
I don't know when I'll get this done, but I hope what I did post gave you some good ideas or insight or something.
Well, it did make me think of this in a completely different light. I'm just not sure how to go about getting up the Map class from the Room class.. it's a little hazy, but hopefully some answers will be had today. http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
nanode
01-22-2001, 06:29 PM
TLD:
- map has a collection of room class(es)
- room can print description
Here's my version of your map class:
public class Map {
private ArrayList roomList = new ArrayList();
public Map(int rooms) {
for(int i=0;i < rooms;i++) {
roomList.add(new Room());
}
}
public void showRoom(int i) {
((Room)map.get(i)).printDescription());
}
}
Gee, I went through everything from scratch and it's just like yours, aside from the down cast syntax was wrong.
That last line is kind of hairy, so you could always create a new room for clarity:
Room r = (Room)map.get(i);
r.PrintDescription();
TheLinuxDuck
01-22-2001, 06:42 PM
Originally posted by nanode:
Gee, I went through everything from scratch and it's just like yours, aside from the down cast syntax was wrong.
That last line is kind of hairy, so you could always create a new room for clarity:
Room r = (Room)map.get(i);
r.PrintDescription();
Well, if you wrote it out almost like I did, then I must have done something right!! http://www.linuxnewbie.org/ubb/smile.gif Dat's qool! http://www.linuxnewbie.org/ubb/smile.gif I'm going to take a look at this more tomorrow when I get back in to work.. I like the idea of creating a new room that is a particular room from the map, and then printing that description, but I also don't have any problems with casting as the line you put in did. http://www.linuxnewbie.org/ubb/smile.gif
Qool.. I think Java is starting to make some sense to me now! http://www.linuxnewbie.org/ubb/smile.gif
I'm going to rehack my hunt the wumpus using this new found knowledge, and see how it turns out. http://www.linuxnewbie.org/ubb/smile.gif
Rock on! http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
Strike
01-22-2001, 07:07 PM
Wow, sounds like you have got a good start on revamping your code. I don't know what to say about my ideas for the classes, really. There's probably a better scheme out there, but this seemed to fit okay. I just thought of the components of the game, and how they fit in with each other, and it seemed basically to me that it was just a Player walking through Rooms in a Map. There were no Bat or Wumpus or Pit classes, because those could be properties of a given room since they are generally static (though the location of something other than the player changes, right? is it the wumpus? actually a Wumpus class itself would work fine now that I think of it, hmmm...)
Anyway, that's just a little illustrative of my train of thought when I was designing it.
Stuka
01-23-2001, 01:19 AM
Hmmm...I've been thinking about this, but haven't had time to work on it...I was kinda thinkin' like strike...basically a Map that was a collection of Rooms. Wumpus, Bat, and Pit would be properties of the Room class, and the Map class might have methods like moveWumpus, movePlayer, etc. to manipulate the non-static items. That would lower the number of objects, keep responsibility for game mechanics in the Map class, and, I think, simplify the class interactions. But I could be wrong...