• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Most efficient way to compare integers?

Status
Not open for further replies.
Level 5
Joined
Jun 24, 2012
Messages
112
I'm making a complex voting system that allows players to vote on 1 of 4 game modes and then vote on 1 of 3 options of that game mode.
The voting system first needs to select the most popular game mode, then the most popular game option regardless of game mode.

If one voting option has a greater amount of votes than all the others, it's easy to perform the necessary actions, BUT if 2 or more game modes have an equal amount of votes, I need the system to randomly select one of the more popular game modes.
The same needs to be repeated for the game options, which vary based on the game mode.

So far, I've tried doing a massive number of if/then/elses but it's not very efficient and hard to read/troubleshoot, etc.
I'm using loops but am kinda stuck on some stuff.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Decouple the actual poll from the poll system. This way you could reuse the same code for both votes managed by some control code.

The idea is the poll system gets set up, started and then runs a result thread when it completes. You can use either triggers or a string of a function name to create the thread.

To handle tie situations you can use a similar process to how the BJ performs a random unit in group selection. If that is not possible use a cache array list to store tie results and then pick a random element from that.
 
Level 5
Joined
Jun 24, 2012
Messages
112
This is what my trigger looks like right now:

  • Tally Votes Copy
    • Events
      • Dialog - A dialog button is clicked for GameModeOptionBox
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Clicked dialog button) Equal to GameModeButton[0]
        • Then - Actions
          • Set Vote[4] = (Vote[4] + 1)
        • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Clicked dialog button) Equal to GameModeButton[1]
            • Then - Actions
              • Set Vote[5] = (Vote[5] + 1)
            • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Clicked dialog button) Equal to GameModeButton[2]
                • Then - Actions
                  • Set Vote[6] = (Vote[6] + 1)
                • Else - Actions
      • -------- --- --------
      • -------- Comparing Last Man Standing --------
      • -------- --- --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • And - All (Conditions) are true
            • Conditions
              • Vote[0] Greater than Vote[1]
              • Vote[0] Greater than Vote[2]
              • Vote[0] Greater than Vote[3]
        • Then - Actions
          • Trigger - Run Last Man Standing <gen> (ignoring conditions)
        • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • And - All (Conditions) are true
                • Conditions
                  • Vote[0] Equal to Vote[1]
                  • Vote[0] Greater than Vote[2]
                  • Vote[0] Greater than Vote[3]
            • Then - Actions
              • Trigger - Run GameMode[(Random integer number between 0 and 1)] (ignoring conditions)
            • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • And - All (Conditions) are true
                    • Conditions
                      • Vote[0] Equal to Vote[1]
                      • Vote[0] Equal to Vote[2]
                      • Vote[0] Greater than Vote[3]
                • Then - Actions
                  • Trigger - Run GameMode[(Random integer number between 0 and 2)] (ignoring conditions)
                • Else - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • And - All (Conditions) are true
                        • Conditions
                          • Vote[0] Equal to Vote[1]
                          • Vote[0] Equal to Vote[2]
                          • Vote[0] Equal to Vote[3]
                    • Then - Actions
                      • Trigger - Run GameMode[(Random integer number between 0 and 3)] (ignoring conditions)
                    • Else - Actions
                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                        • If - Conditions
                          • And - All (Conditions) are true
                            • Conditions
                              • Vote[0] Greater than Vote[1]
                              • Vote[0] Equal to Vote[2]
                              • Vote[0] Greater than Vote[3]
                        • Then - Actions
                          • Set RandomSelector[0] = 0
                          • Set RandomSelector[1] = 2
                          • Trigger - Run GameMode[RandomSelector[(Random integer number between 0 and 1)]] (ignoring conditions)
                        • Else - Actions
                          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                            • If - Conditions
                              • And - All (Conditions) are true
                                • Conditions
                                  • Vote[0] Greater than Vote[1]
                                  • Vote[0] Equal to Vote[2]
                                  • Vote[0] Equal to Vote[3]
                            • Then - Actions
                              • Set RandomSelector[0] = 0
                              • Set RandomSelector[1] = 2
                              • Set RandomSelector[2] = 3
                              • Trigger - Run GameMode[RandomSelector[(Random integer number between 0 and 2)]] (ignoring conditions)
                            • Else - Actions
                              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                • If - Conditions
                                  • And - All (Conditions) are true
                                    • Conditions
                                      • Vote[0] Greater than Vote[1]
                                      • Vote[0] Greater than Vote[2]
                                      • Vote[0] Equal to Vote[3]
                                • Then - Actions
                                  • Set RandomSelector[0] = 0
                                  • Set RandomSelector[1] = 3
                                  • Trigger - Run GameMode[RandomSelector[(Random integer number between 0 and 1)]] (ignoring conditions)
                                • Else - Actions
                                  • -------- --- --------
                                  • -------- Comparing Deathmatch --------
                                  • -------- --- --------
                                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                    • If - Conditions
                                      • And - All (Conditions) are true
                                        • Conditions
                                          • Vote[1] Greater than Vote[0]
                                          • Vote[1] Greater than Vote[2]
                                          • Vote[1] Greater than Vote[3]
                                    • Then - Actions
                                      • Trigger - Run Deathmatch <gen> (ignoring conditions)
                                    • Else - Actions
                                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                        • If - Conditions
                                          • And - All (Conditions) are true
                                            • Conditions
                                              • Vote[1] Equal to Vote[0]
                                              • Vote[1] Equal to Vote[2]
                                              • Vote[1] Greater than Vote[3]
                                        • Then - Actions
                                          • Trigger - Run GameMode[(Random integer number between 0 and 2)] (ignoring conditions)
                                        • Else - Actions
                                          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                            • If - Conditions
                                              • And - All (Conditions) are true
                                                • Conditions
                                                  • Vote[1] Greater than Vote[0]
                                                  • Vote[1] Equal to Vote[2]
                                                  • Vote[1] Greater than Vote[3]
                                            • Then - Actions
                                              • Trigger - Run GameMode[(Random integer number between 1 and 2)] (ignoring conditions)
                                            • Else - Actions
                                              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                • If - Conditions
                                                  • And - All (Conditions) are true
                                                    • Conditions
                                                      • Vote[1] Greater than Vote[0]
                                                      • Vote[1] Equal to Vote[2]
                                                      • Vote[1] Equal to Vote[3]
                                                • Then - Actions
                                                  • Trigger - Run GameMode[(Random integer number between 1 and 3)] (ignoring conditions)
                                                • Else - Actions
                                                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                    • If - Conditions
                                                      • And - All (Conditions) are true
                                                        • Conditions
                                                          • Vote[1] Greater than Vote[0]
                                                          • Vote[1] Greater than Vote[2]
                                                          • Vote[1] Equal to Vote[3]
                                                    • Then - Actions
                                                      • Set RandomSelector[0] = 1
                                                      • Set RandomSelector[1] = 3
                                                      • Trigger - Run GameMode[RandomSelector[(Random integer number between 0 and 1)]] (ignoring conditions)
                                                    • Else - Actions
                                                      • -------- --- --------
                                                      • -------- Comparing Hold the Flag --------
                                                      • -------- --- --------
                                                      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                        • If - Conditions
                                                          • And - All (Conditions) are true
                                                            • Conditions
                                                              • Vote[2] Greater than Vote[0]
                                                              • Vote[2] Greater than Vote[1]
                                                              • Vote[2] Greater than Vote[3]
                                                        • Then - Actions
                                                          • Trigger - Run Hold the Flag <gen> (ignoring conditions)
                                                        • Else - Actions
                                                          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                            • If - Conditions
                                                              • And - All (Conditions) are true
                                                                • Conditions
                                                                  • Vote[2] Greater than Vote[0]
                                                                  • Vote[2] Greater than Vote[1]
                                                                  • Vote[2] Equal to Vote[3]
                                                            • Then - Actions
                                                              • Trigger - Run GameMode[(Random integer number between 2 and 3)] (ignoring conditions)
                                                            • Else - Actions
                                                              • -------- --- --------
                                                              • -------- Comparing Team Hold the Flag --------
                                                              • -------- --- --------
                                                              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                                                                • If - Conditions
                                                                  • And - All (Conditions) are true
                                                                    • Conditions
                                                                      • Vote[3] Greater than Vote[0]
                                                                      • Vote[3] Greater than Vote[1]
                                                                      • Vote[3] Greater than Vote[2]
                                                                • Then - Actions
                                                                  • Trigger - Run Team Hold the Flag <gen> (ignoring conditions)
                                                                • Else - Actions
                                                                  • Trigger - Run GameMode[(Random integer number between 0 and 3)] (ignoring conditions)
As you can see, it's a lot of conditions for only a few options. If I ever add game modes in the future it will complicate things a lot more. Moreover, the trigger only checks for the game mode and not the game mode options. If I want to do the game mode options, I have to do all of this over again and add more if/then/else statements to each game mode trigger so it knows what to do based on the value that would be set if I were to expand the trigger to check for game mode options.

I really don't like how it's triggered right now and want something more efficient, easy to modify and read.
 
Level 5
Joined
Jun 16, 2004
Messages
108
Here is an example implementation of one of the things Dr Super Good mentioned (storing tie votes into an array).
  • Untitled Trigger 003
    • Events
    • Conditions
    • Actions
      • Set MaxVote = 0
      • -------- first pass calculates the highest number of votes for any one mode --------
      • For each (Integer A) from 0 to 3, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Vote[(Integer A)] Greater than MaxVote
            • Then - Actions
              • Set MaxVote = Vote[(Integer A)]
            • Else - Actions
      • -------- second pass takes all votes that match the highest vote and puts them into an array --------
      • For each (Integer A) from 0 to 3, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Vote[(Integer A)] Equal to MaxVote
            • Then - Actions
              • Set VotePool[VoteSize] = (Integer A)
              • Set VoteSize = (VoteSize + 1)
            • Else - Actions
      • -------- pick a random number from 0 to votesize-1, then use that number to index votepool for the game mode --------
      • Set GameMode = VotePool[(Random integer number between 0 and (VoteSize - 1))]
The idea is to do two passes through your votes, one to find the max number, the next to build an array of possible votes to pick. After that you simply pick randomly from the built array to get your game mode.

If there are no ties then the array will have only one value, and the random call will return just that.

Well I did not test the example, but the idea is simple enough. Currently loops from 0 to 3, but you can have it loop for more votes by changing the 3 to whatever number your votes go to (change it for both integer a loops).
 
Status
Not open for further replies.
Top