我正在为学校项目创建一个Hangman游戏,其中一个要求就是以图形方式显示单词。我目前正在使用循环为每个字母添加下划线,并为每个空格添加空格。当用户猜出一封信时,我需要它将所有代表该字母的下划线更改为该字母。例如,如果单词是" 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
我在我试图用来替换字符的代码之前和之后放了**
。
任何人对如何实现这一目标都有任何想法?
谢谢,
答案 0 :(得分:0)
首先,您必须计算数组中的位置。当你用空格分隔每个字母时,每个字母占2个位置(空格+实际字母)。 看起来你的代码中的位置是正确的(使用for循环)。
可以更改数组中的字母,就像添加一个字母一样。
ArrayName(5) = "k"
会将数组的第6个(0,1,2,3,4,5)元素更改为字母k。 “5”可以是任何整数,在数组边界的范围内(0 - arraylength - 1)。如果您想了解有关在VB.net中操作数组的更多信息,请务必选中MSDN。