Basic Word Jumber

dazza666

New member
Joined
Aug 29, 2008
Messages
2
Programming Experience
Beginner
Hi,
I'm very new to programming and i'm trying to create a word jumbler. The idea is someone types something in to a text box and a label displays the letters in a random order. I'm trying to use an array to ensure the same value doesn't appear twice can someone tell me where i'm going wrong?

VB.NET:
[COLOR="Blue"]Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim rndNumber As Integer
        Dim arrRepeat(TextBox1.Text.Length) As Integer

        Dim store, txtCount, i As Integer



        Label2.Text = " "

      

        For txtCount = 1 To TextBox1.Text.Length

start:      Randomize()


            rndNumber = Int((Rnd()) * TextBox1.Text.Length)


            For store = 0 To TextBox1.Text.Length - 1

                If rndNumber = arrRepeat(store) Then

                    GoTo start


                End If


                arrRepeat(txtCount) = rndNumber
            Next store



        Next txtCount

        For i = 0 To TextBox1.Text.Length - 1



            Label2.Text = Label2.Text & TextBox1.Text.Chars((arrRepeat(i) + 1))
        Next






    End Sub[/COLOR]
 
I'm hoping this isn't for a class but it does look like you've tried to research this (a little).

VB.NET:
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

        Dim WordArray() As String = Split(TextBox1.Text, " ")
        Dim Word() As Char
        Dim LetterPlaceholder As Char
        Dim WordCount As Integer
        Dim i As Integer
        Dim Position As Integer

        Dim sb As New StringBuilder()

        For WordCount = 0 To WordArray.Length - 1 'Cycle through array of strings

            If WordCount > 0 AndAlso WordCount <= WordArray.Length - 1 Then
                sb.Append(" ") 'Add space between word
            End If

            Word = WordArray(WordCount)

            For i = 0 To Word.Length - 1 'Cycle through each letter in a word
                Position = (Word.Length - 1) * Rnd() 'Swap with random position in string
                LetterPlaceholder = Word(Position) 'Store char to swap so it doesn't get lost
                Word(Position) = Word(i) 'Set current letter to with letter to swap
                Word(i) = LetterPlaceholder 'Set current letter with swapped letter
            Next

            sb.Append(Word) ' Add swapped word to string builder
        Next

        TextBox2.Text = sb.ToString()

    End Sub
End Class
 
another suggestion...
VB.NET:
Private Sub JumbleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JumbleButton.Click
    Me.ResultLabel.Text = RandomizeWord(Me.InputTextBox.Text)
End Sub

Private r As New Random
Private Function RandomizeWord(ByVal word As String) As String
    Dim newword As String = String.Empty
    For Each c As Char In word
        newword = newword.Insert(r.Next(0, newword.Length + 1), c)
    Next
    Return newword
End Function
 
Back
Top