gpt4 book ai didi

java - 如何从ArrayList中选择一个特定的对象

转载 作者:行者123 更新时间:2023-12-02 03:14:25 27 4
gpt4 key购买 nike

我们正在制作一个简单的角色扮演游戏。现在我们有一个叫做战场的类,玩家可以在其中与怪物战斗。当我们开始战斗时,我们使用随机生成器来随机挑选一个怪物。在终端/主方法中,我们有“攻击”和“运行”等命令,这些命令要么对随机怪物造成伤害,要么让玩家离开/退出游戏。现在,我们正在尝试添加一个名为“attackiest”的命令,这将使玩家能够对抗最难、伤害最大的怪物(我们的主菜单中有三个可供选择)。我们需要一种方法来根据伤害从 ArrayList 怪物中选择特定对象。有人对我们如何做到这一点有建议吗?

这是我们在 Battleground 类中启动游戏的代码:

 public void startBattle() {
printWelcomeMessage();
boolean finished = false;


this.currentMonster = getRandomMonster();


if(this.currentMonster == null) {
return;
}

System.out.println("A random monster is chosen for you. Prepare to meet the mighty " + this.currentMonster.getName());
System.out.println("\n-------- Player Stats ---------");
System.out.println(this.player);
while(!finished && monsters.size() > 0) {
ArrayList<String> commands = reader.getInput();

if(isValidCommand(commands)) {

if(commands.contains("quit") && !this.currentMonster.isDead()){
System.out.println("You can't quit the game in the middle of a fight!");
}else if(commands.contains("quit") && this.currentMonster.isDead()) {
finished = true;
System.out.println();
printFinalStats();
System.out.println();
System.out.println("You have left the arena, and the game has ended.");
System.out.println();
}


if(commands.contains("run")) {
finished = true;
System.out.println("You are a coward and you lose 50 gold pieces...\n");
this.player.changeGold(-50);
printFinalStats();
System.out.println("\nThanks for playing...");

}else if(commands.contains("drink") && !this.currentMonster.isDead()){
Potion potion = (Potion) player.findItem(commands.get(1));
player.drinkPotion(potion);

}else if(commands.contains("equip") && !this.currentMonster.isDead()){
Weapons weapon = (Weapons) player.findItem(commands.get(1));
player.useWeapon(weapon);
} else if(commands.contains("attack") && !this.currentMonster.isDead()) {

if(this.player.attack(this.currentMonster)) {
playerWon();
if(this.monsters.size() > 0) {
System.out.println("\nThere are " + this.monsters.size() + " more monsters to beat\nType \"attack\" if you want to attack another monster, or \"quit\" if you want to end the game.");
} else {
System.out.println("\n\n#### Congratulations ####\nYou have beaten every single monster in the game. You are a true champion!");
printFinalStats();
finished = true;
}

} else if(this.currentMonster.attack(this.player)) {
printLosingMessage();
finished = true;
}

} else if(commands.contains("attack") && this.currentMonster.isDead() && this.monsters.size() > 0) {
this.currentMonster = getRandomMonster();
printContinueMessage(this.player, this.currentMonster);
this.player.changeHealth(50);
}

} else {
System.out.println("Please write a valid command. Valid commands are:");
printCommands();
}
}
}

这是主类中怪物的ArrayList:

 Monster beelzebub = new Monster("Beelzebub", 60);
Monster witch = new Monster("Witch", 40);
Monster ogre = new Monster("Ogre", 80);

ArrayList<Monster> monsters = new ArrayList<>();

monsters.add(beelzebub);
monsters.add(witch);
monsters.add(ogre);

Battleground battleground = new Battleground(monsters, player, reader);
battleground.startBattle();

我们感谢任何帮助!

最佳答案

用 SortedSet 或 PriorityQueue 替换 ArrayList。为 Monster 类实现一个比较器。然后只需选择怪物集合的第一个元素即可。

关于java - 如何从ArrayList中选择一个特定的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40522388/

27 4 0