[JASS] Randomize Player Colors

Status
Not open for further replies.
Level 2
Joined
Dec 1, 2008
Messages
7
I tried to make a function that randomizes player colors, however, it does not seems to work properly as sometimes two players are still assigned the same color and I cannot find the flaw.

It is supposed to work for the first 4 Players (0-3) and only picking from 8 of the colors.

JASS:
function RandomizePlayerColors takes nothing returns nothing
    local playercolor array pcolors
    local integer i = 0
    local integer j = 0
    local integer rnd

    // assign colors to array
    set pcolors[0] = PLAYER_COLOR_RED
    set pcolors[1] = PLAYER_COLOR_BLUE
    set pcolors[2] = PLAYER_COLOR_CYAN
    set pcolors[3] = PLAYER_COLOR_PURPLE
    set pcolors[4] = PLAYER_COLOR_YELLOW
    set pcolors[5] = PLAYER_COLOR_ORANGE
    set pcolors[6] = PLAYER_COLOR_GREEN
    set pcolors[7] = PLAYER_COLOR_PINK
    

    loop
        if (GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING) then
            set rnd = GetRandomInt(0,7) 
            loop 
             // loop through players 0-3 to check for rolled color
             // skip to else if j==i (no check if player to be randomized already has that color)
                if (pcolors[rnd] == GetPlayerColor(Player(j)) and not(j==i) ) then
                    set rnd = GetRandomInt(0,7)
                    set j = 0 
                else
                    set j = j +1 // if color not taken by player increment to next player
                endif
            exitwhen (j >= i) // do not check color of players that have not yet been randomized
            endloop 
            
            call SetPlayerColorBJ(Player(i), pcolors[rnd] , true)        
        else 
            call SetPlayerColorBJ(Player(i), PLAYER_COLOR_LIGHT_GRAY , true)
            // if not playing set player color to gray 
        endif
        
        set i = i+1 
        exitwhen i == 4  // increment index, for next player, stop if player five
    endloop


endfunction
 
Level 4
Joined
Apr 16, 2009
Messages
85
this should work:

JASS:
function RandomizePlayerColors takes nothing returns nothing
    local playercolor array pcolors
    local integer i = 0
    local playercolor temp
    local integer rnd

    // assign colors to array
    set pcolors[0] = PLAYER_COLOR_RED
    set pcolors[1] = PLAYER_COLOR_BLUE
    set pcolors[2] = PLAYER_COLOR_CYAN
    set pcolors[3] = PLAYER_COLOR_PURPLE
    set pcolors[4] = PLAYER_COLOR_YELLOW
    set pcolors[5] = PLAYER_COLOR_ORANGE
    set pcolors[6] = PLAYER_COLOR_GREEN
    set pcolors[7] = PLAYER_COLOR_PINK


    loop
        exitwhen i == 4
        set rnd = GetRandomInt(i,7)
        set temp = pcolors[i]
        set pcolors[i] = pcolors[rnd]
        set pcolors[rnd] = temp
        if (GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING) then
            call SetPlayerColorBJ(Player(i), pcolors[i] , true)
        else
            call SetPlayerColorBJ(Player(i), PLAYER_COLOR_LIGHT_GRAY , true)
            // if not playing set player color to gray
        endif

        set i = i+1
    endloop


endfunction
 
Status
Not open for further replies.
Top