1

I am using java to make a game. I want to make a method that makes it easy to create a world with just a couple lines. I want to have a method like this...

public void makeLevel(see following*)
{
if (... == 1
{
drawGrass(0, 16);
}
......
}
  • I want an easy way to have about 100 - 200 inputs right here.

I want to call it like this

makeLevel(1,2,1,1,2,2,1,3,2,1,2,2,1,2,1,2,3,2,2,2,2,1,2,1,2,2,1,2,1,2,1,2,3,2,1,2,3);

I will also have a method called drawGrass and drawWater that will use Graphics2D to draw these pictures.

I don't think that you need my code but if you do just tell me and I will put it here.

I was thinking that i could somehow use an array or somthing like that.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680

6 Answers6

6

Please don't use 100 inputs. Use a data structure (perhaps an array, or a Map or custom class if you want to name your arguments) to hold your data.

Alternatively, you might want to use variadic arguments. For instance:

public void doSomething(int... args) {
    // treat args as an int[]
}

which can be called with:

doSomething(1, 2, 3);
doSomething(1,2,3,4,5,6,7,8);
// etc.

but 100 arguments in calling code is just cruel.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
5

Accept an Array rather than individual inputs.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
4

Put the values into a vector or an array list and pass this object!

Roxxorfreak
  • 380
  • 2
  • 10
  • Vectors are considered obsolete. Obviously that wasn't your point though! – James T Snell Feb 19 '14 at 21:40
  • really? I didn't know that. I just checked the Java 7 Docs and found no word of deprication... how do you know? – Roxxorfreak Feb 19 '14 at 21:52
  • 1
    @Doberman http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Mike B Feb 19 '14 at 21:53
  • I see, and I agree on the usage restrictions, but it's not officially depricated. That restores my faith in my knowlege :-) – Roxxorfreak Feb 19 '14 at 21:57
  • I suppose "obsolete" was a poor choice of words. "discouraged" seems to be it. Meh, I do actually have a few in use in some places myself. I used to use them like a pirate uses their wooden-leg as a toothpick, surprisingly often. – James T Snell Feb 19 '14 at 22:05
  • 1
    Out of topic but the main problem with `Vector` is that most of its methods are synchronized, so it will introduce some overhead even when no synchronization is needed. When you don't need a thread-safe collection go with `ArrayList` (BTW, this is clearly stated on [Vector Javadocs](http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html)) – Anthony Accioly Feb 19 '14 at 22:18
4

A better option it you want to make a friendly API its to use a fluent interface (http://en.wikipedia.org/wiki/Fluent_interface), instead of passing one hundred parameters (that is obviously not a good idea), you can do something like:

import static mylibrary.createALevel;

Level mylevel = createALevel().withWater(...)
                              .withGrass(...)
                              .withXXX(...)
                              .create()

Easy to write and easy to understand, and with other important benefits (for example if some of your configurations are optional)

AlfredoCasado
  • 818
  • 5
  • 7
  • Yeah I would combine API methods with @jrahhali suggestion of leveraging Enums so that we could do things such as `addTerrain(int terrainSize, Terrain terrain)`, `repeat(int times, LevelFragment levelFragment)` or even `addTerrainWithGaps(int terrainSize, Terrain Terrain, int nGaps, Terrain gap, int gapSize)` and easily build levels in a meaningful and way less repetitive way. – Anthony Accioly Feb 19 '14 at 22:12
1

May I suggest an improvement? I think the biggest 'problem' here is firstly, there are too many parameters, and secondly, there are too many 'magic' numbers. Think about the person using this method to create a level. What does the number 3 mean? what about 16? It is not apparent right off the bat.

So, the first improvement I would make is turning those numbers into meaningful types. Perhaps put them in an enum, like:

public enum Terrain
{
    Grass,
    Sand,
    Water,
    Bush
}

If you are familiar with objects and classes, I would take it a step further and would make a parent Terrain class and a bunch of different sub-types, like Grass, Sand, Water, etc. Each sub type would contain it's own image and would know how to draw itself.

The second improvement deals with the large number of parameters. If you are not familiar with classes and objects, then a 2D array of Terrain enum types would probably be best. If you are comfortable with objects, then I would create a World object which would have methods such as Add(Terrain item).

I hope that helps.

jmrah
  • 5,715
  • 3
  • 30
  • 37
0

if you need to categorize the values, try a Map

Mark Giaconia
  • 3,844
  • 5
  • 20
  • 42