Post

Episode 8: PvE Mode – Fight Against a Bot

Goal: Build a real combat loop where the player battles a bot. The bot will attack or use a skill randomly.

Episode 8: PvE Mode – Fight Against a Bot

1. Project Structure

1
2
3
src/
├── main/
│   └── Main.java         // Updated with game loop

2. Basic PvE Game Loop

Update Main.java to include a turn-based loop with input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import factory.ChampionFactory;
import model.*;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> championNames = Arrays.asList("Garen", "Ahri", "Lux", "Darius");

        System.out.println("Welcome to Java MOBA Console");
        System.out.println("Choose your champion:");

        for (int i = 0; i < championNames.size(); i++) {
            System.out.println((i + 1) + ". " + championNames.get(i));
        }

        System.out.print("Enter the number of your champion: ");
        int choice = scanner.nextInt() - 1;

        if (choice < 0 || choice >= championNames.size()) {
            System.out.println("Invalid choice. Exiting...");
            return;
        }

        Hero player = ChampionFactory.createChampion(championNames.get(choice));
        Hero bot = ChampionFactory.createChampion(championNames.get((choice + 1) % championNames.size()));

        System.out.println("\nYou are: " + player.getName());
        System.out.println("Bot is: " + bot.getName());

        Random random = new Random();

        while (player.getHp() > 0 && bot.getHp() > 0) {
            System.out.println("\n=== Player Turn ===");
            player.processEffects();
            System.out.println(player.getName() + ": " + player.getHp() + " HP");
            System.out.println(bot.getName() + ": " + bot.getHp() + " HP");

            System.out.println("1. Basic Attack");
            for (int i = 0; i < player.getSkills().size(); i++) {
                System.out.println((i + 2) + ". Skill: " + player.getSkills().get(i).getName());
            }

            System.out.print("Your choice: ");
            int action = scanner.nextInt();

            if (action == 1) {
                player.attack(bot);
            } else {
                int skillIndex = action - 2;
                player.useSkill(skillIndex, bot);
            }

            if (bot.getHp() <= 0) break;

            System.out.println("\n=== Bot Turn ===");
            bot.processEffects();

            if (random.nextBoolean() || bot.getSkills().isEmpty()) {
                bot.attack(player);
            } else {
                int skillIndex = random.nextInt(bot.getSkills().size());
                bot.useSkill(skillIndex, player);
            }
        }

        System.out.println("\nGame Over!");
        if (player.getHp() > 0) {
            System.out.println("You win!");
        } else {
            System.out.println("Bot wins!");
        }

        scanner.close();
    }
}

3. BurnEffect.java – Damage over Time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package effect;

import model.Hero;

public class BurnEffect extends StatusEffect {
    private int burnDamage;

    public BurnEffect(int duration, int burnDamage) {
        super(duration);
        this.burnDamage = burnDamage;
    }

    @Override
    public void apply(Hero target) {
        System.out.println(target.getName() + " is burning! Takes " + burnDamage + " damage.");
        target.takeDamage(burnDamage);
    }

    @Override
    public String getName() {
        return "Burn";
    }
}

4. StunEffect.java – Skips Turn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package effect;

import model.Hero;

public class StunEffect extends StatusEffect {

    public StunEffect(int duration) {
        super(duration);
    }

    @Override
    public void apply(Hero target) {
        System.out.println(target.getName() + " is stunned and cannot act!");
        // We'll check this flag in Main or Hero logic later
    }

    @Override
    public String getName() {
        return "Stun";
    }
}

5. HealEffect.java – Heals HP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package effect;

import model.Hero;

public class HealEffect extends StatusEffect {
    private int healAmount;

    public HealEffect(int duration, int healAmount) {
        super(duration);
        this.healAmount = healAmount;
    }

    @Override
    public void apply(Hero target) {
        System.out.println(target.getName() + " is healed for " + healAmount + " HP.");
        target.heal(healAmount);
    }

    @Override
    public String getName() {
        return "Heal";
    }
}

6. Update Hero.java to Support Effects

Update your Hero class to store and process status effects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import effect.StatusEffect;
import java.util.*;

public abstract class Hero {
    // ... existing fields
    protected List<StatusEffect> effects = new ArrayList<>();

    public void addEffect(StatusEffect effect) {
        effects.add(effect);
        System.out.println(name + " is affected by " + effect.getName());
    }

    public void processEffects() {
        Iterator<StatusEffect> it = effects.iterator();
        while (it.hasNext()) {
            StatusEffect effect = it.next();
            effect.tick(this);
            if (effect.isExpired()) {
                System.out.println(effect.getName() + " has worn off.");
                it.remove();
            }
        }
    }

    public void heal(int amount) {
        this.hp += amount;
        System.out.println(name + " now has " + hp + " HP.");
    }

    // existing attack/takeDamage/etc...
}

7. Example Test in Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import effect.*;
import model.*;

public class Main {
    public static void main(String[] args) {
        Hero garen = new Warrior("Garen");
        Hero ahri = new Mage("Ahri");

        ahri.addEffect(new BurnEffect(3, 5));
        garen.addEffect(new HealEffect(2, 10));

        for (int i = 1; i <= 3; i++) {
            System.out.println("=== Turn " + i + " ===");
            garen.processEffects();
            ahri.processEffects();
            garen.attack(ahri);
            ahri.attack(garen);
        }
    }
}
This post is licensed under CC BY 4.0 by the author.