Jump to content
  • Advertisement
Sign in to follow this  
Shakedown

Major string comparisons

This topic is 3946 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

This question is regarding the Combat System in my text game. Each Unit in the game(players and enemies) have a vector called BattleSequence. The size of this vector increases as the Unit's level increases. When two Units enter combat, their BattleSequence is filled (for enemies), or the player is prompted for their actions prior to each Round. After each Round is complete, the sequences are filled again or prompted for again. There is a list of 6 possible actions: slashLeft, slashRight, swipeLeft, swipeRight, blockLeft, blockRight. blockLeft will block slashLeft and swipeLeft, blockRight will block slashRight and swipeRight. Once the BattleSequence vectors are filled for both Units, the system will compare each element(actions) in each of the Units' BattleSequences. Each Round will consists of all actions being executed. But, if a Unit is a higher level than their opponent and they have a larger BattleSequence, those extra elements(actions) should be executed while the lower Unit does nothing. So my problem is this: How can I do this without writing a bunch of HUGE if/else statements(which is what I have now)? I can't figure out a way to use a switch statement, and...I just can't think of how to do it without a gargantuan mess of if/else statements. I feel like there's a somewhat simple and concise solution just out of my reach. Any ideas? Thank you!

Share this post


Link to post
Share on other sites
Advertisement
struct action 
{
// What the action does
enum action_type { Block, Sweep, Slash } type;

// Where it does it
enum direction { Left, Right } dir;

action(action_type a, direction d) : type(a), dir(d) {}

// Does this hit when faced with the other action?
bool hits(const action & other) const
{
return
(type != Block) &&
((other.di != dir) || (other.type != Block));
}
};

// parse a string representing an order
action parse(const std::string &order)
{
typedef std::map<std::string,action> association;

static association assoc;
if (assoc.empty())
{
const int count = 6;
action actions[count] = {
assoc(Slash,Left), assoc(Swipe,Left), assoc(Block,Left),
assoc(Slash,Right), assoc(Swipe,Right), assoc(Block,Right)
};

std::string names[count] = {
"slashLeft", "swipeLeft", "blockLeft",
"slashRight", "swipeRight", "blockRight"
};

for (int i = 0; i < count; ++i)
assoc[names] = actions;
}

association::iterator found = assoc.find(order);

if (found == assoc.end())
throw AppropriateException();

return found->second;
}

template<typename It1, typename It2>
int victories(It1 begin1, It1 end1, It2 begin2, It2 end2)
{
int count = 0;

while (begin1 != end1)
{
if (begin2 != end2)
{
if (begin1->beats(*begin2)) ++count;
++begin2;
}

else if (begin1->type != action::Block) ++count;

++begin1;
}

return count;
}

std::pair<int,int> victories(const std::vector<action> & a, const std::vector<action> & b)
{
return make_pair(
victories(a.begin(),a.end(),b.begin(),b.end()),
victories(b.begin(),b.end(),a.begin(),a.end()));
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!