You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Small Basic猜字游戏首猜功能失效问题求助

Small Basic猜字游戏首次猜测失效问题修复

问题根源是代码中字符串索引处理错误和数组索引不匹配,导致首次猜测时无法正确匹配字母或绘制位置。Small Basic的字符串索引从1开始,但原代码错误地从0开始读取子串,同时字母位置计算存在索引越界问题。

具体修复点

  1. 修正Data数组填充逻辑
    原代码从index=0开始读取子串,导致Data数组的有效字母从索引2开始,与实际字母位置不匹配。改为从index=1开始读取字符串,让Data数组索引1对应第一个字母,索引N对应第N个字母。

  2. 调整Guessletter循环范围
    原循环到WordLength + 1,但修正后的Data数组仅到WordLength索引,所以循环范围改为1 To WordLength。

  3. 提前处理输入为单个字符
    将Letterguess = Text.GetSubText(Letterguess, 1, 1)移到循环外,避免重复处理,确保整个循环使用统一的单个字符进行匹配。

  4. 修正字母绘制位置计算
    原位置计算使用index-2导致索引越界,改为用ListOfxStartingPositions[index] - 125直接获取对应线条的起始位置,无需额外判断。

修复后的完整代码

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

' 修复Data数组填充逻辑
index = 1
While index <= WordLength
  input = Text.GetSubText(WordYouNeedToGuess, index, 1)
  Data[index] = input
  TextWindow.WriteLine(input)
  TextWindow.WriteLine(Data)
  TextWindow.WriteLine(index)
  index = index + 1
EndWhile

index = 0
TextWindow.WriteLine("Guess the letter")
IsLetterGuessed = 0
NumLettersGuessed = 0

used = ""

Sub Guessletter
  index = 0
  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

  ' 提前处理为单个字符,移到循环外
  Letterguess = Text.GetSubText(Letterguess, 1, 1)
 
 ' 调整循环范围到WordLength
 For index = 1 To WordLength
   TextWindow.WriteLine(Data[index])
   TextWindow.WriteLine(used)
   Usedletterslength = Text.GetLength(used)
   TextWindow.WriteLine(Usedletterslength)

   If Data[index] = Letterguess Then
     
      TextWindow.Write("Your letter guess was found at: ")
      TextWindow.Writeline(index)
      ' 修复位置计算逻辑
      xPosToDraw = ListOfxStartingPositions[index] - 125
      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

内容的提问来源于stack exchange,提问作者Geekknight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 01:17:56