none
Trying to split up a word into individual letters as variables for a name game. RRS feed

  • Question

  • Hello,

    I'm new to Small Basic and am attempting my first project other than the given samples.

    I've got an idea for a name game that takes a person's name and then displays a picture from Flickr or by drawing each letter of their name.  The problem is, is that I'm not sure what to do to break the name apart.

    Say for example:

    The user starts the program and it asks for their name.  The user types in "John".  The program then counts how many letters are in the name for future reference, and then breaks up the name into its individual letters, such as "J", "o", "h", and "n".

    Using If/Then/and Else statements, along with Goto's and labels, depending on what letters were typed in, will determine the final results outputted to the user.

    I understand how to do everything but the actual break up of the name into letters and the counting of the letters (needed if I decide to use turtle graphics to draw the letters out - since I will need to know how much space is needed for graphics window).

    I'm not going to ask anybody to write out the program for me.  That would be ridiculous since I wouldn't learn anything.  But perhaps someone could point me in the right direction.  I've seen a list of available commands, and I'm not certain which would apply for this particular situation.  I also understand that this language is somewhat limited in functionality, and that there's no quick simple command to do all that I ask.

    Here is what I have so far.  It's not pretty and far from complete, but as you can see, I'm missing the main functionality.

    TextWindow.Write("Please type in your name: ")
    name = TextWindow.Read()
    
    Turtle.Show()
    Turtle.X = 50
    Turtle.Speed = 9
    Turtle.Turn(90)
    
    Goto CapA
    
    '// Letter "A"
    CapA:
    Turtle.PenUp()
    Turtle.Move(25)
    Turtle.Turn(-90)
    Turtle.PenDown()
    Turtle.Move(50)
    Turtle.Turn(90)
    Turtle.Move(50)
    Turtle.Turn(-90)
    Turtle.Move(25)
    Turtle.Turn(-45)
    Turtle.Move(35)
    Turtle.Turn(-90)
    Turtle.Move(35)
    Turtle.Turn(-45)
    Turtle.Move(25)
    Turtle.Turn(-90)
    Turtle.PenUp()
    Turtle.Move(50)
    Turtle.Turn(90)
    Turtle.PenDown()
    Turtle.Move(50)
    Turtle.Turn(-90)
    Turtle.PenUp()
    Turtle.Move(25)

    Tuesday, June 22, 2010 4:18 PM

Answers

  • Using Goto to jump in and out of loops is a bad plan really.  When the code does the first letter it jumps out the For loop, does the letter, then jumps to Word, which then starts the For loop again from the beginning - Try using subroutines rather than GoTo if you can:

    TextWindow.Title = "Name Game"

    TextWindow.Write("Please type in your name: ")
    name = TextWindow.Read()

    For i = 1 To Text.GetLength(name)
      letter[i] = Text.GetSubText(name,i,1)
    EndFor

    Turtle.Show()
    Turtle.X = 50
    Turtle.Y = 150
    Turtle.Speed = 9
    Turtle.Turn(90)
    GraphicsWindow.Height = 200
    GraphicsWindow.Width = (100 + ((Text.GetLength(name)) * 100))

    For i = 1 To Array.GetItemCount(letter)
      If letter[i] = "A" Then
        CapA()
      ElseIf letter[i] = "B" Then
        CapB()
      Else
        'Goto Exit
      EndIf
    EndFor

    '// Capital Letter "A"
    Sub CapA
      Turtle.PenUp()
    ...
    EndSub

    '// Capital Letter "B"
    Sub CapB
      Turtle.PenUp()
    ...
    EndSub

    • Marked as answer by CircleTilde Tuesday, June 22, 2010 8:36 PM
    Tuesday, June 22, 2010 8:26 PM
    Moderator

All replies

  • Look at the Text methods:

    For example here I turn the text in name into an array of letters.

    name = "TestName"

    For i = 1 To Text.GetLength(name)
      nameArray[i] = Text.GetSubText(name,i,1)
    EndFor

    For i = 1 To Array.GetItemCount(nameArray)
      TextWindow.WriteLine(nameArray[i])
    EndFor


    EDIT

    The unicode value for a character may also be useful for random type pictures e.g.

    For i = 1 To Array.GetItemCount(nameArray)
      unicode = Text.GetCharacterCode(nameArray[i])
      TextWindow.WriteLine(nameArray[i]+" "+unicode)
      Turtle.Move(unicode)
      Turtle.Turn(unicode)
    EndFor

    A lot of work for all letters as you are doing it - I would do a couple letters, then possibly think of a way to store the data required for each letter in a databse file or array to use.

     

    Tuesday, June 22, 2010 5:59 PM
    Moderator
  • Thanks for the reply, litdev.

    After I posted this question, I ended up stumbling across another post that had a similar setup of what I wanted.  Thunder708 has a program under the "Twenty Five Line Challenge" thread import code: JHK469-0

    I started off with referencing what he had put into play to see if I could get it to work in my project.  I also took what you stated and modified my code.

    So far it's closer to where I was before, but I'm still stuck.  I understand what is happening, but I'm just not certain where to go from here.  There are either a couple of things happening that i can possibly think.  Either the loop is storing the data of the first letter in the name, or it's not even using the other letters of the name.

    So far, I've only mapped out the capital letters A and B.  Nothing else.  I tested out combinations of the two and for whatever reason, it's only picking the first letter and repeating it.  Im thinking it is storing the data for the variable, instead of moving on to the next variable.

    I have it on a loop, so this would be the reason why it is repeating, but I thought that it would pull data from the next letter in the name given to it.

    So if I typed "BAB", all I get is an infinite BBBBBBBBBBBB...  I set it up that if you type anything other than A or B, it quits.  My code is posted below.

     

    TextWindow.Title = "Name Game"
    
    TextWindow.Write("Please type in your name: ")
    name = TextWindow.Read()
    
    For i = 1 To Text.GetLength(name)
     letter[i] = Text.GetSubText(name,i,1)
    EndFor
    
    Turtle.Show()
    Turtle.X = 50
    Turtle.Y = 150
    Turtle.Speed = 9
    Turtle.Turn(90)
    GraphicsWindow.Height = 200
    GraphicsWindow.Width = (100 + ((Text.GetLength(name)) * 100))
    
    Word:
    For i = 1 To Array.GetItemCount(letter)
     If letter[i] = "A" Then
      Goto CapA
     ElseIf letter[i] = "B" Then
      Goto CapB
     Else
      Goto Exit
     EndIf
    EndFor
    
    '// Capital Letter "A"
    CapA:
    Turtle.PenUp()
    Turtle.Move(25)
    Turtle.Turn(-90)
    Turtle.PenDown()
    Turtle.Move(50)
    Turtle.Turn(90)
    Turtle.Move(50)
    Turtle.Turn(-90)
    Turtle.Move(25)
    Turtle.Turn(-45)
    Turtle.Move(35)
    Turtle.Turn(-90)
    Turtle.Move(35)
    Turtle.Turn(-45)
    Turtle.Move(25)
    Turtle.Turn(-90)
    Turtle.PenUp()
    Turtle.Move(50)
    Turtle.Turn(90)
    Turtle.PenDown()
    Turtle.Move(50)
    Turtle.Turn(-90)
    Turtle.PenUp()
    Turtle.Move(25)
    Goto Word
    
    '// Capital Letter "B"
    CapB:
    Turtle.PenUp()
    Turtle.Move(25)
    Turtle.Turn(-90)
    Turtle.PenDown()
    Turtle.Move(100)
    Turtle.Turn(90)
    Turtle.Move(25)
    Turtle.Turn(45)
    Turtle.Move(25)
    Turtle.Turn(45)
    Turtle.Move(25)
    Turtle.Turn(45)
    Turtle.Move(12)
    Turtle.Turn(45)
    Turtle.Move(30)
    Turtle.Turn(180)
    Turtle.PenUp()
    Turtle.Move(30)
    Turtle.Turn(45)
    Turtle.PenDown()
    Turtle.Move(12)
    Turtle.Turn(45)
    Turtle.Move(25)
    Turtle.Turn(45)
    Turtle.Move(25)
    Turtle.Turn(45)
    Turtle.Move(25)
    Turtle.Turn(180)
    Turtle.PenUp()
    Turtle.Move(75)
    Goto Word
    
    exit:
    Program.End()

     

     

    Tuesday, June 22, 2010 8:09 PM
  • Using Goto to jump in and out of loops is a bad plan really.  When the code does the first letter it jumps out the For loop, does the letter, then jumps to Word, which then starts the For loop again from the beginning - Try using subroutines rather than GoTo if you can:

    TextWindow.Title = "Name Game"

    TextWindow.Write("Please type in your name: ")
    name = TextWindow.Read()

    For i = 1 To Text.GetLength(name)
      letter[i] = Text.GetSubText(name,i,1)
    EndFor

    Turtle.Show()
    Turtle.X = 50
    Turtle.Y = 150
    Turtle.Speed = 9
    Turtle.Turn(90)
    GraphicsWindow.Height = 200
    GraphicsWindow.Width = (100 + ((Text.GetLength(name)) * 100))

    For i = 1 To Array.GetItemCount(letter)
      If letter[i] = "A" Then
        CapA()
      ElseIf letter[i] = "B" Then
        CapB()
      Else
        'Goto Exit
      EndIf
    EndFor

    '// Capital Letter "A"
    Sub CapA
      Turtle.PenUp()
    ...
    EndSub

    '// Capital Letter "B"
    Sub CapB
      Turtle.PenUp()
    ...
    EndSub

    • Marked as answer by CircleTilde Tuesday, June 22, 2010 8:36 PM
    Tuesday, June 22, 2010 8:26 PM
    Moderator
  • That did it!

     

    Thanks for your help!  I've seen the subroutines in the mix, but wasn't sure of their purpose or how to use them.  I'm certain that I can get the rest from here.  Thank you very kindly!

    Tuesday, June 22, 2010 8:37 PM