我想知道是否有更好的方法来创建和搜索下面的静态地图。如方法中所见main()
。
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public enum PokemonType {
BUG("Bug"),
DARK("Dark"),
DRAGON("Dragon"),
ELECTRIC("Electric"),
FAIRY("Fairy"),
FIGHTING("Fighting"),
FIRE("Fire"),
FLYING("Flying"),
GHOST("Ghost"),
GRASS("Grass"),
GROUND("Ground"),
ICE("Ice"),
NORMAL("Normal"),
POISON("Poison"),
PSYCHIC("Psychic"),
ROCK("Rock"),
STEEL("Steel"),
WATER("Water");
private String name;
@SuppressWarnings("serial")
public static Map<PokemonType, Set<PokemonType>> noEffect = new HashMap<PokemonType, Set<PokemonType>>() {{
put(BUG, new HashSet<PokemonType>());
put(DARK, new HashSet<PokemonType>());
put(DRAGON, new HashSet<PokemonType>());
put(ELECTRIC, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GROUND)); }});
put(FAIRY, new HashSet<PokemonType>());
put(FIGHTING, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GHOST)); }});
put(FIRE, new HashSet<PokemonType>());
put(FLYING, new HashSet<PokemonType>());
put(GHOST, new HashSet<PokemonType>() {{ addAll(Arrays.asList(NORMAL)); }});
put(GRASS, new HashSet<PokemonType>());
put(GROUND, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FLYING)); }});
put(ICE, new HashSet<PokemonType>());
put(NORMAL, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GHOST)); }});
put(POISON, new HashSet<PokemonType>());
put(PSYCHIC, new HashSet<PokemonType>());
put(ROCK, new HashSet<PokemonType>());
put(STEEL, new HashSet<PokemonType>());
put(WATER, new HashSet<PokemonType>());
}};
@SuppressWarnings("serial")
public static Map<PokemonType, Set<PokemonType>> notVeryEffective = new HashMap<PokemonType, Set<PokemonType>>() {{
put(BUG, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FIGHTING,FIRE,FLYING,GHOST,POISON,STEEL)); }});
put(DARK, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DARK,FIGHTING,STEEL)); }});
put(DRAGON, new HashSet<PokemonType>() {{ addAll(Arrays.asList(STEEL)); }});
put(ELECTRIC, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DRAGON,ELECTRIC)); }});
put(FAIRY, new HashSet<PokemonType>());
put(FIGHTING, new HashSet<PokemonType>() {{ addAll(Arrays.asList(BUG,FLYING,POISON,PSYCHIC)); }});
put(FIRE, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DRAGON,FIRE,ROCK,WATER)); }});
put(FLYING, new HashSet<PokemonType>() {{ addAll(Arrays.asList(ROCK,STEEL)); }});
put(GHOST, new HashSet<PokemonType>() {{ addAll(Arrays.asList(STEEL)); }});
put(GRASS, new HashSet<PokemonType>() {{ addAll(Arrays.asList(BUG,DRAGON,FIRE,FLYING,GRASS,POISON,STEEL)); }});
put(GROUND, new HashSet<PokemonType>() {{ addAll(Arrays.asList(BUG,GRASS)); }});
put(ICE, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FIRE,ICE,STEEL,WATER)); }});
put(NORMAL, new HashSet<PokemonType>() {{ addAll(Arrays.asList(ROCK,STEEL)); }});
put(POISON, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GHOST,GROUND,POISON,ROCK)); }});
put(PSYCHIC, new HashSet<PokemonType>() {{ addAll(Arrays.asList(PSYCHIC,STEEL)); }});
put(ROCK, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FIGHTING,GROUND,STEEL)); }});
put(STEEL, new HashSet<PokemonType>() {{ addAll(Arrays.asList(ELECTRIC,FIRE,STEEL,WATER)); }});
put(WATER, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DRAGON,GRASS,WATER)); }});
}};
@SuppressWarnings("serial")
public static Map<PokemonType, Set<PokemonType>> superEffective = new HashMap<PokemonType, Set<PokemonType>>() {{
put(BUG, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DARK,GRASS,PSYCHIC)); }});
put(DARK, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GHOST,PSYCHIC)); }});
put(DRAGON, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DRAGON)); }});
put(ELECTRIC, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FIRE,WATER)); }});
put(FAIRY, new HashSet<PokemonType>());
put(FIGHTING, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DARK,ICE,NORMAL,ROCK,STEEL)); }});
put(FIRE, new HashSet<PokemonType>() {{ addAll(Arrays.asList(BUG,GRASS,ICE,STEEL)); }});
put(FLYING, new HashSet<PokemonType>() {{ addAll(Arrays.asList(BUG,FIGHTING,GRASS)); }});
put(GHOST, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GHOST,PSYCHIC)); }});
put(GRASS, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GROUND,ROCK,WATER)); }});
put(GROUND, new HashSet<PokemonType>() {{ addAll(Arrays.asList(ELECTRIC,FIRE,POISON,ROCK,STEEL)); }});
put(ICE, new HashSet<PokemonType>() {{ addAll(Arrays.asList(DRAGON,FLYING,GRASS,GROUND)); }});
put(NORMAL, new HashSet<PokemonType>());
put(POISON, new HashSet<PokemonType>() {{ addAll(Arrays.asList(GRASS)); }});
put(PSYCHIC, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FIGHTING,POISON)); }});
put(ROCK, new HashSet<PokemonType>() {{ addAll(Arrays.asList(BUG,FIRE,FLYING,ICE)); }});
put(STEEL, new HashSet<PokemonType>() {{ addAll(Arrays.asList(ICE,PSYCHIC)); }});
put(WATER, new HashSet<PokemonType>() {{ addAll(Arrays.asList(FIRE,GROUND,ROCK)); }});
}};
public float getModifier(PokemonType opponentType) {
if (PokemonType.superEffective.get(this).contains(opponentType))
return 2.0f;
if (PokemonType.notVeryEffective.get(this).contains(opponentType))
return 0.5f;
if (PokemonType.noEffect.get(this).contains(opponentType))
return 0.0f;
return 1.0f;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String shortName() {
return this.name.substring(0, 3);
}
private PokemonType(String name) {
this.name = name;
}
public static void main(String[] args) {
assert(PokemonType.ELECTRIC.getModifier(PokemonType.WATER) == 2.0f);
assert(PokemonType.GROUND.getModifier(PokemonType.DARK) == 1.0f);
assert(PokemonType.FIRE.getModifier(PokemonType.DRAGON) == 0.5f);
assert(PokemonType.NORMAL.getModifier(PokemonType.GHOST) == 0.0f);
}
}