locked
Need help with my small basic hangman code RRS feed

  • Question

  • Hi guys,

    I have recently been developing a hangman game using small basic and it is almost finished however I am stuck on one last thing.

    I can't work out how to prevent already guessed letters from being guessed again. At the moment, the user can just enter the same wrong letter 12 times and lose.

    Here is my code:

         

    GraphicsWindow.Width = 300
    GraphicsWindow.Height = 300
    Back = ImageList.LoadImage("http://www.horizonservicesinc.com/wp/wp-content/uploads/Explosion.jpg")
    GraphicsWindow.DrawResizedImage(Back,0,0,300,300)

    NewGame = Controls.AddButton("New Game", 110, 120)
    Controls.SetSize(NewGame, 80, 40)
    ChangeWords = Controls.AddButton("Change Words", 100, 200)
    Controls.SetSize(ChangeWords, 100, 40)

    Controls.ButtonClicked = LastButtonClicked

    Sub LastButtonClicked
      ButtonClicked = Controls.LastClickedButton
      If (NewGame = ButtonClicked) Then
        GraphicsWindow.Clear()
        GraphicsWindow.Width = 1000
        GraphicsWindow.Height = 500

     'Get the word, read a random line from a file'   
    SetWord = File.ReadLine(Program.Directory + "\words.txt", Math.GetRandomNumber(10))
    WordLength = Text.GetLength(SetWord) 'Find the length of the word'
    GraphicsWindow.FontSize = 40  



    For i = 1 To WordLength
      GraphicsWindow.PenColor = "Green"
      GraphicsWindow.PenWidth = 5
      Shapes.AddLine(50 + i*40, 300, 50 + i*40 + 30, 300) 'Displays the dashes'
    EndFor 


    GraphicsWindow.KeyDown = OnKeyDown  'calls the subprocedure OnKeyDown, Checks for what key is pressed'
    EndIf
     If (ChangeWords = ButtonClicked) Then
       TextWindow.WriteLine("Please enter 10 new words, no capitals!")
       For i = 1 To 10
         TextWindow.Write(i + ". ")
         Words[i] = TextWindow.Read()
         File.WriteLine(Program.Directory + "\words.txt", i, Words[i])
       EndFor
       TextWindow.WriteLine("Your words have been saved.")   
       TextWindow.Pause()
       Program.End()
    Endif

    EndSub
    Sub OnKeyDown
      Convert = Text.ConvertToUpperCase (SetWord)
      Found = 0
      For i = 1 To Text.GetLength(convert)
        If GraphicsWindow.LastKey = Text.GetSubText(convert,i, 1) Then
          GraphicsWindow.DrawText (50 + i*40, 250, Text.GetSubText(convert,i, 1))
          Found = 1
       EndIf
     EndFor

    DrawWrong = GraphicsWindow.LastKey 
    If (Found = 0) Then
       IncorrectLetter = IncorrectLetter + 1
       GraphicsWindow.DrawText(50*IncorrectLetter, 150, DrawWrong )
       If (IncorrectLetter = 12) Then 
         GraphicsWindow.ShowMessage("The word was " + SetWord, "You Lose")
       Else
         seg = IncorrectLetter 
         DrawMan()
       EndIf 
     EndIf 
    EndSub 



    Sub DrawMan   ' This subprocedure draws the hangman according to the number of letters missed'
     If (seg = 1) Then 
    GraphicsWindow.PenColor = "brown"
    GraphicsWindow.DrawLine(640,400,860,400) 'Bottom Line'
    ElseIf (seg = 2) Then
    GraphicsWindow.DrawLine(700,400,700,100) 'Upwards Line'
    ElseIf (seg = 3) Then
    GraphicsWindow.DrawLine(700,100,820,100) 'Top Line'
    ElseIf (seg = 4) Then
    GraphicsWindow.DrawLine(700,160,760,100) 'Top Support'
    ElseIf (seg = 5) Then
    GraphicsWindow.DrawLine(700,340,760,400) 'Bottom Support
    ElseIf (seg = 6) Then
    GraphicsWindow.DrawLine(820,100,820,140) 'Rope'
    ElseIf (seg = 7) Then
    GraphicsWindow.PenColor = "green"
    GraphicsWindow.DrawEllipse(800,140,40,50) 'Head
    ElseIf (seg = 8) Then
    GraphicsWindow.DrawLine(820,190,820,280) 'Body'
    ElseIf (seg = 9) Then
    GraphicsWindow.DrawLine(820,280,780,360) 'Left Leg'
    ElseIf (seg = 10) Then
    GraphicsWindow.DrawLine(820,280,860,360) 'Right Leg'
    ElseIf (seg = 11) Then
    GraphicsWindow.DrawLine(820,200,780,260) 'Left Arm'
    ElseIf (seg = 12) Then
    GraphicsWindow.DrawLine(820,200,860,260) 'Right Arm'
    EndIf
    EndSub

    Please help!

    You will need a words.txt with 10 words in it saved in the same directory for it to work. Thanks, Gorno

    Monday, April 29, 2013 6:01 PM

Answers

  • I put:

      char= GraphicsWindow.LastKey
      If Text.IsSubText(str,char)="False" Then
        str=str+char

    directly after Sub OnKeyDown

    and the endif just before EndSub. The program then ignores keys which have been used already.

    You have no counting of right letters and thus cannot determine if the whole word is guessed. You should add this.

    For testing I added some  GraphicsWindows.ShowMessage("",  <variable> ) lines to show the value of <variable> at that point  (e.g. the value of str just after the adding of char)


    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Monday, April 29, 2013 8:02 PM added testing
    • Marked as answer by Gorno01 Monday, April 29, 2013 8:47 PM
    Monday, April 29, 2013 7:54 PM
    Answerer

All replies

  • You can fill a string with the letters already used and with Text.IsSubtext check if a given letter is in this string.

    Or you can fill an array (indexed with letters) with e.g. 0 and change that to 1 for a used letter


    Jan [ WhTurner ] The Netherlands

    Monday, April 29, 2013 6:07 PM
    Answerer
  • I tried it with an array but couldn't get it to work :(

    Ive never used strings before

    Monday, April 29, 2013 6:11 PM
  • Start with  str=""

    when a letter is used e.g T :  str=str+"T"

    after 5 letter you have e.g.  "TGDNS"

    Text.Issubtext(str,"D")  is "True"     and Text.IsSubtext(str,"X")  is "False"

    The letters used above can also be a variable.


    Jan [ WhTurner ] The Netherlands

    Monday, April 29, 2013 6:24 PM
    Answerer
  • Thanks, i'll try it.
    Monday, April 29, 2013 6:26 PM
  • I still can't get it to work :(

    I know it's a big ask but could you edit it in the right places for me please?

    Thanks

    Monday, April 29, 2013 6:43 PM
  • That will take some more time, i didn't analyse your program yet.

    Jan [ WhTurner ] The Netherlands

    Monday, April 29, 2013 6:53 PM
    Answerer
  • Thank you, I just cant get the loops and stuff in the right places

    Monday, April 29, 2013 6:55 PM
  • I put:

      char= GraphicsWindow.LastKey
      If Text.IsSubText(str,char)="False" Then
        str=str+char

    directly after Sub OnKeyDown

    and the endif just before EndSub. The program then ignores keys which have been used already.

    You have no counting of right letters and thus cannot determine if the whole word is guessed. You should add this.

    For testing I added some  GraphicsWindows.ShowMessage("",  <variable> ) lines to show the value of <variable> at that point  (e.g. the value of str just after the adding of char)


    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Monday, April 29, 2013 8:02 PM added testing
    • Marked as answer by Gorno01 Monday, April 29, 2013 8:47 PM
    Monday, April 29, 2013 7:54 PM
    Answerer
  • It works!!!

    Thank you so much for your help!

    Monday, April 29, 2013 8:47 PM