即使用户第一次验证失败,如何在visual basic中保持用户分数?

时间:2015-06-10 20:24:47

标签: vb.net validation visual-studio-2012

您好我正在制作一个刽子手游戏,您猜测一个随机选择的单词并且您有三轮。每次你赢得一轮比赛你获得10分,但是如果你在10次慷慨的尝试之前没有猜到这个词。你将失去一轮没有获得任何东西。

在你赢得三场刽子手游戏之后,你会看到一个高分形式的新输入文本框输入你的名字以保存你的高分,以便在高分形式上显示并且它已经验证了(意思是用户必须在文本框中至少有一个字符)。这是我的主要问题所在,如果您第一次通过验证,我的输入框将保存您的姓名和积分。但是,如果您没有第一次通过验证但第二次通过验证,则会保存您的姓名,但您的高分将会保存,但只能保存一个点。对不起,我的英语不好,但无论如何,即使第一次验证失败而不是将其更改为1分,仍保留用户得分的分数?这是我的代码(抱歉不好的缩进):

Hangman游戏代码(这是用户获得积分的地方)

            Imports System.IO
      Public Class Hangman
      'Public Variables
     Public AttemptsLeft As Integer = 0
     Public Rounds As Integer = 1
      Public LetterChosen As Char
      Dim EndWord() As Char
       Dim AppPath As String = Application.StartupPath()
      Dim FileRead() As String
       Public GameWinner As Boolean = True
       Dim HangmanShapes As New List(Of PowerPacks.Shape)
            Public ScoreForRound As Integer
               Dim NewControls As New List(Of Button)
               Dim GameWord As New List(Of Label)

'Form Load code
Private Sub Hangman_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Load Word Game Code
    If File.Exists(AppPath & "/wordlist.txt") Then
        FileRead = IO.File.ReadAllLines(AppPath & "/wordlist.txt")
        Dim RandomWord As New List(Of String)
        For i = 0 To FileRead.Length - 1
            RandomWord.Add(FileRead(i))
        Next
        Dim random As New Random() 'Using this to randomise each word
        EndWord = RandomWord(random.Next(0, RandomWord.Count - 1)).ToUpper.ToCharArray 'Will put each character of the randomly chosen word into the labels.
        Score.Text = UScore
        Round.Text = Rounds
        Letter1.Text = EndWord(0)
        Letter1.Visible = False
        Letter2.Text = EndWord(1)
        Letter2.Visible = False
        Letter3.Text = EndWord(2)
        Letter3.Visible = False
        Letter4.Text = EndWord(3)
        Letter4.Visible = False
        Letter5.Text = EndWord(4)
        Letter5.Visible = False
        Letter6.Text = EndWord(5)
        Letter6.Visible = False
        'Attempts left code
    End If
    With HangmanShapes
        .Add(Attempt1)
        .Add(Attempt2)
        .Add(Attempt3)
        .Add(Attempt4)
        .Add(Attempt5)
        .Add(Attempt6)
        .Add(Attempt7)
        .Add(Attempt8)
        .Add(Attempt9)
        .Add(Attempt10Part1)
        .Add(Attempt10Part2)
    End With
    With NewControls
        .Add(LetterA)
        .Add(LetterB)
        .Add(LetterC)
        .Add(LetterD)
        .Add(LetterE)
        .Add(LetterF)
        .Add(LetterG)
        .Add(LetterH)
        .Add(LetterI)
        .Add(LetterJ)
        .Add(LetterK)
        .Add(LetterL)
        .Add(LetterM)
        .Add(LetterN)
        .Add(LetterO)
        .Add(LetterP)
        .Add(LetterQ)
        .Add(LetterR)
        .Add(LetterS)
        .Add(LetterT)
        .Add(LetterU)
        .Add(LetterV)
        .Add(LetterW)
        .Add(LetterX)
        .Add(LetterY)
        .Add(LetterZ)
    End With
    With GameWord
        .Add(Me.Letter1)
        .Add(Me.Letter2)
        .Add(Me.Letter3)
        .Add(Me.Letter4)
        .Add(Me.Letter5)
        .Add(Me.Letter6)
    End With
End Sub
Private Sub AllBtnClicks(ByVal sender As System.Object, ByVal e As EventArgs) Handles LetterA.Click, LetterB.Click, LetterC.Click, LetterD.Click, LetterE.Click, LetterF.Click, LetterG.Click, LetterH.Click, LetterI.Click, LetterJ.Click, LetterK.Click, LetterL.Click, LetterM.Click, LetterN.Click, LetterO.Click, LetterP.Click, LetterQ.Click, LetterR.Click, LetterS.Click, LetterT.Click, LetterU.Click, LetterV.Click, LetterW.Click, LetterX.Click, LetterY.Click, LetterZ.Click
    'Declartions
    Dim LetterGuess As Button = sender
    LetterGuess.Enabled = False

    Dim LetterCorrect As Boolean = False
    'Loop
    For Each Letter In EndWord
        If GetChar(LetterGuess.Name, 7) = Letter Then
            Select Case Array.IndexOf(EndWord, Letter)
                Case Is = 0
                    Letter1.Visible = True
                Case Is = 1
                    Letter2.Visible = True
                Case Is = 2
                    Letter3.Visible = True
                Case Is = 3
                    Letter4.Visible = True
                Case Is = 4
                    Letter5.Visible = True
                Case Is = 5
                    Letter6.Visible = True
            End Select
            LetterCorrect = True
        End If
    Next
    'Lives left code
    If LetterCorrect = False Then
        AttemptsLeft += 1

        Select Case AttemptsLeft
            Case 1
                Attempt1.Visible = True
                Attempts.Text = 1
            Case 2
                Attempt2.Visible = True
                Attempts.Text = 2
            Case 3
                Attempt3.Visible = True
                Attempts.Text = 3
            Case 4
                Attempt4.Visible = True
                Attempts.Text = 4
            Case 5
                Attempt5.Visible = True
                Attempts.Text = 5
            Case 6
                Attempt6.Visible = True
                Attempts.Text = 6
            Case 7
                Attempt7.Visible = True
                Attempts.Text = 7
            Case 8
                Attempt8.Visible = True
                Attempts.Text = 8
            Case 9
                Attempt9.Visible = True
                Attempts.Text = 9
            Case 10
                Attempt10Part1.Visible = True
                Attempt10Part2.Visible = True
                Attempts.Text = 10
                LetterA.Enabled = False
                LetterB.Enabled = False
                LetterC.Enabled = False
                LetterD.Enabled = False
                LetterE.Enabled = False
                LetterF.Enabled = False
                LetterG.Enabled = False
                LetterH.Enabled = False
                LetterI.Enabled = False
                LetterJ.Enabled = False
                LetterK.Enabled = False
                LetterL.Enabled = False
                LetterM.Enabled = False
                LetterN.Enabled = False
                LetterO.Enabled = False
                LetterP.Enabled = False
                LetterQ.Enabled = False
                LetterR.Enabled = False
                LetterS.Enabled = False
                LetterT.Enabled = False
                LetterU.Enabled = False
                LetterV.Enabled = False
                LetterW.Enabled = False
                LetterX.Enabled = False
                LetterY.Enabled = False
                LetterZ.Enabled = False
                MsgBox("You have lost the round!")
                ResetForm(0)
        End Select


        'Winning a round code
    Else : Dim GameWinner As Boolean = True
        Dim WordCheck As Label
        For Each WordCheck In GameWord
            If Not WordCheck.Visible Then
                GameWinner = False
                Exit For
            End If
        Next

        If GameWinner Then
            MsgBox("You have won the round!")
            ResetForm(10)

            'Losing a round code

        End If
    End If
End Sub
Private Sub ResetForm(ScoreForRound As Integer)
    UScore += ScoreForRound
    If Rounds = 3 Then
        Me.Close()
        HighScore.Show()
    Else
        Score.Text = +10
        AttemptsLeft = 0
        Attempts.Text = 0
        Rounds += 1
        Hangman_Load(Nothing, Nothing)
        Dim HangmanReset As PowerPacks.Shape
        For Each HangmanReset In HangmanShapes
            HangmanReset.Visible = False
        Next
        Dim ControlReset As Control
        For Each ControlReset In NewControls
            ControlReset.Enabled = True
        Next
    End If
End Sub
End Class

高分形式(这是用户保存积分的地方,之后也可以查看高分)

   Imports System.IO
   Public Class HighScore
Dim AppPath As String = Application.StartupPath()
Public Username As String
Private Sub HighScore_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim FileData() As String
    Dim SizeArray As Integer
    Try
        FileData = File.ReadAllLines(AppPath & "/highscore.txt")
        SizeArray = FileData.Length
    Catch Break As Exception
        MsgBox("The file is missing!", MsgBoxStyle.Critical)
    End Try
    For begin = 0 To SizeArray - 1 Step 1
        Me.UserNameLabel.Text = UserNameLabel.Text & FileData(begin) & vbNewLine
    Next

End Sub

Private Sub Backtomainmenu_Click(sender As Object, e As EventArgs) Handles Backtomainmenu.Click
    MainMenu.Visible = True
    Me.Visible = False
End Sub

Private Sub HelpButtonHighScore_Click(sender As Object, e As EventArgs) Handles HelpButtonHighScore.Click
    MsgBox("This is the high score, this shows the top 10 players who achieved well in this game, this is ranked by the amount of points score. If you want to have your name in this high score, play the game well in order to achieve this.", MsgBoxStyle.Information)
End Sub
'This is where the user saves their high scores
Private Sub SaveName_Click(sender As Object, e As EventArgs) Handles SaveName.Click
    Username = NameInput.Text
    Try
        File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
    Catch ex As Exception
        MsgBox("The file is missing!", MsgBoxStyle.Critical)
    End Try
    UScore = vbNull
    If NameInput.Text = "" Then
        MsgBox("Enter a name please")
    Else
        File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
        Me.Close()
        MainMenu.Show()
    End If
End Sub
       End Class

2 个答案:

答案 0 :(得分:0)

  

用户必须在文本框中至少有一个字符

您的代码目前正在 之前保存到文件

-(void) monitorVolumes
{
    // Notification for Mountingthe USB device

    [[[NSWorkspace sharedWorkspace] notificationCenter]addObserver:self selector: @selector(onVolumeMount:)
      name: NSWorkspaceDidMountNotification object: nil];

    // Notification for Un-Mountingthe USB device

    [[[NSWorkspace sharedWorkspace] notificationCenter]addObserver:self selector: @selector(onVolumeMount:)
      name: NSWorkspaceDidUnmountNotification object: nil];

}

-(void) onVolumeMount: (NSNotification*) notification
{
    NSLog(@"Volume Mount");

   //Code to get path here...
}

-(void) onVolumeUnmount: (NSNotification*) notification
{
    NSLog(@"Volume Unmount");
}

那段代码(已经写入文件)后,您正在尝试验证:

Username = NameInput.Text
Try
    File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
    MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
UScore = vbNull 

整合代码,只有在验证成功时才写入文件:

If NameInput.Text = "" Then
    MsgBox("Enter a name please")
Else
    File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
    Me.Close()
    MainMenu.Show()
End If

答案 1 :(得分:0)

使用UScore = vbNull,您可能会重置分数,即使NameInput.Text = ""

所以,而不是

Try
    File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
    MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
UScore = vbNull
If NameInput.Text = "" Then
    MsgBox("Enter a name please")
Else
    File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
    Me.Close()
    MainMenu.Show()
End If

UScore = vbNull放在If语句中

Try
    File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
    MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
If NameInput.Text = "" Then
    MsgBox("Enter a name please")
Else
    File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
    Me.Close()
    MainMenu.Show()
    UScore = vbNull 'Put it here instead
End If