Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am looking to make a program that will allow me to test units for balance when in combat. The idea is that there will be two teams of so many units of different types. I would loop through a battle system that would pick units, have them attack, etc, and then see which team won and do this many many times for an average.

The thing I am having issues with is how to design or make the classes for the units. I tried making a class for a generic unit and then classes that pulled from it for each different unit. The issue was when I called a unit from a team, tried to add one etc. I need to know what the unit type was, that was I couldn't write one function that understood all the unit classes.

I am looking to make a system for the units that allows me to have each unit have the same basic stats, health, speed, etc. But that each unit will also have their own functions for abilities that would be different then other units. It would also allow me to make a method where I can say get a unit from an arraylist and call its get speed method and it wouldn't demand I know the unit class. For instance, grunt g = new grunt() instead something where I can get the unit and just call, unit.getspeed();

Any help or ideas for how I can do this, please ask for more information if needed.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Your units need to inherit from a single parent class.

class grunt extends unit

Then you can just do something like

unit u = grunt() 

and call e.g.

u.getspeed()

Then, you can test with

if(grunt.class == u.class)

if u is a grunt, and then cast it to

((grunt)u).pokeStuffWithPointyStick(); 

to call functions specific to the grunt class.

So, you have a variable of the parent class and just put in a child class. This variable can use all functions of the parent class and can be casted to specific classes to use specific functions of specific classes.

share|improve this answer
add comment

maybe something like this:

import java.util.*;
abstract class Unit {
    Unit() {
        this(10, 1);
    }
    Unit(int health, int firepower) {
        this.health = health;
        this.firepower = firepower;
    }
    boolean isAlive() {
        return health > 0;
    }
    void fireAt(Unit unit) {
        unit.health -= firepower;
    }
    public String toString() {
        return "" + health;
    }
    int health;
    int firepower;
}
class Grunt extends Unit {}
class Marksman extends Unit {
    Marksman() {
        super(10, 2);
    }
}
class Team {
    boolean fire(Team other) {
        for (Unit unit : units) {
            if (unit.isAlive()) {
                boolean foundAliveOpponent=false;
                for (Unit opponent : other.units)
                    if (opponent.isAlive()) {
                        unit.fireAt(opponent);
                        foundAliveOpponent=true;
                        break;
                    }
                if(!foundAliveOpponent)
                    return true;
            }
        }
        return false;
    }
    public String toString() {
        String s = "";
        for (Unit unit : units)
            s += unit.health + " ";
        return s;
    }
    Set<Unit> units = new HashSet();
}
public class Main {
    public static void main(String[] args) {
        Team red = new Team();
        red.units.add(new Grunt());
        red.units.add(new Grunt());
        Team blue = new Team();
        blue.units.add(new Marksman());
        blue.units.add(new Grunt());
        while (true) {
            System.out.println("blue attacks");
            if (blue.fire(red)) {
                System.out.println(red);
                System.out.println("blue wins");
                break;
            }
            System.out.println(red);
            System.out.println("red attacks");
            if (red.fire(blue)) {
                System.out.println(blue);
                System.out.println("red wins");
                break;
            }
            System.out.println(blue);
        }
    }
}
share|improve this answer
    
Thanks, that is an interesting setup. –  Tempus35 Dec 18 '11 at 21:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.