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

Small Basic Hangman游戏字母检测功能异常问题排查

问题排查与修复方案

核心问题分析

你的Hangman游戏出现字母误判,主要由以下几个关键问题导致:

  • 变量与子程序同名冲突:win既是变量又是子程序名,start既是变量又是子程序名,导致逻辑调用混乱
  • 大小写不匹配:目标单词和输入字母未统一大小写,存在的字母会因大小写差异被误判为不存在
  • 重复猜字母的错误累加:未记录已猜字母,重复猜同一字母时会重复累加胜利计数,触发错误的胜利状态
  • 语法错误:btnclicked子程序中300K是笔误,导致绞刑架绘制失败
  • temp变量逻辑缺陷:原逻辑中只要遇到第一个不匹配字符就设置temp=1,后续匹配无法完全修正判断结果

修复后的完整代码

Start()

Sub Start
  GraphicsWindow.Clear()
  textbox = Controls.AddTextBox(150, 100)
  GraphicsWindow.FontSize = 24
  GraphicsWindow.DrawBoundText(0, 400, 500, "Please enter less than 15 characters.")
  GraphicsWindow.DrawBoundText(100, 50, 400, "Word to Guess")
  Controls.TextTyped = OnTextTyped
  startBtn = Controls.AddButton("Start Game", 150, 200)
  Controls.ButtonClicked = OnButtonClicked
  hangCount = 0
  winCount = 0
  guessedLetters = ""
EndSub

Sub WinGame
  GraphicsWindow.FontSize = 48
  GraphicsWindow.DrawBoundText(200, 100, 600, "You Win!")
  restartBtn = Controls.AddButton("Restart", 0, 100)
EndSub

Sub DrawHangman
  If hangCount = 1 Then
    GraphicsWindow.DrawEllipse(975, 100, 50, 50)
  ElseIf hangCount = 2 Then
    GraphicsWindow.DrawLine(1000, 150, 1000, 225)
  ElseIf hangCount = 3 Then
    GraphicsWindow.DrawLine(950, 175, 1050, 175)
  ElseIf hangCount = 4 Then
    GraphicsWindow.DrawLine(1000, 225, 950, 275)
  ElseIf hangCount = 5 Then
    GraphicsWindow.DrawLine(1000, 225, 1050, 275)
    GraphicsWindow.FontSize = 48
    GraphicsWindow.DrawBoundText(200, 100, 600, "You Lose!")
    restartBtn = Controls.AddButton("Restart", 0, 100)
  EndIf
EndSub  

Sub CheckLetter
  temp = 0
  letterUpper = Text.ConvertToUpperCase(letter)
  wordUpper = Text.ConvertToUpperCase(t)
  
  If Text.IsSubText(guessedLetters, letterUpper) Then
    GraphicsWindow.DrawBoundText(300, 400, 300, "You already guessed this letter!")
    Return
  EndIf
  guessedLetters = guessedLetters + letterUpper
  
  For i = 1 To u
    currentChar = Text.GetSubText(wordUpper, i, 1)
    If currentChar = letterUpper Then
      GraphicsWindow.FontSize = 36
      GraphicsWindow.DrawBoundText(200 + 70* i, 160, 70, letterUpper)
      temp = 2
      winCount = winCount + 1
    EndIf
  Next
  
  If temp = 0 Then
    hangCount = hangCount + 1
    DrawHangman()
  EndIf
  
  If winCount = u Then
    WinGame()
  EndIf
EndSub

Sub OnTextTyped
  t = Controls.GetTextBoxText(textbox)
EndSub

Sub OnButtonClicked
  If Controls.LastClickedButton = restartBtn Then
    Start()
  ElseIf Controls.LastClickedButton = startBtn Then
    GraphicsWindow.Clear()
    GraphicsWindow.DrawLine(800, 300, 1200, 300)
    GraphicsWindow.DrawLine(900, 300, 900, 50)
    GraphicsWindow.DrawLine(900, 50, 1000, 50)
    GraphicsWindow.DrawLine(1000, 50, 1000, 100)
    GraphicsWindow.DrawBoundText(0, 0, 300, "Make sure to be in Full Screen")
    u = Text.GetLength(t)
    enterBtn = Controls.AddButton("Submit", 300, 350)
    guessBox = Controls.AddTextBox(300, 300)
    hangCount = 0
    winCount = 0
    guessedLetters = ""
    For i = 1 To u
      GraphicsWindow.DrawRectangle(200 + 70 * i, 200, 70, 20)
    Next
  ElseIf Controls.LastClickedButton = enterBtn Then
    letter = Controls.GetTextBoxText(guessBox)
    Controls.SetTextBoxText(guessBox, "")
    If Text.GetLength(letter) = 0 Then
      GraphicsWindow.DrawBoundText(300, 400, 200, "Please enter a letter.")
    ElseIf Text.GetLength(letter) = 1 Then
      CheckLetter()
    ElseIf Text.GetLength(letter) >= 2 Then
      GraphicsWindow.DrawBoundText(300, 400, 200, "Please enter only 1 letter.")
    EndIf
  EndIf
EndSub

关键修复说明

  1. 解决同名冲突:将win子程序改为WinGame,start按钮变量改为startBtn,win变量改为winCount,彻底消除命名冲突
  2. 统一大小写校验:将目标单词和输入字母都转为大写,完全避免大小写差异导致的误判
  3. 记录已猜字母:新增guessedLetters变量存储已猜字母,避免重复猜测,并给出明确提示
  4. 优化temp逻辑:移除原逻辑中干扰判断的else分支,仅在循环结束后根据temp值判断是否匹配到字母
  5. 修复语法错误:将300K修正为300,确保绞刑架正常绘制
  6. 提升用户体验:提交猜测后自动清空输入框,添加胜利/失败的文字提示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 14:28:08