刽子手/猜字游戏的问题

时间:2016-12-08 16:19:07

标签: vb.net

我正在制作一个刽子手游戏。当我按下按钮时,我需要输入一个单词,然后成为我需要猜测的单词。当我按下按钮时,我遇到了如何检查单词中输入信件的问题。如果字母在单词中,我给的字母需要从*改为自己的字母。例如word:需要转换为密码字符的房子和猜测的字母:o那么它应该变成* o ***。当单词中有多个字母时,都需要显示它们。

这是我在荷兰的代码,但我希望你能提供帮助 这里是一些单词的翻译:woord,word ster,star aantalletters,amountofletters nieuwwoord,newword Geef het woord,给geef een geldig woord这个词,给一个有效的词干扰错误,给错误的错误字母

代码:

Public Class Form1
Dim woord, tip, ster As String
Dim aantalletters As Integer 
Dim letter As String
Dim nieuwwoord As String

Private Sub btnnieuwspel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnieuwspel.Click
    txtwoord.Text = ""
    Try
      woord = InputBox("Geef het woord.", "Het woord")
      Do While woord = "" Or woord < 0 Or woord > 0
        If woord = "" Or woord < 0 Or woord > 0 Then
          MessageBox.Show("geef een geldig woord in")
        End If
        woord = InputBox("Geef het woord.", "Het woord")
      Loop
    Catch
    End Try
    Try
      tip = InputBox("Geef de tip", "De tip")
    Catch

    End Try

    aantalletters = woord.Length
    lblTip.Text = "Tip:" & tip

    For i = 1 To aantalletters
      txtwoord.Text = txtwoord.Text & "*"
    Next
End Sub

Private Sub btnSluiten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSluiten.Click
  Me.Close()
End Sub

Private Sub btnletter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnletter.Click
  letter = InputBox("Geef een letter", "De Letter")

  If woord.Contains(letter) Then

  Else
    MessageBox.Show("jammer mis")
  End If        
End Sub

End Class

1 个答案:

答案 0 :(得分:0)

有几种方法,一种方法如下:

Option Compare Database
Option Explicit

Dim MyWord      As String
Dim txtwoord    As String

' Just used for testing the loop.
Sub Test_It()
    MyWord = "house"      ' Set test word
    txtwoord = "*****"    ' Set test Mask
    ' Call function with my 'guess'
    Debug.Print "Result: " & Find_Characters("o")
    Debug.Print "Result: " & Find_Characters("e")
    Debug.Print "Result: " & Find_Characters("s")
    Debug.Print "Result: " & Find_Characters("u")
    Debug.Print "Result: " & Find_Characters("h")
End Sub

Function Find_Characters(letter As String) As String
Dim i       As Integer
    For i = 1 To Len(MyWord)    ' Loop thru all characters
        If Mid(MyWord, i, 1) = letter Then    ' If found, replace asterisk
           Mid(txtwoord, i, 1) = letter
        End If
    Next i
    Find_Characters = MyWord & vbTab & txtwoord
    ' Need to stop somehow...
    if InStr(1,txtword, "*") = 0 then Msgbox "Finished guessing... Add code...'
End Function