PDA

View Full Version : Can I ask a general programming question?


Darkroman
August 4th, 2012, 11:28 PM
I was just wondering if that is allowed here, as I'm attempting to make a text-based pokemon battle simulator in c++ where you can choose a party of 6 pokemon and the attacks it can do. I'm doing this as a side project really for fun, and I figured if I can at least make a fully workable simple c++ text-based program, I can move on to windows programming, and possibly graphics :). I'm up for the challenge!

DaSpirit
August 5th, 2012, 08:15 AM
You shouldn't be asking if it's allowed, you should just be asking the question. There are a few people here who know C++ and could help you out with it (including me).

Full Metal
August 5th, 2012, 10:16 AM
Go for it man! I'll happily give you pointers when you need them! [/pun]

DarkDoom3000
August 5th, 2012, 11:12 PM
Go for it man! I'll happily give you pointers when you need them! [/pun]

I could see that pun comming, but nonetheless, i lol'd XD

Darkroman
August 6th, 2012, 11:53 PM
Here's how my program will work. You first pick 6 pokemon in your party (I have this as 2 vectors of pointers to the pokemon classes, 1 vector for player 1, and 1 for player 2). The pokemon are given random IV's (for the moment), and EV's are at 0. Now, for each pokemon in your party you can choose 4 attacks. Here's where I'm getting some trouble at.

I'm having trouble with is how to implement the battle system itself. To give you a bit of an idea of what I've done so far. I have my base pokemon class, and the derivatives are gonna be the pokemon itself. I understand the flow of the battle and how I want to go about it from pseudocode. The problem I'm having is how to go about it. Do I do each attack as a function, or should I make the attack a class? Also, what's a good way to implement a pokemon's attack table within the class itself?

DaSpirit
August 7th, 2012, 11:51 AM
Why should we program your game for you? Your questions are a bit too general.

I would use a queue to store which attack is going to be executed in which order. The attacks would be all children from an attack class. I would make a virtual "run" function which could let each attack have a unique effect.

Darkroman
August 7th, 2012, 12:33 PM
Believe me, I'm not asking for actual code, just a step in the right direction. Thanks DaSpirit, that may be all I need. However, I'm not familiar with the phrase "virtual run function". Is that the polymorphic class function I'd use to differentiate between the children classes?

Maruno
August 7th, 2012, 12:57 PM
In Essentials, each move is an instance of one class, which reads the compiled data for that move. In battles, each move is an instance of a different class which inherits much of the information from the first class (i.e. base power, etc.). This battle move class also contains various functions that can modify the move's power, accuracy, type and so forth.

It seems to be a reasonable way of doing it. With it, you can modify a particular move's information when required during battle without worrying about also modifying the compiled data for that move (which should remain constant).

KingCharizard
August 8th, 2012, 03:53 AM
In Essentials, each move is an instance of one class, which reads the compiled data for that move. In battles, each move is an instance of a different class which inherits much of the information from the first class (i.e. base power, etc.). This battle move class also contains various functions that can modify the move's power, accuracy, type and so forth. This is the way I'd suggest to do it.. Honestly it would be the best way. This way each attack doesn't need its own function or class. While in battle, lets say pikachu attacks a squritle you could have the attack function check between the pokemon types and move types then have a super effective attack happen. I hope you understand where I'm going with this I just cant word it very well at the moment if your having trouble understanding I will try to explain better.

DaSpirit
August 8th, 2012, 07:03 AM
Believe me, I'm not asking for actual code, just a step in the right direction. Thanks DaSpirit, that may be all I need. However, I'm not familiar with the phrase "virtual run function". Is that the polymorphic class function I'd use to differentiate between the children classes?

If you don't know how to use the virtual keyword, then I don't know if you're cut out for this. C++ is a very difficult language. I recently took a few tests for it online and they turned out to be harder than I thought.

A "virtual" function is a function in a class that has to be overridden by the child. In the parent, you can set the function equal to 0 if you don't want the parent to use it, but you do want the children to.

Dragonite Ernston
August 8th, 2012, 08:32 AM
One of the first things I did while programming Four Star Mon (which is also in C++ :P) was to create a command-line battle interface. So you're not alone on that part :P

Yes, definitely create a move as a base class, but in that base class, have a virtual function called "useMove()" or something that the battle engine will call to run the move. That way, the battle engine doesn't need to care what move is being used (and what class type), since they will all have the same useMove() which will then automatically branch into other things depending on the type of subclass.

Secondly, it's generally a bad idea to implement the Pokémon's attack table and other data into the move class, because there's a way to do things that doesn't require it, and doing so will have multiple instances of the same move in the engine, which really is unnecessary. (In object-oriented programming, this is known as "separation of concerns". Try not to link actions together in the same class if it's not necessary to do so.)

For exapmle, Four Star Mon's move class doesn't have a PP counter, or any data on attack, defense, and accuracy. All that data is stored in the Pokémon who knows the move (the PP counter is placed in an "Attack" class that contains a pointer to the move that the Attack represents). The useMove(), once run, will request this data from the engine or the user of the move by itself.

I'm not familiar with the phrase "virtual run function". Is that the polymorphic class function I'd use to differentiate between the children classes?

Yes, yes it is. Although not "differentiate", it's quite the opposite. It's the one thing that they all have in common and the important thing in common that will be used.

Darkroman
August 9th, 2012, 01:52 AM
Thank you for all your replies, guys. It'll definitely help (once I have time to go back to this program). I do have some follow-up questions, though.

This is the way I'd suggest to do it.. Honestly it would be the best way. This way each attack doesn't need its own function or class. While in battle, lets say pikachu attacks a squritle you could have the attack function check between the pokemon types and move types then have a super effective attack happen. I hope you understand where I'm going with this I just cant word it very well at the moment if your having trouble understanding I will try to explain better

I'm not 100% of what you're saying, but it seems like you're saying what I have planned out. The battle engine itself will check for the move and pokemon type currently in battle. I have my pokemon and move types as an int variable of that pokemon class. I plan to make a function which compares between the pokemon's type and the attack being used against it, as well as checking for STAB for the pokemon currently attacking (doing all damage calculations etc).

In Essentials, each move is an instance of one class, which reads the compiled data for that move. In battles, each move is an instance of a different class which inherits much of the information from the first class (i.e. base power, etc.). This battle move class also contains various functions that can modify the move's power, accuracy, type and so forth.

This part I'm not completely grasping what you're trying to explain here (please forgive me! I'm only learning c++ on my own, haven't really taken an actual course). This is what I think you're talking about: class composition - making one class which has all of its instances being the moves themselves, but through compiled data? As in from an external data file? The battle move class inherits the information through the first class, meaning the class that has each move as an instance of the class?

Yes, definitely create a move as a base class, but in that base class, have a virtual function called "useMove()" or something that the battle engine will call to run the move. That way, the battle engine doesn't need to care what move is being used (and what class type), since they will all have the same useMove() which will then automatically branch into other things depending on the type of subclass.

Yes. My original plan was like this. Create a base 'attack/move' class with a function similar to 'useMove()' as a pure virtual function to make it an abstract class. Which brings me to what I had originally planned for some parts of the whole battle engine.

My pokemon base class will have an attack list vector, representing the 4 attacks the user chooses to have his/her pokemon to have. The derived pokemon (the pokemon themselves) will inherit their own list/vector where these would store pointers to the attacks. In the main program, the user chooses the attacks he/she would like to use and put the attack in the list (something along the lines attack_list.push_back(new attack()) ). In my main 'battle()' function, where the main battle engine will be where you can choose an attack for the pokemon that's first in your party as well as various other stuff, would call the 'attack() function of that pokemon class. The 'attack()' function will pass an integer (based on which attack slot the user chooses) into the function that will determine which attack the pokemon will use from the list. The attacks would have, similar to what Dragonite Ernston says, a useMove() function that determines its power, accuracy, status effect, etc etc. It will also check the opponent's pokemon type, defense, all that good stuff to calculate damage and apply miscellaneous effects.

Now that you guys have an idea of what I had originally planned to do, you could possibly clarify, and correct me on my chosen plan. I'm understanding more and more as I write this post out and discuss it.

Edit: Wow, didn't realize my post was so long!

Dragonite Ernston
August 9th, 2012, 07:16 AM
Yes. My original plan was like this. Create a base 'attack/move' class with a function similar to 'useMove()' as a pure virtual function to make it an abstract class. Which brings me to what I had originally planned for some parts of the whole battle engine.

Well, seems like we think alike then!

My pokemon base class will have an attack list vector, representing the 4 attacks the user chooses to have his/her pokemon to have. The derived pokemon (the pokemon themselves) will inherit their own list/vector where these would store pointers to the attacks.

Wait, lemme get this straight, you're going to make a subclass for each Pokémon? Couldn't you just use that base class by itself and do pretty much the exact same thing? There's no need to inherit stuff if you're just making objects out of a class; only if you're making classes out of other classes.

In the main program, the user chooses the attacks he/she would like to use and put the attack in the list (something along the lines attack_list.push_back(new attack())). In my main 'battle()' function, where the main battle engine will be where you can choose an attack for the pokemon that's first in your party as well as various other stuff, would call the 'attack() function of that pokemon class. The 'attack()' function will pass an integer (based on which attack slot the user chooses) into the function that will determine which attack the pokemon will use from the list. The attacks would have, similar to what Dragonite Ernston says, a useMove() function that determines its power, accuracy, status effect, etc etc. It will also check the opponent's pokemon type, defense, all that good stuff to calculate damage and apply miscellaneous effects.

I think, rather than a battle function, you should have an entire object to manage that part. Battling is a multi-turn process, which is rather hard to do in just one function, even if you use while loops.

Also, make the Attack class a wrapper for the Move class that points to the Move. You don't want to have multiple instances of the same move class (which will do the exact same thing) in the program if you don't need to (and you don't).

Also, the power, accuracy, etc. should be stored in the Move class itself, not determined by useMove().

If you want ideas about how to do it, check out the source code of this old Java engine that I never finished: http://github.com/joe-zeng/java-pokemon

Now that you guys have an idea of what I had originally planned to do, you could possibly clarify, and correct me on my chosen plan. I'm understanding more and more as I write this post out and discuss it.

Edit: Wow, didn't realize my post was so long!

Don't worry, I've made longer.

FL
August 12th, 2012, 07:26 AM
This is the way I'd suggest to do it.. Honestly it would be the best way. This way each attack doesn't need its own function or class. While in battle, lets say pikachu attacks a squritle you could have the attack function check between the pokemon types and move types then have a super effective attack happen. I hope you understand where I'm going with this I just cant word it very well at the moment if your having trouble understanding I will try to explain better.I agree, this is a very good polymorphism use. Also notes that the moves uses a dynamic class call based in each move function code.

Darkroman
August 25th, 2012, 12:30 AM
Thanks for all the advice guys. I'll put this to work once I'm done with college here in a couple weeks. I'll also be looking at the pokemon online battle simulator source for a guideline, too. Again thank you :)