• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] How to find max integer in series

Status
Not open for further replies.
title looks silly but it isn't as easy as it seems ;]

I have bunch if integers data, after players click dialog button /multiplayer game/
VoteFor[0] //- number of votes for red player VoteFor[1] //- number of votes for blue player ..... VoteFor[7] //- number of votes for pink player VoteFor[11] //- numer of votes for "close system"
so the result can be for example:

example1
VoteFor[0] = 0, VoteFor[1] = 1, VoteFor[2] = 0, VoteFor[3] = 2
VoteFor[4] = 0, VoteFor[5] = 0, VoteFor[6] = 0, VoteFor[7] = 0
VoteFor[11] = 0

example2
VoteFor[0] = 1, VoteFor[1] = 2, VoteFor[2] = 2, VoteFor[3] = 0
VoteFor[4] = 0, VoteFor[5] = 0, VoteFor[6] = 0, VoteFor[7] = 0
VoteFor[11] = 0

example3
VoteFor[0] = 1, VoteFor[1] = 2, VoteFor[2] = 1, VoteFor[3] = 0
VoteFor[4] = 0, VoteFor[5] = 0, VoteFor[6] = 0, VoteFor[7] = 0
VoteFor[11] = 3

what I need is function that returns player "winner's" id ( value in [] )
or
'-1' if there's no winner
so:
from example1 it should return '3' (Player(3) is a winner)
from example2 it should return '-1' (no winner)
from example3 it should return '11'
 

Ardenian

A

Ardenian

I don't know if you absolutly need vJass,
but in Jass you could loop through all values.

JASS:
function GetHighestVote takes nothing returns integer
local integer i
local integer highestvote

loop
exitwhen VoteFor[i] == null
    if VoteFor[i] > highestvote then
        set highestvote = i
    endif
    set i = i + 1
endloop

return highestvote

endfunction

Something like that
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
I'm sure you can do that with 2 nested loop and/or more if/then but i suppose this is enough :

JASS:
function Meh takes nothing returns integer
   local integer i = -1
   local integer max = 0
   local integer winner_id = -1

   // get the max

   loop
   set i = i+1
      if VoteFor[i] > max then
         set winner_id = i
         set max = VoteFor[i]
      endif
   exitwhen i==7
   endloop

  // trying to avoid a second loop

   if VoteFor[11] > max then
      return 11
   endif
   if VoteFor[11] == max then
      return -1
   endif
   

   set i = -1

   // now check if there is no egality
   loop
   set i = i+1
      if VoteFor[i] == max and winner_id != i then
         return -1
      endif
   exitwhen i==7
   endloop

   return winner_id

endfunction
If it's not enough "efficient" for you i can give you an other solution, just for the sake of it.
EDIT ; However, since the loop is pretty small, that doesn't worth it imho (the code will be more complex).

EDIT : The code was not complete, i forgot to set the max in the loop.

Oh well, just because i can :

JASS:
function Meh takes nothing returns integer
   local integer i = -1
   local integer max = 0
   local integer previous_max = 0
   local integer winner_id = -1

   // check if there is one vote at least

   loop
   set i = i+1
      if VoteFor[i] > 0 then
         set winner_id = i
         set max = VoteFor[i]
         loop
         exitwhen i==7
         set i = i+1
            // there is one vote, now we need to check if there is egality
            if VoteFor[i] == max then // for the moment we have an egality
               set previous_max=max
            elseif VoteFor[i] > max then // new winner
               set winner_id = i
               set max = VoteFor[i]
            endif
         endloop
      endif
   exitwhen i==7
   endloop

   if VoteFor[11] > max then
      return 11
   endif
   if VoteFor[11] == max or max == previous_max then
      return -1
   endif

   return winner_id

endfunction
 
Last edited:
I don't think you need two loops. Just do as Ardenian said (with a few modifications to fit his spec):
JASS:
function GetPlayerWithMostVotes takes nothing returns integer 
	/*
		max  -> keep track of highest number of votes
		best -> the index of the player with most votes (return value)
		i    -> used for looping
	*/
	local integer max  = -1
	local integer best = 0
	local integer i = 0

	loop 
		exitwhen i == 12 

		/*
			If this player has more votes than our
			current maximum, then update.

			If this player has votes equal to our
			current maximum, then we will set the
			"best" player index to -1.
		*/
		if VoteFor[i] > max then
			set max  = VoteFor[i]
			set best = i
		elseif VoteFor[i] == max then
			set best = -1
		endif

		set i = i + 1
	endloop

	return best
endfunction

It should work just fine. We can check for ties as we're looping, because if there is a tie that we care about, then the max will have already been stored.
 
Status
Not open for further replies.
Top