Reputation: 67
Currently creating a MUD game as my first serious project in Java, I already have the lore and background set up I just need to start the actual code. I started by first trying to create sets of enemies like this:
// Name, HP, MP, ATK enemies[] = {"Weak Skeleton:", 100, 0, 100; "Blue Slime:", 50, 0, 25; "Novice Rogue Mage:", 25, 100, 0;}
but I obviously can't do that in Java just like that. Is there a way to assign strings a value (kind of like a key-value pair in python, but multiple values assigned to one string) in Java and if so is there a way for me to access and manipulate the integer values assigned to the string?
Upvotes: 1
Views: 11917
Reputation: 11030
Two ways: one is just to use an array. To get "loose" semantics in Java just use Object[]
. This works but isn't great.
Object[][] enemies1 = {{"Weak Skeleton:", 100, 0, 100},
{"Blue Slime:", 50, 0, 25},
{"Novice Rogue Mage:", 25, 100, 0}};
A rather better idea as mentioned is to create a more semantic type like Enemy
and create new objects of that type.
Enemy[] enemies1 = { new Enemy( "Weak Skeleton:", 100, 0, 100 ),
new Enemy( "Blue Slime:", 50, 0, 25 ),
new Enemy( "Novice Rogue Mage:", 25, 100, 0 ) };
Upvotes: 2
Reputation: 14698
The most proper to do this is to have an object representing the combination of values you have;
public class Enemy {
private String name;
private Integer healthPoint;
private Integer manaPoint;
private Integer attackPower;
// getter, setter, constructors
// example constructor
public Enemy(String name, Integer hp, Integer mp, Integer ap) {
this.name = name;
this.healthPoint = hp;
this.manaPoint = mp;
this.attackPower = ap;
}
}
Then represent this with a map if you'd like, by doing;
List<Enemy> enemies = new ArrayList<>();
enemies.add(new Enemy("Weak Skeleton", 100, 0, 100));
enemies.add(new Enemy("Blue Slime", 50, 0, 25));
enemies.add(new Enemy("Novice Rogue Mage", 25, 100, 0));
Map<String, Enemy> enemyMap = enemies.stream().collect(Collectors.groupingBy(Enemy::getName));
So if you'd need to access and edit any value, it is easy via;
enemyMap.get("Weak Skeleton").setAttackPower(75);
or to add a new enemy;
Enemy dragonBoss = new Enemy("Dragon Boss", 1000, 500, 250);
enemyMap.put(dragonBoss.getName(), dragonBoss);
Though, I'd suggest using some enumeration to identify each enemy, trying to access with name String is not the best practice.
public enum EnemyType {
WEAK_SKELETON("Weak Skeleton"), BLUE_SLIME("Blue Slime"), NVC_ROGUE_MAGE("Novice Rogue Mage");
private String name;
public String getName() {
return this.name;
}
private EnemyType (String name) {
this.name = name;
}
}
And using it as name/key in map.
You'd be better off with Lombok, to chain setters if you'd want, plus it reduces a lot of boilerplate code (getters, setters, constructors). Check @Data
with @Accessors(chain = true)
Upvotes: 2
Reputation: 4104
You need to change the approach to think about data as you are changing the language.
What you want to achieve, can be achieve in following way:
class Data{
//Choose your access-specified as per your need
public int hp, mp, atk;
public Data(int h, int m, int a)
{ hp = h; mp=m; atk=a;}
}
...
HashMap<String,Data> dataSet = new HashMap<>();
dataSet.put("val1", new Data(50,40,30));
...
Data d = dataSet.get("val1");
System.out.println("Value of HP is:" + d.hp);
...
Upvotes: 1
Reputation: 20566
You can create class that represents enemy
public class Enemy {
private int hp;
private int mp;
private int atk;
public Enemy(int hp, int mp, int atk) {
this.hp = hp;
this.mp = mp;
this.atk = atk;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getMp() {
return mp;
}
public void setMp(int mp) {
this.mp = mp;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
}
and then use it like this:
HashMap<String, Enemy> map = new HashMap<>();
map.put("Weak Skeleton", new Enemy(100, 0, 100));
map.put("Blue Slime", new Enemy(50, 0, 25));
Upvotes: 1
Reputation: 1500
You can use Map
in java.
Map<String,List<Integer>> enemies1 = new HashMap<>();
enemies1.put("str1",list1);
enemies1.put("str2",list2);
enemies1.put("str3",list3);
Upvotes: 2