Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Basic game design question  (Read 2443 times)
0 Members and 1 Guest are viewing this topic.
Offline ribot

Junior Member




Ribot - mobile UI specialist


« Posted 2005-10-09 10:37:41 »

Hi all,

I'm currently designing the core for a personal game project of mine and seem to have got stuck with the simplist of problems. 

[size=12pt]The background[/size]

I have three classes

  • Mouse
  • Cat
  • Dog

all of which extend the Animal class.

There are only two other classes to consider: House which extends Building.

[size=12pt]Class structures[/size]

Animal

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public abstract class Animal {

  public int x,y,qty;

  public Animal(int x, int y, int qty) {
    this.x = x;
    this.y = y;
    this.qty = qty;
  }

}


Mouse

1  
2  
3  
4  
5  
6  
7  
8  
9  
public class Mouse extends Animal {

  public static final int ID = 1;

  public Mouse(int x, int y, int qty) {
    super(x,y,qty);
  }

}


Building

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public abstract class Building {

  // Option 1
  Animal[] pets;
  // Option 2
  ArrayList pets;
  // Option 3
  HashMap pets;

  public Building() {
    // Option 1
    pets = new Animal[2];
    // Option 2
    pets = new ArrayList();
    // Option 3
    pets = new HashMap();
  }

}


House

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class House extends Building {

  public static final int ID = 1;

  public House() {
    super();
    // Option 1 (Array)
    pets = new Animal[]{new Mouse(getX(), getY(), 1)};
    // Option 2 (ArrayList)
    pets.add(new Mouse(getX(), getY(), 1));
    // Option 3 (HashMap)
    pets.put(new Integer(Mouse.ID), new Integer(1));
  }

}


[size=12pt]The problem[/size]

Seen in the Building and House classes are three options for ways to save your pet objects into the house.  At any point though, another mouse could come into the house, bumping up the qty variable value by one of the mouse object.  My question is how do I do this efficiently and what is the best method to use? 

i.e. Using arrays - If a mouse enters the house, I first need to run through the pets array attempting to cast each element to a Mouse, upon success, the qty value is incremented, but using the instanceof or casting seems very inefficient.

Using ArrayLists - nice to use, but I can't seem to find a nice way to pull individual animals out of the list when "building.getPet(id animalID)" is called.

Using HashMap - Each animal type has it's own unique ID - this ID is fed into the key of the hashMap and the value associated with this is the qty variable.  My problem with this is that I would no longer have access to any of the animal's other variables, methods...  If I were to use pets.put(new Mouse(x,y,z,1), new Integer(1)) this would duplicate the qty variable.  :/

Ideally i'd like to use the unique static IDs as a lookup table to the actual class.  So I think I'd use

1  
2  
 
pets.put(new Integer(Mouse.ID), new Mouse(x,y,z,1));


Generally, there won't be more than 20 different type of animal in a building (the norm will be around 3).  What would be the most efficient method to use?

Many thanks.  Any advice would be much appreciated!

ribot

http://ribot.co.uk - design agency focused on mobile
http://www.retrospecs.co.uk - online vintage eyewear store
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2005-10-09 10:53:20 »

What in the name of heck does the quantity variable do? If it's a total count of all animals of that type, you might be better making it a static member of Mouse, Cat and Dog, with an abstract getQuantity in the Animal class. However I think you're looking at this from the wrong angle - either you want to treat all animals the same (in which case have one collection for all Animal-derived classes) and stop trying to get specific Mouse / etc, types only. Or you specifically hold different animals in seperate containers for each type.

Edit: And if you're only expecting about 20ish animals total, stop worrying about efficiency and get a nice clean design. You could do a bogo sort on a 20 animal collection and your computer wouldn't even blink.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline ribot

Junior Member




Ribot - mobile UI specialist


« Reply #2 - Posted 2005-10-09 11:53:19 »

hehe, thanks for the reply OT!

Well, what I meant is that there will be at most 20 different types of animals, but the quantities (number of each type) could easily be 30,000 or so.  I do want to treat all the animals the same - it's just that I need a nice efficient method to bump the number of a specific type's quantity up or down by x.

I'm currently tending towards the HashMap:

where the House uses

1  
pets.put(new Integer(Mouse.ID), new Mouse(x,y,z,1));


, which allows a fast lookup on the unique animal ID (Unless I'm wrong?).

Wink

I then use a snippet of code (contained in Building) to update the number of a type of animal in a House:

1  
2  
3  
4  
5  
public void setAnimalQty(int animalID, int qty) {
  if(stock.containsKey(new Integer(animalID))) {
    ((Animal)stock.get(new Integer(animalID))).setQty(qty);
  }
}

http://ribot.co.uk - design agency focused on mobile
http://www.retrospecs.co.uk - online vintage eyewear store
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #3 - Posted 2005-10-09 12:00:03 »

Aha, you're not creating a Mouse object for each mouse then, it's more like a MousePopulation, which represents a whole group of animals? In that case ignore what I said about static quantities. In that case a Map of some variety would be what you're after.

If you want to do it the old-school way, and your ids will always be ints then you can just use an array as a look up table (where the id indexes into the correct object). But that tends to be more fragile and only really gains you a small amount of speed.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline ribot

Junior Member




Ribot - mobile UI specialist


« Reply #4 - Posted 2005-10-09 12:07:57 »

Excellent!  Thanks for the confirmation OT.

Had thought about going the old skool route, but thought that it would probably be best that I went for something a little less fragile for part of the core of my engine.

Any idea as to the actual speed boost gained by using array lookup tables in relation to maps?  (1 - 3%?)

Cheers,

ribot

http://ribot.co.uk - design agency focused on mobile
http://www.retrospecs.co.uk - online vintage eyewear store
Offline chaosdeathfish

Junior Member


Projects: 1



« Reply #5 - Posted 2005-11-08 23:25:52 »

Or to extend OT's method (in Java 1.5), use an enum (e.g. AnimalType) for your object type, and an EnumMap<AnimalType,Animal> to map your object type to the object itself (which IIRC stores the values as an array so is nice and fast).
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (22 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (67 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (55 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (30 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!