Episode 5: Champion Selection with List<Hero>
Goal: Use a List<Hero>' to store all available champions and allow the player to choose one before the battle begins.
Episode 5: Champion Selection with List<Hero>
Update Main: Create Champion List<Hero>
In Main.java, define a method to generate the champion pool:
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
import model.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Hero> championPool = createChampionPool();
System.out.println("Welcome to Java MOBA Console \n");
System.out.println("Choose your champion:");
for (int i = 0; i < championPool.size(); i++) {
Hero hero = championPool.get(i);
System.out.println(i + 1 + ". " + hero.getName() + " - " + hero.getClass().getSimpleName());
}
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of your champion: ");
int choice = scanner.nextInt() - 1;
if (choice < 0 || choice >= championPool.size()) {
System.out.println("Invalid choice. Exiting...");
return;
}
Hero playerHero = championPool.get(choice);
Hero botHero = championPool.get((choice + 1) % championPool.size()); // simple bot selection
System.out.println("\nYou chose: " + playerHero.getName());
System.out.println("Bot will play: " + botHero.getName());
// Simple combat preview
playerHero.attack(botHero);
botHero.attack(playerHero);
scanner.close();
}
public static List<Hero> createChampionPool() {
List<Hero> heroes = new ArrayList<>();
heroes.add(new Warrior("Garen"));
heroes.add(new Mage("Ahri"));
return heroes;
}
}
Bonus: Add More Champions
To make the game more exciting, you can keep adding more subclasses like:
1
2
heroes.add(new Mage("Lux"));
heroes.add(new Warrior("Darius"));
This post is licensed under CC BY 4.0 by the author.