gpt4 book ai didi

java - Java HashMap 问题中的 Zuul 重制

转载 作者:行者123 更新时间:2023-12-01 15:28:16 27 4
gpt4 key购买 nike

我正在使用 BlueJ 做一个 Java 类(class)作业。我们需要向基于文本的游戏 Zuul 添加新功能。我决定开始研究库存和元素系统。我很难找到最好的方法来做到这一点,所以我只是即兴发挥。这是我的代码。抱歉,我还没来得及评论所有内容。游戏可以编译,但运行游戏时控制台出现异常。

错误:

java.lang.NullPointerException
at Game.createPlayer(Game.java:15)
at Game.<init>(Game.java:7)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at bluej.runtime.ExecServer$3.run(ExecServer.java:740)

游戏类(这相当于Java中的Main类,这是游戏运行的地方):

import java.util.*;

public class Game
{
public Game()
{
createPlayer();
createItems();
}

private Entity localPlayer;

public void createPlayer(){
Player localPlayer = new Player("Player Name", 0, 0, 0, 0, 0);
localPlayer.equipArmour("Helm", armourDB.get("Helm")); // This is where I think I have gone wrong
}

// Create global hashmap variables
private HashMap<String, Weapon> weaponsDB;
private HashMap<String, Armour> armourDB;
private HashMap<String, Supplement> supplementDB;

public void createItems(){
// Create weapons
weaponsDB = new HashMap<String, Weapon>();

Weapon weaponFists = new Weapon("Fists", "Weapon", 0, 0, 0, 0, 0, "Melee");
Weapon weaponSword = new Weapon("Sword", "Weapon", 0, 0, 0, 0, 0, "Melee");
Weapon weaponBow = new Weapon("Bow", "Weapon", 0, 0, 0, 0, 0, "Ranged");
Weapon weaponDagger = new Weapon("Dagger", "Weapon", 0, 0, 0, 0, 0, "Melee");

weaponsDB.put("Fists", weaponFists);
weaponsDB.put("Sword", weaponSword);
weaponsDB.put("Bow", weaponBow);
weaponsDB.put("Dagger", weaponDagger);

// Create armour
armourDB = new HashMap<String, Armour>();

Armour armourBreastplate = new Armour("Breatplate", "Chest", 0, 0, 0, 0, 0);
Armour armourHelm = new Armour("Helm", "Head", 0, 0, 0, 0, 0);

armourDB.put("Breastplate", armourBreastplate);
armourDB.put("Helm", armourHelm);

// Create supplements
supplementDB = new HashMap<String, Supplement>();

Supplement supplementHealthPotion = new Supplement("Health Potion", 0, 0);
Supplement supplementPowerPotion = new Supplement("Power Potion", 0, 0);

supplementDB.put("Health Potion", supplementHealthPotion);
supplementDB.put("Power Potion", supplementPowerPotion);
}
}

实体类(玩家类和敌人类的构造):

import java.util.*;

public class Entity
{
private boolean entityStatus;

private String entityName;
private int entityHealth;
private int entityPower;
private int entityHealthRegen;
private int entityPowerRegen;
private int entityAttackPower;

private HashMap<String, Armour> entityEquipment;
private ArrayList<Item> entityInventory;

public Entity(
String paramEntityName,
int paramEntityHealth,
int paramEntityPower,
int paramEntityHealthRegen,
int paramEntityPowerRegen,
int paramEntityAttackPower)
{
entityStatus = true;

entityName = paramEntityName;
entityHealth = paramEntityHealth;
entityPower = paramEntityPower;
entityHealthRegen = paramEntityHealthRegen;
entityPowerRegen = paramEntityPowerRegen;
entityAttackPower = paramEntityAttackPower;

entityEquipment = new HashMap<String, Armour>(); // Set all possible equipment slots to null on initial run
entityEquipment.put("Head", null);
entityEquipment.put("Shoulders", null);
entityEquipment.put("Chest", null);
entityEquipment.put("Hands", null);
entityEquipment.put("Legs", null);
entityEquipment.put("Feet", null);
entityEquipment.put("Weapon", null);

entityInventory = new ArrayList<Item>();
}

public boolean getEntityStatus(){
return entityStatus;
}

public String getEntityName(){
return entityName;
}

public int getEntityHealth(){
return entityHealth;
}

public int getEntityPower(){
return entityPower;
}

public int getEntityHealthRegen(){
return entityHealthRegen;
}

public int getEntityPowerRegen(){
return entityPowerRegen;
}

public int getEntityAttackPower(){
return entityAttackPower;
}

// Equips the player with an item into the equipment slot
public void equipArmour(String paramEquipmentSlot, Armour paramArmourName){
entityEquipment.put(paramEquipmentSlot, paramArmourName);
}

public void printInventory(){
System.out.println("Something");
}
}

我认为主要问题是我无法理解主题标签的使用,我需要看一个实例来了解它是如何工作的。有人可以帮忙吗?如果您需要我提供任何其他信息,请告诉我。

最佳答案

这就是问题所在:

armourDB.get("Helm")

此时您尚未初始化 armourDB。如果您在 createPlayer() 之前调用 createItems(),则该特定行应该没问题。但您仍然不会初始化名为 localPlayerisntance 变量。您只需为 createPlayer 中声明的本地变量分配一个值。

说实话,目前还不清楚你想要实现什么目标,但这是前两个问题......

关于java - Java HashMap 问题中的 Zuul 重制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9908487/

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