Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Intense Degree on Wed 11/07/2012 18:12:42

Title: Discerning the highest variable in an array in a struct.
Post by: Intense Degree on Wed 11/07/2012 18:12:42
I have a struct which is declared like this:

Code (AGS) Select
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) Select
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:


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?
Title: Re: Discerning the highest variable in an array in a struct.
Post by: Crimson Wizard on Wed 11/07/2012 18:22:38
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) Select

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++ :)
Title: Re: Discerning the highest variable in an array in a struct.
Post by: Intense Degree on Thu 12/07/2012 09:39:14
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:
Title: Re: Discerning the highest variable in an array in a struct.
Post by: Crimson Wizard on Thu 12/07/2012 10:03:21
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.