Sorting Algorithm

Started by Calin Leafshade, Wed 25/11/2009 00:59:25

Previous topic - Next topic

Calin Leafshade

I'm having a kind of mental block.
I know this is a simple thing to do but for some reason my brain isnt working.

I have an array of characters with various attributes (agility etc. Characters[0].Agl )
however they also have a boolean flag which indicates whether or not they have had their turn in combat yet. (Characters[0].HadTurn)

I need to find which character in the array has the highest agility but also has not had their turn yet.

Helpz? lolz ^___^ kthxbi

Crimson Wizard

Why, thats terribly simple.

int i;
int MaxAgl;
int iChar = -1;

while (i < MAXCHARACTERS)
{
  if (!Characters[ i ].HadTurn)
 {
     if (Characters[ i ].Agl > MaxAgl)
     {
          MaxAgl = Characters[ i ].Agl;
          iChar = i;
     }
 }
 i++;
}

iChar will be index of character you need, or -1 if all characters had their turn.

Calin Leafshade

much obliged wizard.. that is alarmingly simple.

I feel stupid now.

Crimson Wizard

Oh, and yes, you may also make condition

     if (Characters[ i ].Agl > MaxAgl || Characters[ i ].Agl == MaxAgl && Random(99) < 50)

This will make an action order between characters with same Agility a bit randomized ;)
(Otherwise they will act in order of their appearance in characters array, which is not really fair, IMO)

SMF spam blocked by CleanTalk