Randomized Number Help

TheTempest

New Member
Reaction score
6
Randomized Number Help [FIXED]

Hey guys. In my map there are 20 islands and 12 players. I want to give each player an island, but randomize it so it's different each time (keeps things fresh).

The problem I'm running into is that 2 players can't have the same island.

I need some sort of way to get this right. The only thing I thought which MIGHT work is just looping until it finds an island that isn't taken up. Of course, this is really inefficient.

If you can do it in GUI I would prefer it (I code in a lot of languages but I don't know JASS. How ironic :rolleyes:)

Thanks
 

Manix

Well-Known Member
Reaction score
29
You didn't mention how you're "giving" the player an island, but let's just say you're spawning a unit there, you should create a dummy unit, or if you already have one create another one called Island Dummy and create one per island (via world editor) then in your trigger where you're giving the islands set the actions up like that:

set tempgroup = units in (entire map) matching (matching unit) equal to Island Dummy
for each integer from 1 to 12 do
loop
set tempunit = random unit from temgroup
remove tempunit from tempgroup
set temppoint = position of tempunit
create 1 peasant for player(integer a) at temppoint
custom script: call RemoveLocation(udg_temppoint)
>outside the loop
custom script: call DestroyGroup(udg_tempgroup)
 

TheTempest

New Member
Reaction score
6
You didn't mention how you're "giving" the player an island, but let's just say you're spawning a unit there, you should create a dummy unit, or if you already have one create another one called Island Dummy and create one per island (via world editor) then in your trigger where you're giving the islands set the actions up like that:

set tempgroup = units in (entire map) matching (matching unit) equal to Island Dummy
for each integer from 1 to 12 do
loop
set tempunit = random unit from temgroup
remove tempunit from tempgroup
set temppoint = position of tempunit
create 1 peasant for player(integer a) at temppoint
custom script: call RemoveLocation(udg_temppoint)
>outside the loop
custom script: call DestroyGroup(udg_tempgroup)

Maybe I wasn't very clear.

I have an int variable PlayerIsles[12] and each element holds the Island # (1-20)

Problem I'm having is that if Player 1 is assigned 3 so PlayerIsles[1] = 3 and Player 2 is assigned 3 then PlayerIsles[2] = 3.

Once the randomizing is finished each player gets a builder at that location so obviouslly you can't have 2 players with the same location.

If I knew JASS i could just do While(tRandNum != PlayerIsles[1] && tRandNum != PlayerIsles[2] etc.)
 

Manix

Well-Known Member
Reaction score
29
That's exactly what I told you to do, you have unit groups here which makes it easier for you to code stuff, you don't need to use integers like in other languages, just look again at my previous post
 

TheTempest

New Member
Reaction score
6
That's exactly what I told you to do, you have unit groups here which makes it easier for you to code stuff, you don't need to use integers like in other languages, just look again at my previous post

I'd prefer to use integers, they take up less memory than creating dummy units.
 

Manix

Well-Known Member
Reaction score
29
o_O well if creating dummies is a problem for you just remove them from the game, which is a total removal of the unit, after you've used them for their purpose..

If you wish to use integers so badly, then simply remove the given isle from the isles variable, example

Isles[1-20] // are your islands
PlayerIsles[1-12] // are the isles that are supposed to be given
IslesCount // is the count of current isles available (Not an Array)
Isle // Integer Variable (Not an Array)

For each integer B from 1 to 12 do
loop
Set Isle = Random number between 1 and IsleCount // This sets the random isle, let's say it's 5
Set PlayerIsles[Integer B] = Isle // Now you need to set the IslesCount to -1
Set IslesCount = IslesCount -1 // Done
For each integer A from Isle to IslesCount // you need to loop through all isles and downgrade them by 1
loop
Set Isles[Integer A] = Isles[Integer A + 1] // now Isle 5 (which I chose to be the random number) becomes Isle 6 and so on, isle 20 becomes isle 19 and the max number of isles becomes 19

I hope you get the idea
 

TheTempest

New Member
Reaction score
6
o_O well if creating dummies is a problem for you just remove them from the game, which is a total removal of the unit, after you've used them for their purpose..

If you wish to use integers so badly, then simply remove the given isle from the isles variable, example

Isles[1-20] // are your islands
PlayerIsles[1-12] // are the isles that are supposed to be given
IslesCount // is the count of current isles available (Not an Array)
Isle // Integer Variable (Not an Array)

Set Isle = Random number between 1 and IsleCount // This sets the random isle, let's say it's 5
Set PlayerIsles[1] = Isle // Now you need to set the IslesCount to -1
Set IslesCount = IslesCount -1 // Done
For each integer A from Isle to IslesCount // you need to loop through all isles and downgrade them by 1
loop
Set Isles[Integer A] = Isles[Integer A + 1] // now Isle 5 (which I chose to be the random number) becomes Isle 6 and so on, isle 20 becomes isle 19 and the max number of isles becomes 19

I hope you get the idea

I downloaded the NewGen pack and JASS is retarded easy. Thanks for trying, Manix. I'll report back when I get some working code (Writing it in JASS is going to be so much easier than GUI when I can use exitwhen)
 

Manix

Well-Known Member
Reaction score
29
Alrightie, what I posted above is the system, if I can call it that way, to pick random stuff by integers, I highly believe it's based off the same idea in jass aswell ;)
 

TheTempest

New Member
Reaction score
6
Fuck JASS syntax.

JASS:
if (iRand == udg_PlayerIsles[a])
then 
set IsTaken = true 
endif


You have to put udg_ before any global variable, correct?

PlayerIsles is an integer array, so I call it using usg_PlayerIsles[] ?
 

Manix

Well-Known Member
Reaction score
29
whether it is an array or not it's still udg_VarName which stands for UserDefinedGlobal, in jass variables are of two types, globals and locals but for more info you should refer to the jass section
 

TheTempest

New Member
Reaction score
6
whether it is an array or not it's still udg_VarName which stands for UserDefinedGlobal, in jass variables are of two types, globals and locals but for more info you should refer to the jass section

I know that. That's the code I have and it's saying that udg_PlayerIsles is an undefined variable (no it isn't)
 

TheTempest

New Member
Reaction score
6
What is this udg_PlayerIsles[a]

JASS:
function GenerateIslandNumber takes integer iPlayerNum returns integer

local integer iRand = GetRandomInt(1, 20)
local integer a = 0
local boolean IsTaken = false

    loop
        set a = a + 1
        
        // If the random number matches one of the islands that have already
        // been taken then set istaken to true
        if (iRand == udg_PlayerIsles[a])
        then 
        set IsTaken = true 
        endif
        
        // After we have looped through all the players (a > iPlayerNum)
        // and there hasn't been a match then exit the loop and return iRand
        exitwhen a > iPlayerNum and IsTaken == false
        
        // This is only called if the exitwhen condition was false
        if(a > iPlayerNum) then

        // Reset our loop variable and generate a new number
        set a = 0
        set iRand = GetRandomInt(1, 20)

        endif
    endloop

return iRand

endfunction


"udg_PlayerIsles undefined variable."

In my variable editor: "PlayerIsles : Integer : Array[12]"
 

TheTempest

New Member
Reaction score
6
What is this udg_PlayerIsles[a]

JASS:
function GenerateIslandNumber takes integer iPlayerNum returns integer

local integer iRand = GetRandomInt(1, 20)
local integer a = 0
local boolean IsTaken = false

    loop
        set a = a + 1
        
        // If the random number matches one of the islands that have already
        // been taken then set istaken to true
        if (iRand == udg_PlayerIsles[a])
        then 
        set IsTaken = true 
        endif
        
        // After we have looped through all the players (a > iPlayerNum)
        // and there hasn't been a match then exit the loop and return iRand
        exitwhen a > iPlayerNum and IsTaken == false
        
        // This is only called if the exitwhen condition was false
        if(a > iPlayerNum) then

        // Reset our loop variable and generate a new number
        set a = 0
        set iRand = GetRandomInt(1, 20)

        endif
    endloop

return iRand

endfunction


"udg_PlayerIsles undefined variable."

In my variable editor: "PlayerIsles : Integer : Array[12]"

EDIT: Sorry for the double post.
 

Summoned

New Member
Reaction score
51
Try setting the Array Size to 1 in the object editor. The Size stands for something very different than what you'd expect Array Size to mean.
 

Manix

Well-Known Member
Reaction score
29
Well Idk, You could've done what I suggested, it works I've used that system for an AoS -ar command
 

Manix

Well-Known Member
Reaction score
29
Well try stuff out, try changing it, changing the array capacity, as already suggested, try creating a different variable and using it, try setting it in the same function see what does what

EDIT: I'm goin to bed nao, I wish you good luck on fixing your code, will respond back tomorrow if it's still an issue
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top