Is there an easier way?

Started by
5 comments, last by TheChubu 9 years, 8 months ago

Ok so for those of you who don't know I am in the process of making a very small console based pokemon clone in java. While coding today I ran across a question and figured that I should ask it here and maybe I'll learn something new. (My goal is to learn at least one new detail about Java everyday). Anyways, I was beginning to hard code some stuff like pokemon moves, stats, etc. and wondered if there was a better way of doing it all. Currently, I am really just using the name of the pokemon in a switch to determine which moves it has. It is really a lot of work for just one pokemon and I got to thinking how someone would do this for lets say 100 pokemon. Not that I plan to do 100 pokemon, I am just wondering if there are more efficient concepts behind stuff like his. Thanks in advance!!

Advertisement

I'd suggest storing all of the stats in a text file for each type and then reading it into either an ArrayList where the index is the pokemon number or into a HashMap where the key is the pokemon type and the value is an object containing the stats. This makes it easy to change stats or add pokemon without adding any code. If you'd rather not load from a file, you can still use the mentioned data structures to make it easier to handle without a lot of switch statements.

I would try to create some sort of XML-Format to use as "database".

Maybe something like that


<pokemon id="78">
  <name>Snorlax</name>
  <attack level="1" strength="30" />
  <attack level="2" strength="45" />
</pokemon>
<pokemon id="79">
  <name>Smogogo</name>
  ...

These information would be loaded into a bunch of Pokemon-Objects which then contain a bunch of Attack-Objects.

That's about how I would do it... hope it helps ;)

You don't need a switch ...

"Hard coding" pokedudes is fine if you are only going to have a few of them, however in the long run it is far easier to store them in a text file and add / modify them using a separate application .

Each pokedude should be a separate object with attacks, health, and specials stored inside.


public class pokedude{ // how a pokedude class may look like
String name;          // this code is not debugged 
String[] abilities;
String current_buff;
String current_debuff;
int experience;
int current_health;
int max_health;

     public void pokedude(String n, String[] a, int mh){
          name = n;
          abilities = a;
          current_health = mh;
          max_health = mh;
          current_buff = "";
          current_debuff = "";
          experience = 0;
     {

     public void setExperience (int e){
          experience += e;
     }
     public void setCurrentHealth (int ch){
          current_health += ch;
     }
     public void heal (){
          current_health = max_health;
     }
     public String getName(){
          return name;
     }
     public String getCurrentBuff(){
          return current_buff;
     }
     public String getCurrentDebuff(){
          return current_debuff;
     }
     public String[] getAbilities(){
          return abilities;
     }
     public int getCurrentHealth(){
          return current_health;
     }
     public int getExperience(){
          return experience;
     }
}

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Don't store everything in strings, its error prone, bloats the data, and string comparison is relatively expensive.

Stats are the same for all pokemons, so you could just have a bunch of fields for strength, agility, endurance, etc. Moves themselves could be regular objects with modifiers, the modifier itself could be an object that takes a pokemon and applies some effect to it. So you'd have health modifier, stat modifier, etc. Also, not all pokemon can learn all the moves, so you'd have to store that knowledge somewhere (either each move can know what pokemons can learn it, or each pokemon knows what moves it can learn).

A pokemon would have its stats, and a list of 4 moves, each of them having their own modifiers (which could be one or more, say, damage + poison).

You can serialize all of that data as Fell mentioned later for easy modification.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Ok I like the concept that Fell said about using a data base, but I am totally lost as to where to start. Some questions that I have about it would be, How do I make the data base?, Where do I put the data base? How would I use the information from the data base to make an object? Sorry for my inexperience, but I am only trying to learn.

Yeah about that, I'd say just figure out the basics (what classes do you need) first. Serialization/deserialization isn't always a nice topic.

In any case, what you will be looking for (after figuring out the basics) is for JAXB probably. Its the standard way in Java to go from XML files (what Fell described) to objects and vice versa. Java has tons of "standard" frameworks for doing all sort of things, this is one of those things that are in the standard library that comes with all JVM installs (thus, not necessarily in Android).

If XML isn't for you, there are other markup languages to use like JSON or YAML, I *think* Java standard library supports serializing objects to a JSON file, not sure, for using YAML you'll have to use an external lib like SnakeYAML, there are probably external libs for JSON too.

But, as I said, first your class design, later you can figure out how to store/modify/load that data.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement