Sign in to follow this  

Program construction question

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I've been coding a game for a little while now in a largely trial and error method trying to learn as I go along. As of now I am kind of working on two different paths. One is just working my way through graphics tutorials and trying to get a handle on various graphical topics (I am a complete n00b at graphics). Secondly I've been writing a lot of the real game logic with a simple text based interface which I plan on replacing with a graphical one when I feel confident enough to do that. I ran into a problem with on particular class method though, in trying to keep my interface and implementation separate. It seems like a real simple problem to solve (and I apologize in advance if I'm just missing something obvious) but what happens when I need the interface (either text or graphical) to report something several times in the midst of a given class method? Specifically I have an attack method which takes a target and then the attacker and the target exchange blows based on a few dice rolls and so on. I want the interface to report the results of each blow (hit, miss, or whatever) even though several blows are carried out, via a simple loop, each time the method is invoked. Thanks in advance for any help anyone can give, and again, my apologies if I'm missing something obvious. -pleth

Share this post


Link to post
Share on other sites
Quote:

what happens when I need the interface (either text or graphical) to report something several times in the midst of a given class method?

You have a design flaw.

The interface for computing damage shouldn't perform I/O -- that is, it shouldn't report anything via the screen or whatever. It should simply return the damage. The higher-level subsystem -- the one that told the damage calculation subsystem to do its work -- should be the one reporting the results somehow. In some cases, in fact, the result of the calculation should bubble up a couple levels before it finally reaches the system that displays things.

I realize this is fairly abstract, but so was your question. I can illustrate more concretely if you provide an example of what your various subsystems look like right now.

Share this post


Link to post
Share on other sites
I'm sitting in a lab right now, but I'll attempt to sum up what I'm asking with some pseudo-code:

Here is the method in question, it is a member of the Monster Class:

void attack(monster target)
{
int one = numAttacks, two = target.getNumAttacks();

while (one > 0 or two > 0)
{
//a few ifs resulting in the two monsters exchanging blows as long as
//one or the other has blows left and decrementing one and two each time.
//I'd like to be able to report the results of what just happened each time
//through the loop.
}
}

Now what I think is making this difficult for me is that this is not a case of merely trading damage in real time. This is a turn based game I'm talking about and each monster gets a constant number of strikes each time it attacks. Therefore each monster has a member variable which determines how many times it will attack. This is why it made sense to me to include the attack method as a member rather than an independant function (what does a monster do? It attacks.)

Now I guess what I could do is have main or whatever merely retrieve the number of attacks from each participant in the fight prior to running the loop and then actually putting the loop in the I/O part of the program, thereby allowing me to report results after each pass through the loop. But isn't that more of a workaround than a real solution?

-pleth

(is there a simple tutorial on this site on how to make those code boxes within a post? I looked around breifly but didn't see anything readily apparent).

Share this post


Link to post
Share on other sites
It sounds like you need to pull the attacking apart into subfunctions, or perhaps have the method return a "damage report" structure that contains the results of the combined attack (blows traded, etc) like this:


DamageReport Monster::Attack(Monster &other)
{
DamageReport result;
...
return (result);
}


A higher-level system can display the results based on the return value.
If you want more interactivity, consider looking into callbacks.

You can edit my post to see how I put the code box in. Larger blocks of code are done the same way, with "source" tags instead of "code" tags.

Share this post


Link to post
Share on other sites

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this