Domanda

i'm writing a simple project in C++ for my programing class. I'm making a simple pokemon battle simulator which runs in the console

The problem ive run into however, is what is the best way to store the data of the pokemon? I have to store things such as HP, Attack, Defense, etc in a file, but I'm not sure whats the best way to approach this. I know some basics of reading and writing to a file, but nothing that fulfills what I'm trying to do. I was thinking using YAML, but after spending hours trying to figure it out I gave up, since i dont think i need something that complex

I guess some psuedocode for what im trying to do would be like

Open file
find string with name "pikachu"
find defense for pokemon pikachu
defense = pikachu defense
find HP for pokemon pikachu
HP = pikachu HP
find attack for pokemon pikachu
attack = pikachu attack

So what would be the best way to do this, tool to use?

È stato utile?

Soluzione

You can write some structs like this:

// A separate struct for stats allow you to add and remove
// stats parameters without touch pokemon struct.
struct stats
{
    int hp;
    int attack;
    int defense;
};

typedef struct stats stats_t;



struct pokemon
{
    string name;
    stats_t stats;

    // You can also add some behaviour to your pokemons.

    void defend_from_attack(int incomming_attack)
    {
        // And for instance.
        stats.defense -= incomming_attack;
    }

    void attack(pokemon attacked_pokemon)
    {
        attacked_pokemon.defend_from_attack(stats.attack);
    }

    // This is cool right?
    void evolve(stats_t new_stats)
    {
        stats = new_stats;
    }

}

All you need now is a function that reads the file (or files) where you put your pokemon's stats. If you're using YAML is even easier. That function has to read the file and return an instance of pokemon with the required data.

The following example follows your seudocode:

// Assume the YAML file has been open.

// find string with name "pikachu"
struct pokemon pikachu;
// find defense for pokemon pikachu
// defense = pikachu defense
pikachu.stats.defense = defense;
//find HP for pokemon pikachu
//HP = pikachu HP
pikachu.stats.hp = HP;
//find attack for pokemon pikachu
//attack = pikachu attack
pikachu.stats.attack = attack;

Implementing that function is up to you, I think you must already have an idea of how to doit.

Altri suggerimenti

The best way to store this data in runtime is to create a class structure for pokemon. This means you will have a class called pokemon, it will have various attributes such as name, hp, and attack. If you want to save this data over multiple executions; you are correct, some file I/O will be needed. Once you have the class structure, it is simple to make functions that write and read the pokemon data to files. Check this links for more info if you are unfamiliar with object orientation. http://www.cplusplus.com/doc/tutorial/classes/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top