我无法弄清楚如何处理基于文本的Java游戏的库存。我刚刚完成了数据结构和算法,所以我认为这对我的简历来说是一个很好的项目。
目前,我在播放器类的构造函数中创建了库存。我想用物品类中的3个药水物品来实例化库存。我尝试在播放器构造函数中执行此操作,但我无法弄清楚为什么它不起作用。
我的问题是,我应该如何在他/她的广告资源中使用3种魔药来实例化每个角色?
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;
}
}
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");
}
}
答案 0 :(得分:1)
实际上在pd.eval()
构造函数中:
Player
你在调用:
inventory = new ArrayList<>();
inventory.add(Item.addPotion(3, this.player));
这是一个实例方法。
您只能在实例上调用实例方法。
您的public void addPotion(int numOfPotions, Player player){
方法看起来像是一种创建药水addPotion()
的工厂方法:
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");
}
static
和addPotion()
来解决你的问题。
此外,由于potion()
仅由potion()
调用,因此您可以通过设置addPotion()
来降低其级别访问权限。
答案 1 :(得分:0)
如果要将该方法称为Item.addPotion(...)
,则必须将addPotion(...)方法更改为静态方法我强烈建议您将您的Item类设置为枚举Enum java doc,并将这些枚举添加到播放器的广告资源中。这些是您应该考虑的大量设计考虑因素。如果你选择枚举设计。您将强制将项目锁定为仅几种类型的项目,并且玩家共享此项目。
但是,如果项目与您的玩家强烈耦合,例如,您希望每次玩家使用该部分时项目减少。然后简单地使方法addPotion静态。
public static void addPotion(...)