This Code Shows you how to scramble words. This code is good for something like a Scrambler game. Here are a neat series of functions that will allow you to scramble each word in a sentence. This code would word well for a scrambler program. Put these functions Module:
Public Function RandomNumber(startNum As Integer, endNum As Integer) As Integer
Randomize
RandomNumber = Int(((endNum - startNum + 1) * Rnd) + startNum)
End Function
' swaps two characters in a string
Public Function swap(text As String, pos1 As Integer, pos2 As Integer)
Dim temp As String
temp = Mid(text, pos1, 1)
text = Mid(text, 1, pos1 - 1) + Mid(text, pos2, 1) + Mid(text, pos1 + 1)
text = Mid(text, 1, pos2 - 1) + temp + Mid(text, pos2 + 1)
swap = text
End Function
' scrambles a word
Public Function scrambleWord(ByVal text As String)
Dim scrambleStrength As Integer, pos1 As Integer, pos2 As Integer
Dim i As Integer
' probably doesn't need to be higher than this
scrambleStrength = (Len(text) - 1) * 2
For i = 0 To scrambleStrength
pos1 = RandomNumber(1, Len(text))
pos2 = RandomNumber(1, Len(text))
text = swap(text, pos1, pos2)
Next
scrambleWord = text
End Function
Public Function scrambleInput(text As String)
Dim words() As String, i As Integer
words = Split(text, " ") ' split is a VB function for breaking a string into an array of strings
For i = 0 To UBound(words)
' scramble each word
words(i) = scrambleWord(words(i))
Next
' output a string of the scrambled text
scrambleInput = Join(words, " ")
End Function
Example on how to use these functions to scramble the words in a sentence:
Text1 = scrambleInput(Text1)
0 comments:
Posting Komentar