ThunderfootMOB ThunderfootMOB - 1 year ago 88
Java Question

How to create a turn based fight system with multiple enemies?

I'm trying to learn Java at home using online tutorials and documentation. I have very little experience learning a programming language.

I'm trying to create a "turn based" fighting system with multiple enemies. I have created a system using 1 enemy and it works fine, but I can't figure out how to tell the system which enemy I am fighting.

Im not sure how in depth to go but I will try and be as thorough as possible.I am using JetBrains IntelliJ IDE, To try and keep things organized here is the layout (~Packages, *Classes) Also i'm keeping everything public for the most part for now, but IntelliJ lets me know what should and can be set to Private etc. Any tips for posting in the future would be great aswell :P

*BareBones (public abstract class)

*Enemy0 (extends BareBones)

*Enemy1 (extends BareBones)

*PlayerChar (extends BareBones)







Alright, Now for the code

public abstract class BareBones {

private int health;
private int attPower;

public int getHealth() {
return health;

public void setHealth(int health) { = health;

public int getAttPower() {
return attPower;

public void setAttPower(int attPower) {
this.attPower = attPower;

Enemy0, Enemy1, PlayerChar are all just "public class (name) extends BareBones{"


public class MainClass {
public static void main(String[] args) {
Game game = new Game();;


import Assets.PlayerChar;
import BattlePlans.Tutorial;
import java.util.Scanner;

public class Game {

//Scanner for user input
public static Scanner userInput = new Scanner(;
//Instantiate the user's character
public static PlayerChar userHero = new PlayerChar();

void run() {
//Set player stats

//Begin instruction
System.out.print("Welcome, this is a \"Turn Based Combat\" engine");
System.out.print("Let's get through the basics, with your first battle!");


//Method for pauses in between dialogue
public static void contDialogue() {
System.out.print("\nPress [ENTER] to continue...");


public class Tutorial {
//Instantiate our fight system
static FightSystem fightSystem = new FightSystem();
//Instantiate our first enemy
static Enemy0 enemy0 = new Enemy0();
//Are we in battle?
private static boolean fighting = true;

//Create our tutorial method
public static void tutorial(){

//Assign our enemy its stats

//Tutorial Dialogue
System.out.print("\n1 = Attack");

//Fight System
while (fighting) {
if (Game.userHero.getHealth() <= 0 || enemy0.getHealth() <= 0) {
fighting = false;
if (Game.userInput.hasNextInt()) {
int input = Game.userInput.nextInt();
if (!(input == 1 || input == 2 || input == 3 || input == 4)) {
} else if (input == 1) {
if (!fighting) {
if (Game.userHero.getHealth() <= 0) {
} else if (enemy0.getHealth() <= 0) {

private static void youWon() {
System.out.print("\nCongratulations!\nYou have defeated the enemy!");

private static void youLost() {
System.out.print("\nSorry but you have lost!");


public class FightSystem {

public static void fightInput() {
System.out.print("\n1 = Attack");

private void statusUpdate() {
System.out.print("\n(HP = " + Game.userHero.getHealth() + "|| (ENEMY HP = " + Tutorial.enemy0.getHealth() + ")");

private void enemyAttack() {
System.out.print("\nThe enemy's attack does " + Tutorial.enemy0.getAttPower() + " damage!");
Game.userHero.setHealth(Game.userHero.getHealth() - Tutorial.enemy0.getAttPower());

public void fight1() {
System.out.print("\nYour attack does " + Game.userHero.getAttPower() + " damage to the enemy!");
Tutorial.enemy0.setHealth(Tutorial.enemy0.getHealth() - Game.userHero.getAttPower());

Answer Source

You could put your enemies into an ArrayList, and use the indices in the list as the fight number:

public ArrayList<Enemy> enemies;

public Tutorial()
    enemies = new ArrayList<Enemy>();
    // other constructor stuff

The fight function can look something like this then:

public void fight(int i)
    System.out.print("\nYour attack does " + Combat.userHero.getAttPower() + " damage to the enemy!");
    Tutorial.enemies[i].setHealth(Tutorial.enemies[i].getHealth() - Combat.userHero.getAttPower());
    // other fight stuff

Then in the game loop you can modify the fight checking code to something like:

if (!(input == 1 || input == 2 || input == 3 || input == 4)) {
} else {

If you want to create different kinds of enemies, you should look into polymorphism and inheritance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download