none
How can I ignore if someone writes the wrong words? RRS feed

  • Question

  • Hi!

    I'm new to programming. And my first coding tool is SmallBasic. So i need some help...

    I'm making a text adeventure. Where i want the player to have a lot of space to write whatever they want. I don't want to give them options to write. So i have to write alot of lines that awnser what the player may type. But if they type something that i havent listed i just want it to stay blank. How can i do this?

    TextWindow.Title = "The Key"
    
    Start:
     
    TextWindow.WriteLine("Welcome! You wanna start?")
    
     
    x = TextWindow.Read() 
    
    Sc1:
    
    
    If x = "Yes" Or x = "yes" Or x = "y" or x = "Y" or x = "Ja" Or x = "ja" then
      TextWindow.Clear()
      TextWindow.WriteLine("You open your eyes. A dimed light is all you see. It's far away. What now?")
      Goto Sc2
      ElseIf(x = "No"or x = "no" or x = "n" or x = "N" or x = "Nei" or x = "nei") then
        TextWindow.WriteLine("Ok...")   
    
      Elseif(x = "Mabye" or x = "mabye")Then
        TextWindow.Clear()
        Goto start
      EndIf
    
    
    Sc2:
    
     
      
      x = TextWindow.Read() 
      
      If x = "Walk to it" Or x = "walk" Or x = "walk to it" Or x = "Walk" Or x = "run" Or x = "Run" Then
        TextWindow.Clear()
        TextWindow.WriteLine("As you come closer to the light, you feel yourself getting colder. A breeze     strokes over your left shoulder.")
      EndIf 
    
      

    Thx :)


    • Edited by Haaon Thursday, April 4, 2013 3:06 PM Wrong code
    Thursday, April 4, 2013 2:50 PM

Answers

  • I was assuming you meant that if you didn't understand the input you would wait for input that you did understand.

    So this code

    1] Sets the result of user input to the empty string ""

    2] Keeps checking until understood input is entered. This is the While ... EndWhile loop which keeps repeating the question until understood input is given and the variable result is set to something, when the loop ends and we have got some input that we understand.

    3] The bits inside the While ... EndWhile, just ask the question, get some input and test it for certain keywords, in this case run or walk.

    The keyword testing is done all in lower case to prevent having to check for Walk, walk, WALK etc.

    We also only check if the user input contains the word walk or run (in any case).

    When we get an input with the work run or walk in it we set the variable result to "walk" and therefore result is not = "" and the While loop ends and we have a variable called result with the instruction in it "walk".

    We could also test for other keywords, go back, wait, hide etc and store a different result string for these possible actions.

    Try running and modifying the code to see how it works or ask other questions about bits of it you are not sure about.

    • Marked as answer by Haaon Thursday, April 4, 2013 8:42 PM
    Thursday, April 4, 2013 7:16 PM
    Moderator

All replies

  • Analyse (or parse) a human written sentence is too advanced.

    Rather than give a player free rein to type in anything, you should restrict & display the actions which can be performed.

    In some instances, ask for further nouns which a verb acts upon.

    Players can get tired of having to type in full words. So, just ask them to hit 1 key only, which represents the action (or verb) to perform!

    To do that, use TextWindow.ReadKey() instead of regular TextWindow.Read()!  ^_^


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Thursday, April 4, 2013 6:14 PM
    Answerer
  • Perhaps something like this:

    result = ""
    While (result = "")
      TextWindow.WriteLine("You open your eyes. A dimed light is all you see. It's far away. What now?")
      input = Text.ConvertToLowerCase(TextWindow.Read()) 'Lower case for easy comparison
      If (Text.IsSubText(input,"walk") Or Text.IsSubText(input,"run") ) Then 'just check for some keywords
        result = "walk" 'Use a special result that we can process easilty later
      EndIf
    EndWhile
    
    TextWindow.WriteLine("The user wants to "+result)

    Try not to use GoTo if you can and perhaps also consider using subroutines later - good luck and this is a great start.

    Thursday, April 4, 2013 6:19 PM
    Moderator
  • Perhaps something like this:

    result = ""
    While (result = "")
      TextWindow.WriteLine("You open your eyes. A dimed light is all you see. It's far away. What now?")
      input = Text.ConvertToLowerCase(TextWindow.Read()) 'Lower case for easy comparison
      If (Text.IsSubText(input,"walk") Or Text.IsSubText(input,"run") ) Then 'just check for some keywords
        result = "walk" 'Use a special result that we can process easilty later
      EndIf
    EndWhile
    
    TextWindow.WriteLine("The user wants to "+result)

    Try not to use GoTo if you can and perhaps also consider using subroutines later - good luck and this is a great start.

    I'm sorry, but i didn't understand :(
    Thursday, April 4, 2013 7:06 PM
  • I was assuming you meant that if you didn't understand the input you would wait for input that you did understand.

    So this code

    1] Sets the result of user input to the empty string ""

    2] Keeps checking until understood input is entered. This is the While ... EndWhile loop which keeps repeating the question until understood input is given and the variable result is set to something, when the loop ends and we have got some input that we understand.

    3] The bits inside the While ... EndWhile, just ask the question, get some input and test it for certain keywords, in this case run or walk.

    The keyword testing is done all in lower case to prevent having to check for Walk, walk, WALK etc.

    We also only check if the user input contains the word walk or run (in any case).

    When we get an input with the work run or walk in it we set the variable result to "walk" and therefore result is not = "" and the While loop ends and we have a variable called result with the instruction in it "walk".

    We could also test for other keywords, go back, wait, hide etc and store a different result string for these possible actions.

    Try running and modifying the code to see how it works or ask other questions about bits of it you are not sure about.

    • Marked as answer by Haaon Thursday, April 4, 2013 8:42 PM
    Thursday, April 4, 2013 7:16 PM
    Moderator
  • I was assuming you meant that if you didn't understand the input you would wait for input that you did understand.

    So this code

    1] Sets the result of user input to the empty string ""

    2] Keeps checking until understood input is entered. This is the While ... EndWhile loop which keeps repeating the question until understood input is given and the variable result is set to something, when the loop ends and we have got some input that we understand.

    3] The bits inside the While ... EndWhile, just ask the question, get some input and test it for certain keywords, in this case run or walk.

    The keyword testing is done all in lower case to prevent having to check for Walk, walk, WALK etc.

    We also only check if the user input contains the word walk or run (in any case).

    When we get an input with the work run or walk in it we set the variable result to "walk" and therefore result is not = "" and the While loop ends and we have a variable called result with the instruction in it "walk".

    We could also test for other keywords, go back, wait, hide etc and store a different result string for these possible actions.

    Try running and modifying the code to see how it works or ask other questions about bits of it you are not sure about.

    I understood what you were talking about! Thank you so much!

    Thursday, April 4, 2013 8:42 PM
  • Hi Haaon

    Using your program, to leave the screen unchanged/blank and still read the users input, you can add TextWindow.Write("") on the line above x = TextWindow.Read().

    Sc2:
    TextWindow.Write("") 'added to leave the screen blank and still add data. <<<<<<<<<<<<<<<<
    x = TextWindow.Read() 
    If x = "Walk to it" Or x = "walk" Or x = "walk to it" Or x = "Walk" Or x = "run" Or 

    You're off to a flying start.

    Also I recommend, if you don't already, work through the Curriculum that's available, http://sdrv.ms/YX494K while you're writing your program. It's very helpful and will give you some great ideas for your programs. And try some of the Monthly challenges. Small Basic and its curriculum is a great place to start a journey on learning to program.

    Happy coding.

    Friday, April 5, 2013 2:50 AM
    Moderator