What algorithm best describe this?

Started by
18 comments, last by wodinoneeye 12 years, 8 months ago

Ugh... separate the decision logic from the result. Bad code style, dude. There is so much duplication of code in there that my head was spinning. That whole thing could be reduced to about 20 lines.

IA is right. But since you seem to be new at this, we won't be too harsh :)

Write a single function that checks two pieces and returns a result of who won (or a tie). Then based on that result, you only have a couple of branches to tell the player and the game what happened.
[/quote]
This guy knows what he's talking about. The code that can be pulled into its own function should be clear as day. Since you have it repeated for every if statement. **hint, hint**

Beginner in Game Development?  Read here. And read here.

 

Advertisement

I would probably not use any conditionals to determine who wins, but a lookup table instead:
[/quote]
It seems to me that a lookup table is unnecessary. Maybe I'm misunderstanding the rules though.

I see a few special cases (attacking/defending spy, attacking flag). All the other cases can be decided, as IADaveMark said, by using the appropriate comparison operator - provided we define the internal identifier of the pieces in the correct order:

enum Piece { FiveStarGeneral, FourStarGeneral, ThreeStarGeneral, /*...*/, Spy, Flag, NPieces };

enum BattleResult { AttackerWins, NobodyWins, DefenderWins };

BattleResult compute_battle(Piece attacker, Piece defender)
{
// Equal pieces eliminate one another
if(attacker == defender)
{
// Special case: attacking flag eliminates defending flag
if(attacker == Flag)
{
return AttackerWins;
}
return NobodyWins;
}

// Special case: Spy attacking (spies lose to privates)
if(attacker == Spy)
{
return defender == Private ? DefenderWins : AttackerWins
}

// Special case: Spy defending (spies lose to privates)
if(defender == Spy)
{
return attacker == Private ? AttackerWins : DefenderWins;
}

// Pieces go from low values to high in the Piece enum, so we use "less than" here...
return attacker < defender ? AttackerWins : DefenderWins;
}
Oh thanks to all of you.. Im gonna try it.. Sorry for my code, im just starting to learn the JAVA..
It is written "Java", not "the JAVA". Java is not an acronym! :)

This guy knows what he's talking about.

Meh... I fake it really well.


Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

Question Again: What Algorithm Can I Implement on My Arbiter? Any Suggestion?

Thanks!
Question Still Doesn't Make Sense.

You're welcome!

Question Again: What Algorithm Can I Implement on My Arbiter? Any Suggestion?

Thanks!

There is no "algorithm". You write code. Of course, anything you write will be an algorithm, won't it?

We've already told you that all you need to do is a little more than a simple "<" check for most of your conditions. There are a few exceptions, of course. This isn't even AI as much as it is simple ordinal math. 1 < 2. 2 < 3. 1 < 3. 1 < 7. 3 < 7... whatever. All of that can be checked with, "is A < B?" You seem to be desperate to make it harder than that.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

What do you think an algorithm is?

Named algorithms are just basic ones that come up again and again, and are the building blocks for complex algorithms.

I just made a board game, and i dont know what Algorithm best describe this referee or arbiter.

The role of arbiter is to check which rank is higher and then remove the lower rank pieces..

I use a lot if IF CONDITIONS to make it work..

Do you know any algorithm that can i put on my papers?

Thanks!



A 2 dimensional interaction lookup table is pretty easy way (as long ads the 'ranks' are static)

The two ranks are indexed X for side A and Y for side B and the intesection (value in the array) is the action to be taken :
Remove A
Remove B
Remove A and B (mutual destruction)

the values are just an enum ordinal that your 3 clause if then logic will then evaluate and carry out those operations
--------------------------------------------[size="1"]Ratings are Opinion, not Fact

This topic is closed to new replies.

Advertisement