La mejor manera de organizar los datos: número variable de puntos de datos específicos

I'm writing a soccer simulator for fun, and I'm unsure how to organize my player's attribute ratings. I know that the number of attributes will change as I improve the game. I'll need to access each individually, i.e.

SoccerPlayer.getSpeed()
SoccerPlayer.getShooting()

I also know that I'll want to be able to average them with a single function to get an overall rating.

SoccerPlayer.getOverall()

My first inclination is to use a list or array, with constants to define each attribute's index within the array.

static final int SPEED_INDEX = 0;
static final int SHOOTING_INDEX = 1;

static final int NUM_ATTRIBUTES = 2;

int[] attributes = new int[NUM_ATTRIBUTES];

int getSpeed() {
    return attributes[SPEED];
}

int getOverall() {
    int total = 0;

    for (int i : attributes) {
        total += i;
    }

    return (total / attributes.length);
}

That way, whenever I add a new attribute, I only have to change the total constant, and add a new index constant, and the total function will always work. But is there a better way to do this?

preguntado el 25 de agosto de 12 a las 03:08

I'm adding a "Java" tag to this, since one side to the question is "I want to make a class and do something for all fields of the object". The answer would be significantly different in, say, Lisp, from what it is in Java. -

3 Respuestas

You could use an enum for your attributes and a map for the ratings:

enum Attributes {
    SPEED, SHOOTING;
};

private Map<Attributes,Integer> ratings = new LinkedHashMap<Attributes, Integer>();

int getRating(Attribute attribute) {
    return ratings.get(attribute);
}

int getSpeed() {
    return getAttribute(Attributes.SPEED);
}

int getOverall() {
    int total = 0;
    for (int i : ratings.values()) {
        total += i;
    }
    return total / Attributes.values().length;
}

Then to add a new attribute just add it to the enum.

When you're debugging, the ratings will be easier to understand because the attributes have names instead of numbers.

Also, if you want to add something like a scale factor to the attributes (e.g. speed is weighted twice as much as shooting) the enum is a convenient place to put it.

Respondido 25 ago 12, 20:08

I'm surprised that you're ever going to be interested in the sum of a player's attributes. I mean, does it really make sense to add my ability at running to my ability at shooting accurately?

Assuming that you really want to do this, what you've described seems sensible enough.

Respondido 25 ago 12, 19:08

From what I see, your system for an overall ranking is functional. There is one thing however, that you might want to check and make sure is correct. For your ranking to be accurate, 1 point of shooting index must be worth the same competitively as 1 point of speed index. If the changes in speed index are much more valuable then the shooting index, or vise versa, the rankings will be inaccurate. It seems like an elegant design so far though, as long as it is properly balanced.

EG: 4 speed 4 shooting might be able to beat 10 speed 1 shooting every time, but would still be ranked lower. You will have to adjust how much bonus each speed/shooting actually gives someone in order to make sure that the higher ranking team will have a better chance at winning.

Respondido 25 ago 12, 19:08

My goal here isn't to build a comprehensive sim from scratch. I just want to be able to start with a flexible enough structure that I can continue to add complexity without continuously having to redo everything. - Yozarian22

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.