Creating a scrambled list
Posted: Tue Dec 10, 2013 4:05 am
I'm building a game where there is a quiz containing 10 questions. In order to make it more versatile. I choose the 10 questions from a total of 20 questions, randomly. This way when the user retakes the test the same questions may or may not on the quiz.
There are many other uses for scrambling numbers, etc. I think the most compelling reason is to make a game more interesting when its replayed, as the random values can be used to mix the game up a bit.
Issue: AdventureMaker doesn't have global arrays.
Solution: Actually, I came up with my own solution a few years ago by creating functions that work with delimited lists. So, instead of storing values in an array, I store them in a delimited list.
Ex.
aArray(0) = "This" aArray(1) = "Is" aArray(2) = "An" aArray(3) = "Array"
sDelimitedString = "This,Is,A,Delimited,String"
As long as you have functions that work with a delimited string, you can do everything with delimited strings that you can with arrays. The plus is that the string is a variant and can be used globally.
Now for fun:
Create a test frame and add the following to your advanced frame properties:
Before you run it, add the following to your procedures area.
NOTE: You should save a copy of these procedures for future use. I will show other tutorials on how to use them in other situations in the future.
There are many other uses for scrambling numbers, etc. I think the most compelling reason is to make a game more interesting when its replayed, as the random values can be used to mix the game up a bit.
Issue: AdventureMaker doesn't have global arrays.
Solution: Actually, I came up with my own solution a few years ago by creating functions that work with delimited lists. So, instead of storing values in an array, I store them in a delimited list.
Ex.
aArray(0) = "This" aArray(1) = "Is" aArray(2) = "An" aArray(3) = "Array"
sDelimitedString = "This,Is,A,Delimited,String"
As long as you have functions that work with a delimited string, you can do everything with delimited strings that you can with arrays. The plus is that the string is a variant and can be used globally.
Now for fun:
Create a test frame and add the following to your advanced frame properties:
- Code: Select all
cnt = 0
scrambledlist = ""
do Until cnt >= 10
val = randomInt(1,10)
If FindWord(val, scrambledlist, ",") < 0 Then
scrambledlist = scrambledlist & val & ","
cnt = cnt + 1
End If
Loop
scrambledlist = Left(scrambledlist, Len(scrambledlist)-1)
msgbox scrambledlist
Before you run it, add the following to your procedures area.
NOTE: You should save a copy of these procedures for future use. I will show other tutorials on how to use them in other situations in the future.
- Code: Select all
Function FindWord(word, text, delimiter)
sNeedle = word
FindWord = -1
aHaystack = Split(text, delimiter)
for x = 0 to UBound(aHayStack) - 1
If cint(aHayStack(x)) = cint(sNeedle) Then
FindWord = x
Exit For
End If
Next
End Function
- Code: Select all
Function RandomInt(lower,upper)
Randomize
RandomInt = int(rnd*(upper-lower+1))+lower
End Function 'RandomInt
- Code: Select all
Function GetWord(index,text,delimiter)
Dim aWords
If ltrim(text) = "" Then
GetWord = ""
Exit Function
End If
If index > 0 Then
index = index - 1
Else
GetWord = ""
Exit Function
End If
aWords = Split(text, delimiter)
If ubound(aWords) < index Then
GetWord = ""
Exit Function
End If
GetWord = ltrim(aWords(index))
End Function