简体   繁体   中英

What would be the best way to store information for a Pokemon type game?

So I have started making a game for a school project. It is a simpler version of Pokemon without graphics, just text. Currently my problem is I do not know how I would store a database of all the Pokemon (with each individual stat), moves (with their stats), etc.

Each Pokemon (151 different) has stats and specific info about each and each move (around 100) has specific stats and info too. So I can't really make a Pokemon class and 151 classes that extend from it. What I could do is make an array of Pokemon and Moves classes and have the constructor of a Pokemon class (or move class) have the stats.

What I am having trouble is figuring out how to give a Pokemon (lets roll with Pikachu) to the player (or enemy that I am battling) and edit the stats, level, experience, etc of that one Pokemon. I don't want to change the level of every Pikachu in the game, just a specific Pikachu for the player and a specific Pokemon for the enemy. Does that make sense?

I'm new-ish to Java and I'm not sure what I would do. I have seen some people try to do the same thing and people recommend using XML or something? I'm trying to keep it all within Java.

A very simple solution would be (pseudo code)

class Pokemon:
   String name
   String/int... other_stats
   Array<Move> moves // This is a simple strategy to store any moves 
                     // you want to associate with this pokemon.

class Move:
   String name
   int power
   [...other move stats...]

To give a Pokemon to a player, you'd simply instantiate a new Pokemon with a name (such as "Pikachu"), and you would then instantiate some "Move" objects to associate with the Pokemon you just created. So, for example:

ArrayList<Move> moves = new ArrayList<Move>();
moves.add( new Move("Thunderbolt", 16) );
moves.add( new Move("Foobar", 10) );
Pokemon pikachu = new Pokemon( "Pikachu", moves )
// and now you can give pikachu to any player you want.

You can change anything for this particular Pikachu, or you can make another Pikachu, or a Bulbasaur to give to another player.

To save a game state, you can have toString methods that output that object's properties in JSON/XML whatever, and you can then save those in a text file.

This is a fairly simple design. You can, of course, use complex design patterns as others have suggested to enhance functionality.

"What I am having trouble is figuring out how to give a Pokemon (lets roll with pikachu) to the player (or enemy that I am battling) and edit the stats, level, experience, ect of that ONE Pokemon. I don't want to change the level of every pikachu in the game, just a specific pikachu for the player and a specific Pokemon for the enemy. Does that make sense?"

Well you'll have a Pokemon class somewhere with these kinds of fields:

class PStats {
    int defense = 1;
    int attack = 1;
}

class Pokemon {
    int level = 1;
    long xp = 0;
    PStats stats = new PStats();
}

Then somehow you decide the type and potential attributes of the Pokemon, whether you make Pokemon a superclass and subclass it or have fields for these things and load them from outside Java.

Then your player will have an ArrayList that you will be accessing.

If you want the player to be able to save the game and load their Pokemon you'll have to invent a simple file format and save/load each time. DataOutputStream and DataInputStream are maybe suited to this if you want to save mixed types without doing all the formatting to bytes yourself.

As an example maybe you have a file structure that looks like this:

header, UTF (8 bytes)     [FILE]
int, 4 bytes              [number of Pokemon]

1st Pokemon chunk id      [PKMN]
int, 4 bytes              [level]
long, 8 bytes             [experience]
                          [other fields...]

2nd Pokemon chunk id      [PKMN]
                          [level]
and so on...

Where you can make a simple UTF header like so:

static final char[] PKMN {
    'P', 'K', 'M', 'N'
};

Though typically headers/field id are ASCII and you can do that with bytes:

static final byte[] PKMN {
    0x50, 0x4B, 0x4D, 0x4E
};

And the FILE header is whatever you want it to be to identify your files so you don't rely on the extension.

When you want to save the file you will just loop through the array and write the fields to the file. Then when you want to load from a file you will have a basic structure something like this:

// read header and anything else you put at the start of the file

while (/*next bytes are the PKMN id*/) {
    Pokemon pToAdd = new Pokemon();

    pToAdd.level = dis.readInt();
    pToAdd.xp = dis.readLong();

    // and so on

    playerPokemon.add(pToAdd);
}

Recreating everything the player has by creating the objects from what the file indicates.

If you do it this way I'd recommend sketching out your file structure on a piece of paper so you have it in front of you when you are coding your IO.

It's probably more manageable and desirable to store your data outside of the code. To keep it simple, I'd suggest a text file. You can have comma-separated values for each line of the file. Just store the pokemon info in there.

When the program loads up, ask the two players what pokemon characters they want to be. Then read your text file in. When you read a line with one of the names, parse out the data and create an instance of your pokemon class, filling in the stats.

As for moves, just go into a loop, where one player moves, then the other. Each move can be stored in an array, as a Move object, with the stats for that move. Or, if you just want to log it, and you don't need to do anything with the move stats once the move is acted on (pokemon's position has changed, or perhaps a pokemon is damaged), then you can instead write the move info to a file rather than putting the object into an array. Again, you can just store it as comma separated values in a line per move.

You could always make each Pikachu event-driven.

Start with the basic Pikachu, then record every event that happens which affects the stats, level, experience, etc. When you want to actually use an attribute, replay the events. Just chuck 'em in a list for each instance.

If performance is an issue then you can have a "snapshot" associated with the individual Pikachu, which is invalidated if any more events happen and recalculated as required.

You can even persist those events by associating them with an individual Pikachu's identity.

(Note that I know nothing about Pokemon.)

You should read about strategy , abstract factory and visitor 3 design patterns.

What is common for your Pokemon is that it can perform different attack with different result. This mean the logic of an attack its quite complicated and depend on the attacker and victim. You can sole this using strategy pattern.

Additionally you will have to extend the creature do perform some actions, this can be achieved using Vistor pattern.

At the end you will have to setup the creature depending on some experience that has. Give them special attacks or form. here you could use the abstract factory.

I am doing a similar project except it is a 2D Pokemon with a Archon based battle sequence. I am also struggling with storing unique values of the Pokemon. I have come up with two solutions, though i am not sure which is more effective.

Solution 1: Many, many classes which all extend the abstract Pokemon class which contains basic values such as type, hp, level, stats but fills them individually

Solution 2: (what i am trying) A generic Pokemon class which has a distinct enumeration as an id. The enumeration identifies the specific pokemon and searches a JDBC derby database for values. As an example:

public enumeration PokeId{
    Pikachu(),
    Mew(),
    Ditto();
}

public class Pokemon{

   private PokeId id;

   public Pokemon(PokeId id){
      this.id = id;
      if(id == PokeId.Pikachu){
         //load from derby

      }
   }

}

I am not sure which is going to be more effective. I hope this helped some and i would love to collaborate on further development of code like this. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM