7 Replies Latest reply: Jun 15, 2010 9:44 AM by 796262 RSS

    Data storage?

    843853
      Hey guys, I'm trying to make a Pokemon game. And one of the first thing I want to do is storing Types, Pokemon and later Attacks. I'm now doing it this way, though I think this is a kind of dirty to do this. Is there a better way. f.e. making a txt file where I put that data?
      public class main {
           public static void main(String[] args){
                System.out.println("Hello, Welcome to Pokemon Paranoid");
                Core c = new Core();
                Type bug = new Type();
                Type grass = new Type();
                Type fire = new Type();
                Type water = new Type();
                Type electric = new Type();
                Type ground = new Type();
                Type ice = new Type();
                Type fighting = new Type();
                Type psychic = new Type();
                Type ghost = new Type();
                Type dark = new Type();
                Type dragon = new Type();
                Type normal = new Type();
                Type flying = new Type();
                Type rock = new Type();
                Type steel = new Type();
                Type poison = new Type();
                
                //Immunities
                ground.addImmunity(electric);
                ghost.addImmunity(normal);
                ghost.addImmunity(fighting);
                dark.addImmunity(psychic);
                normal.addImmunity(ghost);
                flying.addImmunity(ground);
                steel.addImmunity(poison);
                
                //Resistances
                fire.addResistance(fire);
                fire.addResistance(grass);
                fire.addResistance(ice);
                fire.addResistance(bug);
                fire.addResistance(steel);
                
                water.addResistance(fire);
                water.addResistance(water);
                water.addResistance(ice);
                water.addResistance(steel);
                
                electric.addResistance(electric);
                electric.addResistance(flying);
                electric.addResistance(steel);
                
                grass.addResistance(water);
                grass.addResistance(electric);
                grass.addResistance(grass);
                grass.addResistance(ground);
                
                ice.addResistance(ice);
                
                fighting.addResistance(bug);
                fighting.addResistance(rock);
                fighting.addResistance(dark);
                
                poison.addResistance(grass);
                poison.addResistance(fighting);
                poison.addResistance(poison);
                
                ground.addResistance(poison);
                ground.addResistance(rock);
                
                flying.addResistance(grass);
                flying.addResistance(fighting);
                flying.addResistance(bug);
                
                psychic.addResistance(fighting);
                psychic.addResistance(psychic);
                
                bug.addResistance(grass);
                bug.addResistance(fighting);
                bug.addResistance(ground);
                
                rock.addResistance(normal);
                rock.addResistance(fire);
                rock.addResistance(poison);
                rock.addResistance(flying);
                
                ghost.addResistance(poison);
                ghost.addResistance(bug);
                
                dragon.addResistance(fire);
                dragon.addResistance(water);
                dragon.addResistance(electric);
                dragon.addResistance(grass);
                
                dark.addResistance(ghost);
                dark.addResistance(dark);
                
                steel.addResistance(normal);
                steel.addResistance(grass);
                steel.addResistance(ice);
                steel.addResistance(flying);
                steel.addResistance(psychic);
                steel.addResistance(bug);
                steel.addResistance(rock);
                steel.addResistance(ghost);
                steel.addResistance(dragon);
                steel.addResistance(dark);
                steel.addResistance(steel);
                
                //Weaknesses
                normal.addWeakness(fighting);
                
                fire.addWeakness(water);
                fire.addWeakness(ground);
                fire.addWeakness(rock);
                
                water.addWeakness(electric);
                water.addWeakness(grass);
                
                electric.addWeakness(ground);
                
                grass.addWeakness(fire);
                grass.addWeakness(ice);
                grass.addWeakness(poison);
                grass.addWeakness(bug);
                grass.addWeakness(flying);
                
                ice.addWeakness(fire);
                ice.addWeakness(fighting);
                ice.addWeakness(rock);
                ice.addWeakness(steel);
                
                fighting.addWeakness(flying);
                fighting.addWeakness(psychic);
                
                poison.addWeakness(ground);
                poison.addWeakness(psychic);
                
                ground.addWeakness(water);
                ground.addWeakness(grass);
                ground.addWeakness(ice);
                
                flying.addWeakness(electric);
                flying.addWeakness(ice);
                flying.addWeakness(rock);
                
                psychic.addWeakness(bug);
                psychic.addWeakness(ghost);
                psychic.addWeakness(dark);
                
                bug.addWeakness(fire);
                bug.addWeakness(flying);
                bug.addWeakness(rock);
                
                rock.addWeakness(water);
                rock.addWeakness(grass);
                rock.addWeakness(fighting);
                rock.addWeakness(ground);
                rock.addWeakness(steel);
                
                ghost.addWeakness(ghost);
                ghost.addWeakness(dark);
                
                dragon.addWeakness(ice);
                dragon.addWeakness(dragon);
                
                dark.addWeakness(fighting);
                dark.addWeakness(bug);
                
                steel.addWeakness(fire);
                steel.addWeakness(ground);
                steel.addWeakness(fighting);
                c.start();
           }
      }
      
      ==================
      
      import java.util.ArrayList;
      
      public class Type {
           public ArrayList<Type> weakness;
           public ArrayList<Type> resistance;
           public ArrayList<Type> immunity;
           
           public void addWeakness(Type t){
                weakness.add(t);
           }
           
           public void addResistance(Type t){
                resistance.add(t);
           }
           
           public void addImmunity(Type t){
                immunity.add(t);
           }
           
           public ArrayList<Type> getWeakness(){
                return weakness;
           }
           
           public ArrayList<Type> getResistance(){
                return resistance;
           }
           
           public ArrayList<Type> getImmunity(){
                return immunity;
           }
      }
        • 1. Re: Data storage?
          796262
          bigtukker wrote:
          Hey guys, I'm trying to make a Pokemon game. And one of the first thing I want to do is storing Types, Pokemon and later Attacks. I'm now doing it this way, though I think this is a kind of dirty to do this. Is there a better way. f.e. making a txt file where I put that data?
          Sure, that could work.

          Or you could just make Type into an Interface and have subtypes implement it, setting up the relationships that way instead.
          • 2. Re: Data storage?
            843853
            Do you also know how I can structure data in a txt file and how I can use it in Java? I have seen programs who jused them a way, but I don't know exactly how.
            • 3. Re: Data storage?
              796262
              bigtukker wrote:
              Do you also know how I can structure data in a txt file and how I can use it in Java? I have seen programs who jused them a way, but I don't know exactly how.
              Google Java IO, there are plenty of tutorials on that. If you have a more specific question, post an [SSCCE |http://sscce.org] that shows where you got stuck.

              There is no standard structure to saving things to a text file. I would probably be inclined to use XML, but that's just because I'm more used to it. Others would probably tell you to use serialization instead, but I've never really worked with that. Do whatever makes sense to you.

              I would still say your best bet is to use my first suggestion of turning Type into an interface and letting subtypes implement it however is appropriate for each.
              • 4. Re: Data storage?
                843853
                What do you mean by:
                turning Type into an interface and letting subtypes implement it however is appropriate for each.

                I know this might sound a kinda newbish, but I don't want the user to add Types if that's what you mean.
                • 5. Re: Data storage?
                  796262
                  bigtukker wrote:
                  What do you mean by:
                  turning Type into an interface and letting subtypes implement it however is appropriate for each.
                  Do you know what an interface is?

                  You could also do this using enums.
                  I know this might sound a kinda newbish, but I don't want the user to add Types if that's what you mean.
                  That's not what I mean. The thing is, even if you save these relationships to a file, you're still going to have to import them into some kind of data structure anyway. Do the data structure part first, then worry about saving to a file if you still think you want to do that.
                  • 6. Re: Data storage?
                    843853
                    Well, I didn't know what an interface was (it's a year ago I failed the Informatics class (it went to quick for me)). I followed a tutorial (http://www.youtube.com/watch?v=7SB7q3dFBCM) like presentation and know that an Interface is a class where you can store methods, which other classes can compare with when they have these methods as well. Thing is now, I don't get what you mean by 'subtypes'.
                    • 7. Re: Data storage?
                      796262
                      bigtukker wrote:
                      Well, I didn't know what an interface was (it's a year ago I failed the Informatics class (it went to quick for me)). I followed a tutorial (http://www.youtube.com/watch?v=7SB7q3dFBCM) like presentation and know that an Interface is a class where you can store methods, which other classes can compare with when they have these methods as well. Thing is now, I don't get what you mean by 'subtypes'.
                      Well, let's look at a "real-life" example:

                      [List |http://java.sun.com/javase/6/docs/api/java/util/List.html] is an interface. It has several subtypes that implement it, [ArrayList |http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html] being one of the most commonly used.

                      If you look at the List documentation, you'll see that it has a bunch of methods. However, you can't do List myList = new List(), right (go ahead, try it)? That's because it's an interface, which means that it doesn't actually implement those methods by itself. So how do you call those methods?

                      However, since ArrayList implements List, you are guaranteed that it implements all of the methods found in List (otherwise, it wouldn't compile).

                      List basically says "I know I'm going to contain some items, but I don't care how I do it under the hood, as long as people can still access those items using the following methods." That's how ArrayList, LinkedList, and Stack can all be Lists, but act completely differently.

                      So, your Type interface would just say "I know I'm going to have some weaknesses, but I don't care how you figure them out, as long as you can still call them from the following methods." Then your subtypes (maybe FireType would be one) would implement those methods.

                      Does that make any more sense?

                      Again, you could also use enums, which might be the way to go.