gpt4 book ai didi

Java,在游戏中为敌人的波浪制作和放置循环的最佳方法

转载 作者:行者123 更新时间:2023-12-04 08:27:57 25 4
gpt4 key购买 nike

我正在做一个面向对象的最终项目,这是一个基于文本的生存游戏。我目前正在尝试通过让玩家输入他们想要战斗的敌人数量来完成它,然后循环遍历它直到达到数量为止。最好的路线是什么?我希望它可以选择两次敌人,但我的搭档想要对数组进行洗牌,因为工作量较少。
大多数情况下,我被困在我应该在哪里制作什么样的循环以及我应该把它放在哪里。
这是主类中的一些代码。

Entity entity = new Entity();
ArrayList<Entity> enemies = new ArrayList<>();
EnemyList enemyList = new EnemyList();

Entity troll = new Troll();
troll.setName("Troll");
enemyList.characters.add(troll);

Entity imp = new Imp();
imp.setName("Imp");
enemyList.characters.add(imp);

Entity knight = new Knight();
knight.setName("knight");
enemyList.characters.add(knight);

Entity skeleton = new Skeleton();
skeleton.setName("Skeleton");
enemyList.characters.add(skeleton);


Integer[] array = new Integer[enemyList.characters.size()];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}

Collections.shuffle(Arrays.asList(array));


for (int i = 0; i < enemyCount; i++) {

for (Entity someEnemy : enemyList.characters) {

System.out.println(enemyList.characters.get(array[i]));

System.out.println(someEnemy.getName(entity) + "\n");
System.out.println("A " + someEnemy.getName(entity) + " Appears! It has " + someEnemy.getHealth() + "HP");

while (someEnemy.getHealth() > 0) {

int attack = character.getAttack();
System.out.println("You hit the " + someEnemy.getName(entity) + " for " + character.getAttack());
int monsterTotalHealth = someEnemy.setHealth(someEnemy.getHealth() - attack);
System.out.println(someEnemy.getName(entity) + " has " + monsterTotalHealth + "HP Left");
System.out.println("");

if (someEnemy.getHealth() > 0) {

System.out.println("The monster attacks back for " + someEnemy.getStrength());
int remainingHP = character.damageDelt(someEnemy.getStrength());
System.out.println("Your remaining health is " + remainingHP);
System.out.println("");
character.setHealth(character.getHealth());
}

if (character.isDead()) {

System.out.println("You have been defeated!");
System.exit(0);

} else if (someEnemy.getHealth() < 0) {
System.out.println("Fighting Next monster");

}

}
}

}

实体类
public class Entity {

private String name;
private int health;
private int level;
private int vitality;
private int strength;
private int resistance;
private int dexterity;
private int endurance;
private int intelligence;
private int attack;

public String getName(Entity someenemy) {
return name;
}

public int getHealth() {
return health;
}

public int getLevel() {
return level;
}

public int getVitality() {
return vitality;
}

public int getStrength() {
return strength;
}

public int getResistance() {
return resistance;
}

public int getDexterity() {
return dexterity;
}

public int getEndurance() {
return endurance;
}

public int getIntelligence() {
return intelligence;
}

public int getAttack() { return attack; }

public void setName(String name) {
this.name = name;
}

public int setHealth(int health) {
this.health = health;
return health;
}

public void setLevel(int level) {
this.level = level;
}

public int setVitality(int vitality) {
this.vitality = vitality;
return vitality;
}

public void setStrength(int strength) {
this.strength = strength;
}

public void setResistance(int resistance) {
this.resistance = resistance;
}

public void setDexterity(int dexterity) {
this.dexterity = dexterity;
}

public void setEndurance(int endurance) {
this.endurance = endurance;
}

public void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}

public void setAttack(int attack) {
this.attack = attack;
}


@Override
public String toString() {
return "This enemy is appearing: " + name +
", health is " + health +
", strength is " + strength;
}
}
EnemyList 类
import java.util.ArrayList;
import java.util.List;

public class EnemyList {

List<Entity> characters = new ArrayList<>();

public void print()
{
for (Entity entity : characters)
{
System.out.println(entity.toString());

}
}

}


力学界面

interface Mechanics {

boolean isDead();

int damageDelt (int damage);

//int levelup();


}

最佳答案

好的,会在这里提供帮助。
首先,我认为这是不必要的:

    Integer[] array = new Integer[enemyList.characters.size()];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}

Collections.shuffle(Arrays.asList(array));
您已经拥有 array ,包含在 enemyList 中实例,称为 characters.现在假设您的用户想要与 5 个敌人作战。要随机化事件,请先对数组进行洗牌。
Collections.shuffle(enemyList.characters);
假设 maxEnemies = 5 ,让我们进入循环:
 boolean win = true;
for (int i=0;i<maxEnemies;i++)
{
Entity enemy = enemyList.characters.get(i);
String enemyName = enemy.getName();
System.out.println("Fighting "+enemyName);
//... your logic here
if (character.isDead())
{
win=false;
break;
}
}

if (!win)
System.out.println("You have been defeated! You are a disgrace to your family");
else
System.out.println("You've defeated "+maxEnemies+" bastards. You WON!
Tiger blood runs through your veins");
请注意,我将“与下一个敌人战斗”文本移动到循环子句的开头,因为当最后一个敌人被击败时(不会有下一个敌人),显示的文本将是不正确的。
由于指数是根据您的计数(0-4)计算的,因此不会出现两次怪物。
请注意,您应该验证用户输入,如果他愿意与之战斗的敌人数量大于存储在数组中的实体数量,则拒绝输入。所以 input regarding number of entities to fight <= enemyList.characters.size我也对 exit 方法进行了一些更改,但可以随意更改它们以符合您的目的。您可以调用 break() 而不是杀死线程。为了完成循环,并按照您想要的方式继续该过程。

关于Java,在游戏中为敌人的波浪制作和放置循环的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65173681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com