A
and1000
Hi all,
I feel a bit stupid to ask this question, but I've been working with
this for a while, and I seem to be stuck in a very stupid way of
thinking. I would be very heappy if anybody could point me to a way out
of it. The problem is this: I have a tree structure containing game
data. It's organised as follows: The top of the tree is a scenario
object, and a scenario can contain many teams. Each team contains
several players, and each player contains several objects. Objects in
turn can contain other objects. I thought my problem was one of
traversal at first, but I'm starting to think that what I really want
to do is query the tree. For example, I need to know
- which player owns object with a certain id?
- which team does a certain object belong to?
When traversing the tree, I therefore need information from different
levels, both object level and f.ex team level. I have hard-coded it for
now, because I needed to test other parts of the program. I'll include
it anyway, to show what I mean (I know it's crap, you don't need to
tell me that):
private ITraversable getObject(IObjectModel m, int objectID, String
classname){
List scenarios = m.getContainedElements();
for(Iterator i = scenarios.iterator(); i.hasNext(){
IScenario sc = (IScenario)i.next();
List teams = sc.getContainedElements();
for (Iterator y = teams.iterator(); y.hasNext(){
ITeam t = (ITeam)y.next();
List players = t.getContainedElements();
for(Iterator j = players.iterator(); j.hasNext(){
IPlayer p = (IPlayer)j.next();
if(p.contains(objectID)){
try {
if(Class.forName(classname).isInstance(p)){
return p;
}
else if(Class.forName(classname).isInstance(t)){
return t;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
Then the usage of the method is as follows:
IPlayer owner = (IPlayer)getObject(objectmodel, objectid,
IPlayer.class.getName());
The contains(objectid)-method traverses the tree downwards. The problem
with recursively traversing the tree is that I need to "stop" at
different levels depending on what kind of object I'm asking for (team,
player etc). No objects have references to their parents, only
children. I cannot use id as a comparison value either (e.g return the
first object with this id), because though id's are unique within each
level in the tree, different objects in different levels can have the
same id (e.g a player and a team can both have id 1).
Any help will be greatly appreciated.
Regards
I feel a bit stupid to ask this question, but I've been working with
this for a while, and I seem to be stuck in a very stupid way of
thinking. I would be very heappy if anybody could point me to a way out
of it. The problem is this: I have a tree structure containing game
data. It's organised as follows: The top of the tree is a scenario
object, and a scenario can contain many teams. Each team contains
several players, and each player contains several objects. Objects in
turn can contain other objects. I thought my problem was one of
traversal at first, but I'm starting to think that what I really want
to do is query the tree. For example, I need to know
- which player owns object with a certain id?
- which team does a certain object belong to?
When traversing the tree, I therefore need information from different
levels, both object level and f.ex team level. I have hard-coded it for
now, because I needed to test other parts of the program. I'll include
it anyway, to show what I mean (I know it's crap, you don't need to
tell me that):
private ITraversable getObject(IObjectModel m, int objectID, String
classname){
List scenarios = m.getContainedElements();
for(Iterator i = scenarios.iterator(); i.hasNext(){
IScenario sc = (IScenario)i.next();
List teams = sc.getContainedElements();
for (Iterator y = teams.iterator(); y.hasNext(){
ITeam t = (ITeam)y.next();
List players = t.getContainedElements();
for(Iterator j = players.iterator(); j.hasNext(){
IPlayer p = (IPlayer)j.next();
if(p.contains(objectID)){
try {
if(Class.forName(classname).isInstance(p)){
return p;
}
else if(Class.forName(classname).isInstance(t)){
return t;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
Then the usage of the method is as follows:
IPlayer owner = (IPlayer)getObject(objectmodel, objectid,
IPlayer.class.getName());
The contains(objectid)-method traverses the tree downwards. The problem
with recursively traversing the tree is that I need to "stop" at
different levels depending on what kind of object I'm asking for (team,
player etc). No objects have references to their parents, only
children. I cannot use id as a comparison value either (e.g return the
first object with this id), because though id's are unique within each
level in the tree, different objects in different levels can have the
same id (e.g a player and a team can both have id 1).
Any help will be greatly appreciated.
Regards