smallbasic

Small basic function only works on first guess (hangman)


When you run the program your first guess doesn't work, after the first guess the program works as expected. The file the program needs to read is the mit 10k words txt file. Im unsure how to fix the problem, my guess is that it doesnt read the first index properly due to the order the code ix executed in.

Start:
GraphicsWindow.Clear()
GraphicsWindow.BackgroundColor = "SteelBlue"
GraphicsWindow.Title = "Hangman"
GraphicsWindow.Width = 1920
GraphicsWindow.Height = 1080
GraphicsWindow.Show()
GraphicsWindow.PenColor = "Black"
GraphicsWindow.FontSize = 30
GraphicsWindow.BrushColor = "Black"

GraphicsWindow.Left = -10
GraphicsWindow.Top = 0
 Controls.AddTextBox(150, 540)

xStart = 150

'TextWindow.Hide()

Sub YouWin
    TextWindow.WriteLine("You Won!!!")
    WriteBlankLine()
    TextWindow.Write("The word was: ")
    TextWindow.Write(WordYouNeedToGuess)
    WriteBlankLine()
    EndSub


WordCompleted = 0
Sub WriteBlankLine
  TextWindow.WriteLine(" ")
  EndSub



WordYouNeedToGuess = 0
var = 0

While var = 0 
  WordYouNeedToGuess = File.ReadLine("**ADD YOUR FILEPATH HERE**", Math.GetRandomNumber(10000))
  var = 1
EndWhile
TextWindow.WriteLine(WordYouNeedToGuess)
index = 0
WordLength = Text.GetLength(WordYouNeedToGuess)
For index = 1 to WordLength
  GraphicsWindow.PenWidth = 10
 
  
  GraphicsWindow.DrawLine(xStart, 360, xStart + 75, 360)
  xStart = xStart + 125
  ListOfxStartingPositions[index] = xStart
  TextWindow.Write("x started at: ")
  TextWindow.Write(xStart)
  WriteBlankLine()
  TextWindow.WriteLine(ListOfxStartingPositions)
endfor






For i = 1 To WordLength
  TextWindow.WriteLine(i)
EndFor
index = 0
While index < WordLength + 1
  input = Text.GetSubText(WordYouNeedToGuess, index, 1)
  index = index+1
  Data[index] = input
  TextWindow.WriteLine(input)
  TextWindow.WriteLine(Data)
  TextWindow.WriteLine(index)
EndWhile
index = 0
TextWindow.WriteLine("Guess the letter")
IsLetterGuessed = 0
NumLettersGuessed = 0




used = ""


Sub Guessletter
  index = 0
  'TextWindow.Hide()
  pen:
  Controls.SetTextBoxText(Controls.LastTypedTextBox,"")
  Letterguess = Controls.GetTextBoxText(Controls.LastTypedTextBox)
  If Text.IsSubText(used, Letterguess) Then
    TextWindow.WriteLine("You have guessed this!")
    Goto pen
    endif
  If Letterguess = "" Then
    Goto Pen
    EndIf
 
 For index = 1 To WordLength + 1
   TextWindow.WriteLine(Data[index])
   TextWindow.WriteLine(used)
   Usedletterslength = Text.GetLength(used)
   TextWindow.WriteLine(Usedletterslength)
 


Letterguess = Text.GetSubText(Letterguess, 1, 1)
   If Data[index] = Letterguess Then
     
      TextWindow.Write("Your letter guess was found at: ")
      TextWindow.Writeline(index -1)
      xPosToDraw = ListOfxStartingPositions[index - 2]
      If xPosToDraw < ListOfxStartingPositions[1] then
      xPosToDraw = 150
    EndIf
    
      GraphicsWindow.DrawText(xPosToDraw + 30, 300, Letterguess)
      
      
      
      TextWindow.Write(" ")
      Data[index] = ""
      
      
      
      NumLettersGuessed = NumLettersGuessed + 1
    EndIf
  EndFor
  
  used = used + Letterguess
EndSub



NumLettersGuessed = 0
While WordCompleted = 0
   
  Guessletter()
  TextWindow.Write("Length of word ")
  TextWindow.Write(WordLength)
  TextWindow.WriteLine(" ")
  TextWindow.Write("Number of letters guessed ") 
  TextWindow.Write(NumLettersGuessed)
  WriteBlankLine()
  if NumLettersGuessed = WordLength then
WordCompleted = 1
YouWin()
Goto Start
endif
EndWhile





'This script will draw a letter at the right line



Solution

  • GetTextBoxText does not WAIT for something to be typed. Thus, you're in a tight loop that clears the box, fetches the box, and checks for null. If you happen to type just AFTER the GetTextBoxText call, it will be erased before you read it. You should have something like wait: / GetTextBoxText / if guess = "" Then / Goto wait without doing the clear in the middle.