Multiple variable comparisons in one "if" command


#1

I have a situation where I want to randomly distribute the values 1 through 15 to fifteen variables with no repetition. Sort of like shuffling a 15-card deck. In order to do that now, I need a monster block of code that goes something like this:

*rand var1 1 15
*label randvar2
*rand var2 1 15
*if {var2 = var1}
    *goto randvar2
*else
    *goto randvar3
*label randvar3
*rand var3 1 15
*if {var3 = var1} or {var3 = var2}
    *goto randvar3
*else
    *goto randvar4
*label randvar4
*rand var4 1 15
*if {var4 = var1} or {var4 = var2}
    *goto randvar4
*elseif {var4 = var3}
    *goto randvar4
*else
    *goto randvar5
*label randvar5
*rand var5 1 15
*if {var5 = var1} or {var5 = var2}
    *goto randvar5
*elseif {var5 = var3} or {var5 = var4}
    *goto randvar5
*else
    *goto randvar6

Etc. Etc. Etc.

Each subsequent variable requires more and more lines to successfully randomize. Is there an easier way to do this that I’m missing? If not, can the next release support more than two comparisons within one “if” block? That would make this much easier.


#2

I don’t know ChoiceScript Language so don’t know what is available; looks awfully like assembly language (yuk). Anyways, here is an idea:

*temp var1 1
*temp var2 2
*temp var3 3
*temp var4 4

*temp random 0
*rand random 1 4

*temp chosenRand {"var"&random}
${chosenRand}

I did a quick read, because it’s very odd for any language not allowing more than two conditions for if statement. You are CAN use more than two just gotta use brackets. Here is the example of complex condition in the documentation:

((leadership > 60) and (agility > 20)) or (strength > 80)

Last options is to use *script command to use JavaScript if you know that.

They also just added array in the language (kind of lists, though current implementation is weird for programmers who are used to other languages), so soon you’ll be able to make use of them. That could save you in situations like this.


#3

Haven’t tested this, but something like this is how I would do it:

*label randomassignment
*comment this line skips the assignment if variable1 is not 0, I.E, we already assigned it something
*if not(variable1 = 0)
  *goto random2
*set assigning variable1
*rand dieroll 1 15
*set returnplace
*gotoref dieroll
*label random2
*comment duplicate everything from above here, 15 times... Make sure to change random1 to random2, etc.

*comment *gotoref dieroll will go here if dieroll = 1
*label 1
*comment if we already did number 1, skip
*if taken1
  *comment goes back to start.
  *goto randomassignment
*comment if 'assigning' is 'random1' this sets 'random1' to [random value 1]
*setref assigning [random value 1]
*set taken1 true
*comment goes back to start.
*goto randomassignment
*comment copy all that 15 times and change random1 to random2, random3, etc.

Not sure if this is understandable or even works, but it avoids recursive if explosions at least.

Make sure to declare all your temp variables.


#4

You can use parentheses to do more than one comparison on a single line.

*if (var4 = var1) or ((var4 = var2) or (var4 = var3))

As for the problem as a whole…

I think I would handle this with arrays, despite @dfabulich’s warning that they’re experimental right now. Something like this:

*create var_1 1
*create var_2 2
*create var_3 3
*create var_4 4
*create var_5 5
(...to 15)
*create choosea
*create chooseb
*create swapa
*create swapb
*create iterations 20

*label swap
*rand choosefirst 1 15
*rand choosesecond 1 15

*if (choosefirst != choosesecond)
    *set swapa[{choosefirst}]
    *set swapb[{choosesecond}]
    *set var[{choosesecond}]swapa
    *set var[{choosesecond}]swapb

*set iterations %- 1

*if (iterations != 0)
    *goto swap

(Disclaimer: I didn’t actually verify this code block.)

This system will pick pairs of numbers and swap their positions for as many iterations as you want. With enough iterations, that should scramble your order decently, and then you can pull them out of the array into whatever else you want them for.