zahlen schnell vergleichen

  • zahlen schnell vergleichen

    einen juten morgen allerseits
    ich bastel gerade an ner ki bei der die ki selbst eintscheiden soll was sie als nächstes tun wird aufgrund ihrer bedürfnisse. um nun nicht lauter if hunger is lager than durst und solches zeug zu machen (die ki hat an die 10 bedürfnisse das würd jahre dauern) wollte ich wissen ob eine funktion gibt die dir den größten wert aus einer reihe raussuchen kann?

    mfg johannski
  • ja an sowas hab ich gedacht das is schon mal großartig....jetz nur noch 2 fragen bevor ich weiterarbeite:

    1. kann ich es schaffen dass die ausgabe von max(.....) nicht eine zahl sondern die variable mit der höchsten zahl ist?

    2. Was is der vorteil ann listen und welche soll ich benutzen?

    hm die fragen werden wohl immer mehr :P
  • Listen sind einfach sinnvoller und flexibler. Lies dir am besten die Hilfe dazu mal durch.
    Oder einfacher lies den Spoiler :D ...

    Spoiler anzeigen
    A list stores a collection of values in a particular order. You can add values at the end or insert them somewhere in the middle of the list. You can address the values using an index. Also you can sort the elements, either in ascending or descending order. Lists can be used in many ways, for example to store changing collections of values. They are implemented using simple arrays but, as this is done in compiled code it is a lot faster than using an array yourself. The following functions are available:

    ds_list_create() Creates a new list. The function returns an integer as an id that must be used in all other functions to access the particular list.
    ds_list_destroy(id) Destroys the list with the given id, freeing the memory used. Don't forget to call this function when you are ready with the structure.
    ds_list_clear(id) Clears the list with the given id, removing all data from it but not destroying it.
    ds_list_copy(id,source) Copies the list source into the list with the given id.
    ds_list_size(id) Returns the number of values stored in the list.
    ds_list_empty(id) Returns whether the list is empty. This is the same as testing whether the size is 0.
    ds_list_add(id,val) Adds the value at the end of the list.
    ds_list_insert(id,pos,val) Inserts the value at position pos in the list. The first position is 0, the last position is the size of the list minus 1.
    ds_list_replace(id,pos,val) Replaces the value at position pos in the list with the new value.
    ds_list_delete(id,pos) Deletes the value at position pos in the list. (Position 0 is the first element.)
    ds_list_find_index(id,val) Find the position storing the indicated value. If the value is not in the list -1 is returned.
    ds_list_find_value(id,pos) Returns the value stored at the indicated position in the list.
    ds_list_sort(id,ascend) Sorts the values in the list. When ascend is true the values are sorted in ascending order, otherwise in descending order.
    ds_list_shuffle(id) Shuffles the values in the list such that they end up in a random order.
    ds_list_write(id) Turns the data structure into a string and returns this string. The string can then be used to e.g. save it to a file. This provides an easy mechanism for saving data structures.
    ds_list_read(id,str) Reads the data structure from the given string (as created by the previous call).



    Die frage mit der größe der Variable versteh ich nicht ganz...
    Bzw. du kannst das einfach so schreiben, moment doch jetzt versteh ich es. Du müsstest unnötig tricksen um nicht nur die Größe der größten Variable zu erhalten. Wenn du auch den Variablen Namen erhalten willst, dann sind listen einfach besser. Mein Tipp benutz listen, die können auch die n-t größte Größe zurückgeben, du kannst sie einfach sotieren, etwas hinzufügen, entfernen. Und speichern lässt sich auch ganz einfach machen...

    [So viele Größen ;( :D ]
    PULSE

    Zweispieler [||||||||||]
    Einspieler [||||||||||]

    [Die Entgrater ist auf Eis gelegt]
  • JOhannski schrieb:


    2. Was is der vorteil ann listen und welche soll ich benutzen?


    Ganz einfach:

    They are implemented using simple arrays but, as this is done in compiled code it is a lot faster than using an array yourself.


    Hmm zu langsam. Hab mir mit dem Antworten Button zu viel Zeit gelassen >.<
    mfg Critical
  • Das wird nicht mit Listen funktionieren, da in diesen nur die Werte abgespeichert würden und nach dem sortieren keine Zuordnung zu den Variablennamen mehr stattfinden kann, bzw. nur umständlich, da man jede Variable mit dem größten Wert vergleichen müsste um die richtige Variable zu finden. Dann könnte man auch gleich max(...) verwenden.

    Probier es mal mit folgendem Script:

    GML-Quellcode

    1. // Script: scr_variable_max
    2. // argument0 = Listen ID mit Variablennamen
    3. // Gibt Variablennamen mit größtem Wert zurück
    4. var max_name, i;
    5. if (ds_list_size(argument0) > 0) {
    6. max_name = ds_list_find_value(argument0,0);
    7. }
    8. for (i = 1; i < ds_list_size(argument0); i += 1) {
    9. if (variable_local_get(ds_list_find_value(argument0,i)) > variable_local_get(max_name)) {
    10. max_name = ds_list_find_value(argument0,i);
    11. }
    12. }
    13. return max_name;
    Alles anzeigen

    und dann kannst mit diesem Script die Variable mit dem größten Wert erhalten. z.B. so:

    GML-Quellcode

    1. bla1 = 10;
    2. bla2 = 12;
    3. bla3 = 8;
    4. bla4 = 11;
    5. ind = ds_list_create();
    6. ds_list_add(ind,"bla1");
    7. ds_list_add(ind,"bla2");
    8. ds_list_add(ind,"bla3");
    9. ds_list_add(ind,"bla4");
    10. show_message(scr_variable_max(ind));
    Alles anzeigen
  • Dank meines neuen Wissens und Bl@ckSp@rk Hilfe, habe ich auch einen Lösungansatz für dich.

    Der Vorteil ist, das du alles in einer Map verwaltest und so deinen Globalen Namensraum nicht mit Variablen vollmüllst:

    GML-Quellcode

    1. //ds_map_find_max
    2. //@param ds_map Die zu prüfende Map
    3. var map, cur, i, size, maxValue, val, keyFound;
    4. map = argument0;
    5. cur = ds_map_find_first(map);
    6. size = ds_map_size(map);
    7. maxValue = ds_map_find_value(map, cur);
    8. keyFound = "";
    9. for(i = 0; i < size; i += 1) {
    10. val = ds_map_find_value(map, cur);
    11. if(maxValue <= val) {
    12. maxValue = val;
    13. keyFound = cur;
    14. }
    15. cur = ds_map_find_next(map, cur);
    16. }
    17. return keyFound;
    Alles anzeigen


    Test:

    GML-Quellcode

    1. map = ds_map_create();
    2. ds_map_add(map, "a", 1);
    3. ds_map_add(map, "b", 40);
    4. ds_map_add(map, "d", 5);
    5. ds_map_add(map, "c", 14);
    6. show_message(ds_map_find_max(map));


    Erwartet: b
    Ergebnis: b
    On teh internet u pwn noobs - but in real life noobs own you.
  • Benutzer online 2

    2 Besucher