Hi

I was just wondering if someone may be able to help with a program I'm trying to complete for college. I am useless at programming so I have tried and tried to sort it, to no avail, yet its probably something very simple and stupid!

Basically I have created a VB game that has 3 levels (easy, medium and hard) in which the program generates a random number from a range dependent on the level chosen, and the user has up to 5 attempts to guess the correct number. One the user has exceeded the maximum number of attempts, a messagebox appears to say they have lost. Obviously if they guess the number correctly a message box appears to say they have got it correct. What I need to do now (but dont have the knowledge, expertise and patience!) is to show the number of games played, number of games won and number of games lost. I have written code that gets the number of games played to show as 1 game played, but if i play another game, for some reason it doesn't update!

Here is the code I have so far:

Public Class frmGame
    Dim guess As Integer = 10
    Dim answer As Integer
    Dim attempts As Integer = 0
    Dim TurnsExceeded As Boolean = False
    Dim GamesPlayed As Integer
    Dim GamesWon As Integer
    Dim GamesLost As Integer
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        attempts = 0

        If ComboBox1.SelectedItem = "Easy" Then
            Randomize()
            answer = Int(Rnd() * 25)
            Do Until (guess = answer) Or attempts = 5
                attempts = attempts + 1 'add 1 to the attempts variable
                txtAttempts.Text = attempts 'display the attempts variable in the text box
                guess = InputBox("Please enter your guess number. It must be between 0-25")
                If guess = answer Then
                    MsgBox("You Got It! Well Done! The answer was " & answer & ".")
                ElseIf guess < answer Then
                    MsgBox("Your guess is too low, try again")
                ElseIf guess > answer Then
                    MsgBox("Your guess is too high, try again")
                End If
                If attempts = 5 Then
                    MsgBox("You Have Exceeded The Maximum Number of Guesses. YOU LOSE!")
                End If
                If attempts > 1 Then
                    GamesPlayed = +1
                    TextBox1.Text = GamesPlayed
                End If
            Loop
        End If
        If ComboBox1.SelectedItem = "Medium" Then
            Randomize()
            answer = Int(Rnd() * 50)
            Do Until (guess = answer) Or attempts = 5
                attempts = attempts + 1 'add 1 to the attempts variable
                txtAttempts.Text = attempts 'display the attempts variable in the text box
                guess = InputBox("Please enter your guess number. It must be between 0-50")
                If guess = answer Then
                    MsgBox("You Got It! Well Done! The answer was " & answer & ".")
                ElseIf guess < answer Then
                    MsgBox("Your guess is too low, try again")

                ElseIf guess > answer Then
                    MsgBox("Your guess is too high, try again")
                End If
                If attempts = 5 Then
                    MsgBox("You Have Exceeded The Maximum Number of Guesses. YOU LOSE!")
                End If
                If attempts > 1 Then
                    GamesPlayed = +1
                    TextBox1.Text = GamesPlayed
                End If
            Loop
        End If
        If ComboBox1.SelectedItem = "Hard" Then
            Randomize()
            answer = Int(Rnd() * 100)
            Do Until (guess = answer) Or attempts = 5
                attempts = attempts + 1 'add 1 to the attempts variable
                txtAttempts.Text = attempts 'display the attempts variable in the text box
                guess = InputBox("Please enter your guess number. It must be between 0-100")
                If guess = answer Then
                    MsgBox("You Got It! Well Done! The answer was " & answer & ".")
                ElseIf guess < answer Then
                    MsgBox("Your guess is too low, try again")
                ElseIf guess > answer Then
                    MsgBox("Your guess is too high, try again")
                End If
                If attempts = 5 Then
                    MsgBox("You Have Exceeded The Maximum Number of Guesses. YOU LOSE!")
                End If
                If attempts > 1 Then
                    GamesPlayed = +1
                    TextBox1.Text = GamesPlayed
                End If

            Loop

        End If


    End Sub

    Private Sub endBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles endBtn.Click
        Close()

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If attempts > 1 Then
            GamesPlayed = +1
            TextBox1.Text = GamesPlayed
        End If


    End Sub
End Class

Any help would be really, really appreciated!!!

Recommended Answers

All 4 Replies

if you are trying to play the game by pressing the start button then it wouldnt update the attemts because on the start button click you are making the attemts =0 statement.. so when you start a new game the attemt will always be 1.

hi..

i think i found the error.

change the following part:

If attempts > 1 Then
            GamesPlayed = +1
            TextBox1.Text = GamesPlayed
        End If

to

If attempts > 1 Then
            GamesPlayed += 1  // + symbol appears before = to make it increment
            TextBox1.Text = GamesPlayed
        End If

very silly error

Hi,

If you say the attempts is the same as gameplayed then you can change this part of code:

If attempts > 1 Then
            GamesPlayed = +1
            TextBox1.Text = GamesPlayed
        End If

into this:

If attempts = 5 Then
            MsgBox("You played 5 times!")
        Else
            attempts = attempts + 1
            TextBox1.Text = attempts
        End If

You should also think of to reset the attempts when the player select another level.

Thanks for the replies people :) I have changed the = +1 to +=1. I have also tried putting the GamesPlayed code to the top of the button sub, but it still isn't adding to the number of games played as I need it to. So I'm a tad confused!

Also, the number of attempts does in fact reset when changing the level or starting a new game.

This programme is really annoying me now! :( :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.