How do I Compare four variables and find out which one is the biggest? What happens in the tie?

0 favourites
  • 8 posts
From the Asset Store
Footsteps SFX Four contains 400 sounds of steps and jumps on different surfaces.
  • Hello Hello!

    For the project I'm doing I need to compare four variables - apples, pears, oranges and coconuts.

    You enter a certain number of each fruit, and then when you press a button - the variable that you have the most of is saved as a new variable (we'll call it "biggest") and then you have a variable (biggest) that tells you what you have the most of (and then of course you can do whatever you want to display with it as text or image, etc., etc.)

    How do I create a comparison between four variables??

    Here comes the tweets - what if you have 2 or more fruits that are in a tie for the highest number? Let's say there are the most apples and oranges. How can I tall "biggest" to save it as variables and then say "show one of them at random"???

    Thank you. I'm trying to crack it and couldn't get to the answer!

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Well you can do it with a bunch of conditions. For two vars

    If A>B

    If B<A

    If A=B

    For three you just do the same for all cases

    a>b and a>c

    B>a and b>c

    C>a and c>b

    A=b and a>c

    A=c and a>b

    B=c and b>a

    A=b and b=c

    For four it would be similar.

    Another way would be an array.

    3 a

    22 b

    4 c

    Then you could sort the array to get the greatest, but you’d need some logic to when there are multiple with the same value.

    A third idea that I like best is to use sprites instead. Make a sprite called item, give it two variables: name and value. Then just create an instance for each fruit and set the vars.

    Finally to get the highest and get a random one when there is a tie you’d do:

    For each item ordered by item.value+random(0.5) descending

    — set best to item.name

    — stop loop

  • I'd go the array route.

    1. Array with your dataset

    2. Sort Array based on amount

    3. Loop through array, copy the current entry into another Array. Compare the "loopindex" amount with the "loopindex+1" amount. If the comparison is true, repeat the loop, otherwise stop the loop.

    4. You now have an array that contains all fruits that tied for the highest amount (one or more)

    5. You can output a random entry from that array like Array.At(floor(random(0,Array.length+1)))

  • Here is my version

  • Thank you all for taking the time to give me answers.

  • Here is my version

    I can't believe you took what i wes able to produce in 23 lines of code and reduced it to three. This is amazing (I didn't know MAX existed at all)

    There is only one thing I have trouble with and that is the last line. which displays the name of the random variable. I can't understand the line.

    The part I didn't understand is: mid(list,random(len(list)),1)

    After we have created a list of all the variables that are equal to the largest number (MAXX)

    How does the random selection from the list happen here?

    My ultimate goal is that in the end the some STRING variable will receive one of the options:

    apples, pears, oranges, coconuts

    (Then of course I can play with it for my purposes)

    I would appreciate your help on this matter.

  • The part I didn't understand is: mid(list,random(len(list)),1)

    In my case the list of variables will be something like this: acd

    So to extract a random variable name (one letter) from that string I'm using mid() expression, which extracts one character at random(len(list)) position.

    In your case I suggest a different approach. You can build a list of variables separated by space or comma:

    apples pears oranges

    Having space as a separator is easier because you can use trim(list) to remove any extra leading/ending spaces. Then extract one variable name using tokenat/tokencount:

    Set list to trim(list)

    Browser Log tokenat(list, random(tokencount(list, " "), " ")

    See the documentation if you need help with any of these expressions:

    construct.net/en/make-games/manuals/construct-3/plugin-reference/text

  • Here's my last idea. Useful for any number of variables.

    dropbox.com/scl/fi/60yypcux55y0cy0edb0ws/highestVar_randInstWhenTied.capx

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)