Discerning the highest variable in an array in a struct.

Started by Intense Degree, Wed 11/07/2012 18:12:42

Previous topic - Next topic

Intense Degree

I have a struct which is declared like this:

Code: AGS
struct pmoves{
  int value;
};

pmoves pmove[25];


(There is more of it, but this is the relevant bit for this question)

In the game each of pmove[1] - pmove[24] has a value of between 0-20, which is calculated on a number of factors within the game which change very frequently.

What I want is a function which will tell me which pmove has the highest value at any time. [in the event that there is more than one that shares the highest value, I just need it to return any one of them].

So far I have:

Code: AGS
function movechooser(){
  chosenmove = 0;
  choosemove = 1;
  while (choosemove <25){
    if (pmove[choosemove].value==1){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==2){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==3){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==4){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==5){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==6){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==7){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==8){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==9){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==10){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==11){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==12){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==13){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==14){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==15){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==16){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==17){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==18){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==19){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  choosemove=1;
  while (choosemove <25){
    if (pmove[choosemove].value==20){
      chosenmove=choosemove;
      choosemove=25;
    }
    choosemove++;
  }
  Display ("I have chosen move %d", chosenmove);
}


The logic (at least in my mind!) behind the above:


  • Choosemove is a variable to allow me to sort through the 24 options.
  • Chosenmove is a variable that I want to set to show me which of the pmove has the highest (or one of the highest) values.
  • Therefore first I cycle through all pmove.value and as soon as I get to the first one with a value of 1 chosenmove is set to that pmove. This is then repeated for a value of 2 and so on all the way up until 20. Therefore chosenmove ends up being set to the first of the pmove with the highest value.

Whilst I haven’t finished testing it properly (it is difficult with respect to where it is in the game and is conditional on one or two other bits that aren't quite working!), I can’t believe this is anything like the best way to do it but I’m having a bit of a mental meltdown at the moment and could do with some pointers or help!

Besides my stupid idea of using 2 variables that look almost identical(!) how can I do this better?

Crimson Wizard

#1
Quote from: Intense Degree on Wed 11/07/2012 18:12:42
What I want is a function which will tell me which pmove has the highest value at any time. [in the event that there is more than one that shares the highest value, I just need it to return any one of them].

Unless I missed something, here's the solution:
Code: ags

int getmaxpmoveindex(){
   int max_pmove = -1;
   int max_pmove_index = -1;
   int i = 0;
   int number_of_pmoves = 25;

   while (i < number_of_pmoves)
   {
      if (pmove[i].value > max_pmove)
      {
         max_pmove = pmove[i].value;
         max_pmove_index = i;
      }

      i++;
   }

   return max_pmove_index;
}


EDIT: forgot i++ :)

Intense Degree

Ah! Always so simple when I see it, thanks very much CW. :smiley:

I think my main trouble is that I haven't done anywhere near enough programming to think like a programmer sometimes. :sad:

Crimson Wizard

#3
Quote from: Intense Degree on Thu 12/07/2012 09:39:14
I think my main trouble is that I haven't done anywhere near enough programming to think like a programmer sometimes. :sad:
Well, first rule is: the simple = the correct. I remember when I started to learn programming I did exactly same mistakes.
So, here's the hint: if you feel your algorithm becomes too complex, especially with lots of duplicating code, this usually means you are doing something terribly silly. ;)
perhaps you should put your code aside and think it over from the start.

EDIT: Ah, yes, and regarding current case: in the situations like this try to look for a way to merge similar/duplicating code; ask yourself, what is common in all those blocks from mathematical/logical point of view.

SMF spam blocked by CleanTalk