面向对象的java,在游戏中创建和放置敌方波循环的最佳方法

ubof19bj  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(293)

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

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();

}
ioekq8ef

ioekq8ef1#

好的,我会尽力帮忙的。
首先,我认为这是不必要的:

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() 以完成循环,并按您希望的方式继续处理过程。

相关问题