# Designing a unit combat system (java)

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();

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.

• and maybe unit.fight(unit u) is abstract, so every single type has its own way of fighting – Hobbamok Mar 19 '19 at 12:49

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();
Team blue = new Team();
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);
}
}
}