【发布时间】:2016-02-09 21:16:17
【问题描述】:
我正在为一个学校项目创建一个刽子手游戏,其中一个要求是以图形方式显示单词。我目前正在使用循环为每个字母添加下划线,并为每个空格添加一个空格。当用户猜出一个字母时,我需要将代表该字母的所有下划线更改为该字母。例如,如果单词是“Stack Overflow”,它会是_ _ _ _ _ _ _ _ _ _ _ _ _,如果他们猜到了o,它会变成_ _ _ _ _ O _ _ _ _ _ o _。如何更改数组中的某些字母/字符以允许我更改字母?
这是我的代码:
Imports System.Runtime.InteropServices
Public Class Hangman
Dim UnderscoresStr() As Char
Dim GuessLength As Integer
Dim GuessedLetter As String
Dim WordLength As Integer
Dim RandomNum As String
Dim Category As String
Dim Word As String
Dim Letters() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Private Const EM_SETCUEBANNER As Integer = &H1501
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
Private Sub SetCueText(ByVal control As Control, ByVal text As String)
SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
End Sub
Public Function Random()
Dim r As Random = New Random
RandomNum = r.Next(1, 5)
Return RandomNum
End Function
Public Function Underscores(Length)
Dim WordArr() As Char = Word.ToCharArray()
Dim UnderscoresText As String
For i = 0 To Length - 1
If WordArr(i) = " " Then
UnderscoresText += " "
Else
UnderscoresText += "_"
UnderscoresText += " "
End If
Next
labelWord.Text = UnderscoresText
Dim UnderscoresStr() As Char = UnderscoresText.ToCharArray
Return UnderscoresText
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetCueText(comboboxCategory, "Category...")
SetCueText(textboxGuess, "Enter your guess...")
buttonGuess.Enabled = False
buttonGiveUp.Enabled = False
textboxGuess.Enabled = False
End Sub
Private Sub buttonStart_Click(sender As Object, e As EventArgs) Handles buttonStart.Click
Category = comboboxCategory.Text
Dim Food() As String = {"Burger", "Pizza", "Fish And Chips", "Steak Pie", "Spaghetti"}
Dim Animals() As String = {"Lammergeier", "Portia Spider", "Hagfish", "Demon Stingerfish", "Tapir"}
Dim Movies() As String = {"Mrs Doubtfire", "Jurassic World", "Life Of Pi", "Fast And Furious", "Fury"}
Dim FastFoodRestaurants() As String = {"Burger King", "KFC", "McDonalds", "Five Guys", "Costa"}
Dim ChocolateBars() As String = {"Double Decker", "Mars Bar", "Twix", "Wispa", "Twirl"}
If comboboxCategory.Text = "" Then
MsgBox("Please select a category first!")
Exit Sub
Else
labelGuessesLeft.Text = "Guesses Left: 6"
buttonGuess.Enabled = True
buttonGiveUp.Enabled = True
textboxGuess.Enabled = True
buttonStart.Enabled = False
comboboxCategory.Enabled = False
End If
For i = 0 To 25
listboxLettersLeft.Items.Add(Letters(i))
Next
Select Case Category
Case "Food"
Word = Food(Random())
Case "Animals"
Word = Animals(Random())
Case "Movies"
Word = Movies(Random())
Case "Fast Food Restaurants"
Word = FastFoodRestaurants(Random())
Case "Chocolate Bars"
Word = ChocolateBars(Random())
End Select
MsgBox(Word)
WordLength = Len(Word)
Underscores(WordLength)
End Sub
Private Sub buttonGiveUp_Click(sender As Object, e As EventArgs) Handles buttonGiveUp.Click
pictureboxHangman.Image = My.Resources.hangman_full
End Sub
Private Sub buttonGuess_Click(sender As Object, e As EventArgs) Handles buttonGuess.Click
Dim WordArr() As Char = Word.ToCharArray()
GuessedLetter = textboxGuess.Text
GuessLength = Len(GuessedLetter)
If GuessLength > 1 Then
MsgBox("Please only enter a single letter!", 16, "Error!")
Else
If GuessedLetter Like "[a-z]" Then
If listboxGuessed.Items.Contains(GuessedLetter) Then
MsgBox("Please enter a letter you haven't already guessed!", 16, "Error!")
Else
listboxGuessed.Items.Add(GuessedLetter)
listboxLettersLeft.Items.Remove(GuessedLetter)
For i = 0 To WordLength - 1
If WordArr(i) = GuessedLetter Then
** UnderscoresStr(i) = GuessedLetter **
End If
Next
End If
Else
MsgBox("Please only enter a letter!", 16, "Error!")
End If
End If
End Sub
End Class
我在我试图用来替换字符的代码之前和之后都放置了**。
有人对我如何实现这一点有任何想法吗?
谢谢,
【问题讨论】:
-
看起来正确。你试过了吗?您有任何问题或遇到任何错误吗?
-
@RBarryYoung 是的,首先我意识到我忘记重新定义标签的文本以获取数组的值,所以我包含了
Dim UnderscoresStrString As String = String.Join("", UnderscoresStr) labelWord.Text = UnderscoresStrString但是现在标签只是显示为空白,当我猜一封信。甚至没有任何下划线出现。
标签: vb.net