8 Replies - 2376 Views - Last Post: 04 March 2013 - 04:36 AM Rate Topic: -----

#1 AnnabelleCheung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-February 13

Guess The Game Visual Basic

Posted 26 February 2013 - 06:43 PM

This game is for one player to insert a number, then the other player to guess it. We are supposed to make the game, and then add in the following:

1. Add in a label to say if the second player's guess was too high or too low
2. The challenger is only suppsoed to put a num between 1 to 10, but when they do, my program does not stop them. I don't know what to do
3. To add in a list of the second players previous guesses to help them with the guesses
4. Add a feature to the program to keep a running score of the number of games the player has won or lost

Also, I think i have made an error in the program, so when the the game is replayed not all of the Label/cmd/txt components are reset properly. How do I correct this?

This is my program so far.
[ Public Class Form1
    Dim Number As Integer
    Dim GuessCount As Integer   
    Private Sub cmdAcceptNumber_Click(sender As System.Object, e As System.EventArgs) Handles cmdAcceptNumber.Click


        Label1.Visible = False
        Number = txtNumberBox.Text
        txtNumberBox.Visible = False
        cmdAcceptNumber.Visible = False

        txtGuess.Visible = True
        Label2.Visible = True
        cmdAcceptGuess.Visible = True

        txtGuess.Focus()

    End Sub

    Private Sub cmdExit_Click(sender As System.Object, e As System.EventArgs) Handles cmdExit.Click
        End
    End Sub

    Private Sub cmdAcceptGuess_Click(sender As System.Object, e As System.EventArgs) Handles cmdAcceptGuess.Click
        Dim Guess As Integer
        Dim msg As String

        Guess = txtGuess.Text
        GuessCount = GuessCount + 1

        If Guess = Number Then
            msg = "Well done you guessed it in " & GuessCount & " guesses"
            MsgBox(msg, , "Game Result = Challenger Wins")
            ResetGame()
            Exit Sub
        Else
            If GuessCount = 7 Then
                Label3.Text = "No more guesses"
                MsgBox("You lose - too many guesses", , "Game Result = Challenger loses!")
                ResetGame()
                Exit Sub
            End If
        End If

        Label3.Text = "Guesses left: " & 7 - GuessCount
        txtGuess.Focus()
        txtGuess.Text = ""

    End Sub

    Private Sub ResetGame()

        Label1.Visible = True
        txtNumberBox.Text = ""
        txtNumberBox.Visible = True
        cmdAcceptNumber.Visible = True

        txtGuess.Visible = False
        Label2.Visible = False
        cmdAcceptGuess.Visible = False
        txtGuess.Text = ""

        GuessCount = 0

    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        txtGuess.Visible = False
        Label2.Visible = False
        cmdAcceptGuess.Visible = False
    End Sub
End Class ]

This post has been edited by modi123_1: 26 February 2013 - 06:46 PM
Reason for edit:: use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Guess The Game Visual Basic

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,017
  • Joined: 12-June 08

Re: Guess The Game Visual Basic

Posted 26 February 2013 - 06:46 PM

VB.NET questions should be in their own forums and not in the database forum. Moving.
Was This Post Helpful? 0
  • +
  • -

#3 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Guess The Game Visual Basic

Posted 27 February 2013 - 01:52 AM

View PostAnnabelleCheung, on 26 February 2013 - 06:43 PM, said:

2. The challenger is only suppsoed to put a num between 1 to 10, but when they do, my program does not stop them. I don't know what to do


You need to test whether the entered number is greater than 10 or less than 1. If it is tell the user and prevent the guess from counting.

e.g.

Is usersGuess > 10 or usersGuess < 1?
If yes "Sorry your guess needs to be between 1-10.
If No Run rest of the code.

This post has been edited by Mossypne: 27 February 2013 - 01:54 AM

Was This Post Helpful? 1
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 236
  • View blog
  • Posts: 781
  • Joined: 01-February 13

Re: Guess The Game Visual Basic

Posted 27 February 2013 - 08:33 AM

Hello AnnabelleCheung,
The first thing you should do is to make the very first two lines of your program :
Option Explicit On
Option Strict On


You will now see 2 lines with little blue lines under them that have errors. You can then put the mouse over the lines with errors and you will see a red square that you can click on and it will show you how to fix these errors.

Now if you change your (If - Else) statements by using the (ElseIf) statement it will be easy to add some more conditions to check for such as if the number is to high or to low with some more (ElseIf) statements. By adding these you can take care of adding steps 1 and 3 in one shot.
        If Guess = Number Then
            msg = "Well done you guessed it in " & GuessCount & " guesses"
            MsgBox(msg, , "Game Result = Challenger Wins")
            ResetGame()
            Exit Sub
        ElseIf GuessCount = 7 Then
            Label3.Text = "No more guesses"
            MsgBox("You lose - too many guesses", , "Game Result = Challenger loses!")
            ResetGame()
            Exit Sub
        ElseIf Guess > Number Then
            'Tell user number is to high
            Label5.Text = "To High"
            'add number to label to keep track of numbers guessed
            Label6.Text = Label6.Text & " - " & Guess.ToString
        ElseIf Guess < Number Then
            'Tell user number is to low
            Label5.Text = "To Low"
            'add number to label to keep track of numbers guessed
            Label6.Text = Label6.Text & " - " & Guess.ToString
        End If


Now that you see this you should be able to understand what you need to do to take care of adding step 2.
To add step 4 you could create another global integer variable to keep track of the number of times the player wins by adding 1 to the global variable every time the (Guess = Number).
:bigsmile:
Was This Post Helpful? 1
  • +
  • -

#5 Drowjam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 16-October 12

Re: Guess The Game Visual Basic

Posted 27 February 2013 - 06:33 PM

OK This iS going to help control the input to 0 - 10 and keep track of their previous guesses

  
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Try
'Try catches when the submit a string that cant be cast to a value type like integer
'also text changed can be anything just put this where ever ur checking the input - its a pain this way trust if u 'tried 10 it would check 1 first which could cause problems. this was just for the example
            If Val_Check(TextBox1.Text) = True Then
                'What should happen if the value is acceptable
            Else
                'Notify the user that The value is not accaptable
            End If
        Catch ex As Exception
            'Notify the User that the text needs to be a number
        End Try
End Sub
'just a enum for controling the behavior of the val_Check sub
 Public Enum Vals_Sub_contol_Mode
        Add
        Remove
        Check_Only
 End Enum
Public Function Val_Check(ByVal value As Integer, Optional ByVal control As Vals_Sub_contol_Mode = Vals_Sub_contol_Mode.Check_Only) As Boolean
        Static Values(0) As Integer
        Select Case control
            Case Vals_Sub_contol_Mode.Add
                If Values.GetUpperBound(0) <> 0 Then
                    For lP As Integer = 0 To Values.GetUpperBound(0)
                        If value = Values(lP) Then Exit Select
                    Next
                End If
                ReDim Preserve Values(Values.GetLength(0))
                Values(Values.GetUpperBound(0)) = value
            Case Vals_Sub_contol_Mode.Remove
                If Values.GetUpperBound(0) <> 0 Then
                    For lP As Integer = 0 To Values.GetUpperBound(0)
                        If value = Values(lP) Then
                            Values(lP) = Values(Values.GetUpperBound(0))
                            ReDim Preserve Values(Values.GetUpperBound(0) - 1)
                        End If
                    Next
                End If
            Case Vals_Sub_contol_Mode.Check_Only
                If Values.GetUpperBound(0) = 0 Then
                    Return False
                Else
                    For Lp As Integer = 0 To Values.GetUpperBound(0)
                        If value = Values(Lp) Then Return True
                    Next
                    Return False
                End If
        End Select
        Return False
    End Function
'On the form load event add this code
  For lP As Integer = 0 To 10
            Val_Check(lP, Vals_Sub_contol_Mode.Add)
  Next
'Now for keepin track of the previouly guessed values

Public Guessed as string

Public sub Guess()
   Guessed = Guessed & " , The value they guessed here"
End sub 








OK This iS going to help control the input to 0 - 10 and keep track of their previous guesses

  
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Try
'Try catches when the submit a string that cant be cast to a value type like integer
'also text changed can be anything just put this where ever ur checking the input - its a pain this way trust if u 'tried 10 it would check 1 first which could cause problems. this was just for the example
            If Val_Check(TextBox1.Text) = True Then
                'What should happen if the value is acceptable
            Else
                'Notify the user that The value is not accaptable
            End If
        Catch ex As Exception
            'Notify the User that the text needs to be a number
        End Try
End Sub
'just a enum for controling the behavior of the val_Check sub
 Public Enum Vals_Sub_contol_Mode
        Add
        Remove
        Check_Only
 End Enum
Public Function Val_Check(ByVal value As Integer, Optional ByVal control As Vals_Sub_contol_Mode = Vals_Sub_contol_Mode.Check_Only) As Boolean
        Static Values(0) As Integer
        Select Case control
            Case Vals_Sub_contol_Mode.Add
                If Values.GetUpperBound(0) <> 0 Then
                    For lP As Integer = 0 To Values.GetUpperBound(0)
                        If value = Values(lP) Then Exit Select
                    Next
                End If
                ReDim Preserve Values(Values.GetLength(0))
                Values(Values.GetUpperBound(0)) = value
            Case Vals_Sub_contol_Mode.Remove
                If Values.GetUpperBound(0) <> 0 Then
                    For lP As Integer = 0 To Values.GetUpperBound(0)
                        If value = Values(lP) Then
                            Values(lP) = Values(Values.GetUpperBound(0))
                            ReDim Preserve Values(Values.GetUpperBound(0) - 1)
                        End If
                    Next
                End If
            Case Vals_Sub_contol_Mode.Check_Only
                If Values.GetUpperBound(0) = 0 Then
                    Return False
                Else
                    For Lp As Integer = 0 To Values.GetUpperBound(0)
                        If value = Values(Lp) Then Return True
                    Next
                    Return False
                End If
        End Select
        Return False
    End Function
'On the form load event add this code
  For lP As Integer = 0 To 10
            Val_Check(lP, Vals_Sub_contol_Mode.Add)
  Next
'Now for keepin track of the previouly guessed values

Public Guessed as string

Public sub Guess()
   Guessed = Guessed & " , The value they guessed here"
End sub 







Was This Post Helpful? 1
  • +
  • -

#6 AnnabelleCheung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-February 13

Re: Guess The Game Visual Basic

Posted 01 March 2013 - 06:06 AM

@IronRazer @ Drowjam and @Mossypne, thank you guys so much for your help. I will try my best to insert your ideas into my visual basic. I promise I won't copy it all! Thank you once again for taking your time to help me out. I will try my best!
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 236
  • View blog
  • Posts: 781
  • Joined: 01-February 13

Re: Guess The Game Visual Basic

Posted 01 March 2013 - 10:35 AM

View PostAnnabelleCheung, on 01 March 2013 - 08:06 AM, said:

@IronRazer @ Drowjam and @Mossypne, thank you guys so much for your help. I will try my best to insert your ideas into my visual basic. I promise I won't copy it all! Thank you once again for taking your time to help me out. I will try my best!

It was no problem. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 AnnabelleCheung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-February 13

Re: Guess The Game Visual Basic

Posted 02 March 2013 - 07:53 PM

[quote name='AnnabelleCheung' date='26 February 2013 - 06:43 PM' timestamp='1361929425' post='1810346']
Was This Post Helpful? 0
  • +
  • -

#9 Marcus Ho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-March 13

Re: Guess The Game Visual Basic

Posted 04 March 2013 - 04:36 AM

Annabel. please don't put bits of code in your game unless you understand it. if Dear asks you and you can't answer....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1