Java 中的基于文本的游戏。需要有关库存实施和一般编码建议的建议

Text-Based Game in Java. Need advice with Inventory Implementation and general coding advice

我无法弄清楚如何处理基于文本的 java 游戏的库存。我刚刚完成数据结构和算法,所以我认为这对我的简历来说是一个很好的项目。

目前,我在播放器的构造函数中创建库存 class。我想用物品 class 中的 3 个药水物品来实例化库存。我尝试在播放器构造函数中这样做,但我不明白为什么它不起作用。

我的问题是,我应该如何在 his/her 库存中用 3 种药水实例化每个角色?


玩家class:

package projectmoria;


import java.util.ArrayList;
import java.util.List;


public class Player {

    private final String name;
    private final String description;
    private final int maxHitPoints;
    private int hitPoints;
    private final int minDamage;
    private final int maxDamage;
    private final int defense;
    private double critChance;
    private int currX;
    private int currY;
    private Room currRoom;
    private List<Item> inventory;

    public Player(String name, String description, int maxHitPoints,
            int minDamage, int maxDamage, int defense, double critChance) {
        this.name = name;
        this.description = description;
        this.maxHitPoints = maxHitPoints;
        this.hitPoints = maxHitPoints;
        this.minDamage = minDamage;
        this.maxDamage = maxDamage;
        this.defense = defense;
        this.critChance = critChance;
        this.currX = 14;
        this.currY = 14;
        inventory = new ArrayList<>();
        inventory.add(Item.addPotion(3, this.player)); //This is the line I need help with
    }

    public int attack() {
        return ProjectMoria.RAND.nextInt(maxDamage - minDamage + 1);
    }

    public int defend(Monster monster) {
        int incomingAttack = monster.attack();
        int random = ProjectMoria.RAND.nextInt(99) + 1;
        if (random <= monster.getCritChance()) {
            incomingAttack = incomingAttack * 2;
            IO.monsterCrit(); //TODO - move to different spot
        }
        IO.playerHitPointsMessage(incomingAttack, monster);
        hitPoints = (hitPoints * defense > incomingAttack)
                ? hitPoints - incomingAttack : 0;
        return hitPoints;
    }

    public void heal(Item potion){
        this.hitPoints =+ 20;
        inventory.remove(potion);
        IO.heal(this.hitPoints);
    }

    public static Player newWarrior() {
        return new Player("Warrior", "A tough, well-rounded fighter with"
                + " a balanced skillset.", 100, 20, 30, 3, 10);
    }

    public static Player newDuelist() {
        return new Player("Duelist", "A quick, nimble duelist with an"
                + " aptitude for landing critical attacks.", 8000, 10, 50, 2, 
                18);
    }

     public String getDescription() {
        return description;
    }

    public int getHitPoints() {
        return hitPoints;
    }

    public boolean isAlive() {
        return hitPoints > 0;
    }

    public String getName() {
        return name;
    }

    public int getMaxHitPoints() {
        return maxHitPoints;
    }

    public int getMinDamage() {
        return minDamage;
    }

    public int getMaxDamage() {
        return maxDamage;
    }

    public int getDefense() {
        return defense;
    }

    public double getCritChance() {
        return critChance;
    }

    public int getCurrX() {
        return currX;
    }

    public int getCurrY() {
        return currY;
    }

    public List<Item> getInventory() {
        return inventory;
    }


    public Room getCurrRoom() {
        return currRoom;
    }

    public void setCurrRoom(Room room) {
        currRoom = room;
    }

    public void setCurrX(int currX) {
        this.currX = currX;
    }

    public void setCurrY(int currY) {
        this.currY = currY;
    }
}

项目class:

package projectmoria;


public class Item {

    private final String name;
    private final String type;
    private final String description;

    public Item(String name, String type, String description){
        this.name = name;
        this.type = type;
        this.description = description;
    }

    public void use(Player player, Item item){
        if(item.type.equals("Potion")){
            player.heal(item);
        }
    }

    public void addPotion(int numOfPotions, Player player){
        for(int i = 0; i < numOfPotions; i ++){
            player.getInventory().add(potion());
        }
    }

    public Item potion(){
        return new Item ("Potion", "Potion", " a small vial filled with a "
                + "translucent red liquid");
    }
}

实际上在 Player 构造函数中:

inventory = new ArrayList<>();
inventory.add(Item.addPotion(3, this.player)); 

您正在调用:

public void addPotion(int numOfPotions, Player player){

这是一个实例方法。
您只能在实例上调用实例方法。

你的 addPotion() 方法看起来像是一个工厂方法来创建药水 Items :

public void addPotion(int numOfPotions, Player player){
    for(int i = 0; i < numOfPotions; i ++){
        player.getInventory().add(potion());
    }
}

public Item potion(){
    return new Item ("Potion", "Potion", " a small vial filled with a "
            + "translucent red liquid");
}

所以,你应该做 static addPotion()potion() 应该可以解决你的问题。
此外,由于 potion() 仅由 addPotion() 调用,您可以通过将其设置为 private.

来降低它的访问级别

如果您想将该方法调用为 Item.addPotion(...)

,则必须将 addPotion(...) 方法更改为静态方法

我强烈建议将您的物品 class 作为枚举 Enum java doc 并将这些枚举添加到玩家的物品栏中。这些是您应该考虑的大量设计注意事项。如果您使用枚举设计。您将强制将物品锁定为只有几种类型的物品,并且玩家共享该物品。

但是,如果物品与您的玩家紧密耦合,例如,您希望每次玩家使用该部分时物品都会减少。然后简单地使方法 addPotion 静态。

public static void addPotion(...)