13 Replies - 4054 Views - Last Post: 13 July 2011 - 09:01 PM Rate Topic: -----

#1 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Number Guessing Game

Posted 09 July 2011 - 06:12 PM

I am new to programming and am currently taking a course in it. However, I can never seem to understand the instructions for my assignments. Can someone help me interpret what I am supposed to do. I'm not asking for specific code but still need to figure out what the application should do. I am very confused. Please help :helpsmilie: :sweatdrop:

Instructions
Create a form with the features shown below. We will have a form with no textboxes.


We will play the number guessing game. The user picks the range for the number to be guessed from a combobox. Then based on the range, the logic decides how many chances they get to guess the correct number according to the following:
Range between 0 and 10, number of guesses is 3. User gets 1 point for guessing.
Range between 0 and 50, number of guesses is 5. User gets 5 points for guessing.
Range between 0 and 100, number of guesses is 8. User gets 50 points for guessing.
Allow the user to play multiple games and for each one you must:

Add the points for a total score. You will need a form-level variable to add scores. This variable is only reset to 0 under the Reset button handler.
Use CInt(Rnd() * MaxRange) where MaxRange is the max possible guess.
For example, for range between 0 and 50, the computerGuess = CInt( Rnd() * 50)

Allow the user to change their game setting by changing the value of the combo without resetting the score to 0.


Your form and logic should include the following buttons:

Buttons : New Game, Reset, Quit.
One combobox showing options of 0-10, 0-50, 0-100. 0-10 is the default.
When the user clicks New Game, a new guess is generated based on the range of the combo.
A numberOfTries variable is set to the allowed chances based on the choice of the combo.
The computerGuess is generated as shown above.
To check which combo item the user selected use the SelectedIndex which is 0 for the first item. Note a combobox control works just like a listbox control.

A loop will run as long as the user is within their number of guesses and has not guessed the correct answer. You may check if the numberOfTries is within the allowed range, then decrease this variable by one inside the loop for each entry the user makes.

Inside the loop get a new guess from the user and compare it to the current guess.
Use Inputbox to get a new guess from the user. Recall Inputbox returns a String.
If they are equal the game ends and you must add the current score to the total score.
The loop should stop.
If they are not equal, the loop continues as long as the number of tries are not exhausted.
If the number of tries has been exhausted, the game ends.
Reset, restarts the score back from 0.

Missing top level comments or empty handlers cause a 5 points loss each.
Note that Rnd() always starts at one particular value. To make Rnd start at a different value at runtime, use Randomize under Form Load.


Is This A Good Question/Topic? 0
  • +

Replies To: Number Guessing Game

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Number Guessing Game

Posted 09 July 2011 - 06:47 PM

GUI:
- Your form will have a combobox where user will select range of guessed number to be. It is like a level, since 0-10 is a low level so few points and 0-100 is a high level so many points.
- Buttons : New Game, Reset, Quit

CODE:
- You will have class scope variables to store score and number of guess.
- If user click on New Game, you look at which selection he did, and the program gueses a number in that range. Then you let him guess which number was that while counting how many times he/she has tried. If he get the number then he get points according to what selection he made. If he still loosing until the max number of guess is reached. stop and restart game which means he loose....
Was This Post Helpful? 1
  • +
  • -

#3 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Re: Number Guessing Game

Posted 10 July 2011 - 12:38 PM

View Postsmohd, on 09 July 2011 - 06:47 PM, said:

GUI:
- Your form will have a combobox where user will select range of guessed number to be. It is like a level, since 0-10 is a low level so few points and 0-100 is a high level so many points.
- Buttons : New Game, Reset, Quit

CODE:
- You will have class scope variables to store score and number of guess.
- If user click on New Game, you look at which selection he did, and the program gueses a number in that range. Then you let him guess which number was that while counting how many times he/she has tried. If he get the number then he get points according to what selection he made. If he still loosing until the max number of guess is reached. stop and restart game which means he loose....


Thank you for putting it in plain English. This is what I need. However I am slightly confused. My teacher told me that the user is to pick a range and a number within that range that he/she believes is the random number generated by the program. I'm confused because if the user picks a range then whats the importance of picking a number? Or vise versa?
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Number Guessing Game

Posted 10 July 2011 - 03:31 PM

The user select a range where a program can select a specific number in that range. This is like saying guess a number between 0-100..... This makes sure that the game is at least playable instead of having any random number from any where which will be difficult to guess
Was This Post Helpful? 1
  • +
  • -

#5 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Re: Number Guessing Game

Posted 10 July 2011 - 09:26 PM


' Declare variables.
Dim intRange10, intRange50, intRange100 As Integer
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

Private Sub cboNumbRange_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

Private Sub btnQuit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
' Button will close the application.
Me.Close()
End Sub

Private Sub cboNumberRange_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboNumberRange.SelectedIndexChanged
End Sub

Private Sub btnNewGame_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
' Create input box that asks user to guess a number.
InputBox("Guess a number from the number range you selected.", "Provide a Value")
' Declare the user's input as string variable.
Dim strNumberGuessed As String
strNumberGuessed = InputBox("Guess a number from the number range you selected.", "Provide a Value")
End Sub
End Class



What should I do after declaring class variables for each number range? My guess is that I need to create If-Then statements for each range?
Was This Post Helpful? 0
  • +
  • -

#6 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 307
  • Joined: 30-January 11

Re: Number Guessing Game

Posted 10 July 2011 - 09:42 PM

You could do that or pass the max value to a instance of the Randam object's Next() method.

http://msdn.microsof...ndom.aspx#Y1055
Was This Post Helpful? 1
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Number Guessing Game

Posted 10 July 2011 - 09:54 PM

In your btnNewGame_Click event you have to check which selection is made as instruction said, using SelectedIndex. And then in each case you generate random number according to what selection is made. Then let user guess that number and you then check if equal.
For my suggestion you will need the following class variables;
count: To check how many time he already guess
MaxCount: Max number of count according to type he selected
score: To save the scored points
number: Store the number generated by the program

This post has been edited by smohd: 10 July 2011 - 09:57 PM

Was This Post Helpful? 1
  • +
  • -

#8 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Re: Number Guessing Game

Posted 10 July 2011 - 10:21 PM


Private Sub btnNewGame_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
' Create input box that asks user to guess a number.
InputBox("Guess a number from the number range you selected.", "Provide a Value")
' Declare the user's input as string variable.
Dim strNumberGuessed As String
strNumberGuessed = InputBox("Guess a number from the number range you selected.", "Provide a Value")

' Declare formula when user selects each number range.
If cboNumberRange.SelectedIndex = 1 Then
intRange10 = CInt(Rnd() * 3)
ElseIf cboNumberRange.SelectedIndex = 2 Then
intRange50 = CInt(Rnd() * 5)
ElseIf cboNumberRange.SelectedIndex = 3 Then
intRange100 = CInt(Rnd() * 8)
End If

End Sub
End Class


does it make sense to do this in the btnNewGame? After the selected index declarations how do I link that to a score? I'm sorry for so many questions. I am so new to this. I'm still trying to figure out the logic of how it works then trying to use a book to figure out which code to write.

This post has been edited by sucks@VB: 10 July 2011 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Re: Number Guessing Game

Posted 10 July 2011 - 10:27 PM

I'm assuming now I have to make a correlation between the selected range and the random generator and use something that places that number in the corresponding range?
Was This Post Helpful? 0
  • +
  • -

#10 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Re: Number Guessing Game

Posted 10 July 2011 - 10:32 PM

I'm thinking after the first If-Then statements to also add these If-Then statements:

If cboNumberRange.SelectedIndex = intRange10 Then


but the problem is I don't know what the "then" statement should be. I'm thinking it should be then allot 1 point to the lblTotalScore?
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Number Guessing Game

Posted 11 July 2011 - 12:09 AM

Lets me give you a little guide then you can get where to start
'NOTE: I only take part of newGame buton
'First I will declare my class scope variables
Dim count As Integer = 0
Dim MaxCount As Integer
Dim score As Integer = 0
Dim number As Integer
'Then in my btnNewGame_Click event, I will have something like this
Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
'First I will check what he selected
  If cboNumberRange.SelectedIndex = 0 Then
            'Now it depends how you arrange them. According to your instruction the 0-10 is the default, so easily make it the first one in collection
            MaxCount = 3
            'Then generate number between 0 and 10 as shown in your instruction
            'Now you have your number, it is time to ask user to input value
            'Here I recommend you create another sub(if you know how) that will ask and check which will be called in every case here
            'Otherwise just will do it here. but it will be after if which will contain loop

        ElseIf cboNumberRange.SelectedIndex = 1 Then
            MaxCount = 5
            'Then generate number between 0 and 50 as shown in your instruction
        Else
            MaxCount = 8
            'Then generate number between 0 and 100 as shown in your instruction
        End If



Try it and then tell what you have got for people to help you figure out
NOTE: :code:
Also follow the instructions given
Also you have to post your updated code to see where you have arrive, we have guided you to get to the point, now when you return tell us what you achieve up to and what new problem you have
Was This Post Helpful? 1
  • +
  • -

#12 sucks@VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-July 11

Re: Number Guessing Game

Posted 13 July 2011 - 02:42 PM

            Do While NumGuessInt > 0 And ContinuePlay

                Try

                    UserGuessInt = CInt(InputBox("Please enter your guess between 0 and " & CStr(MaxPossible) & ".", "Make a Guess!"))

                    'To simplify we're not Checking to make sure that guess is an integer and will fall within the correct range
                   
                        If UserGuessInt = CompGuessInt Then
                            PointsInt += CurrentPoints
                            MessageBox.Show("You Win!" & ControlChars.CrLf & "You now have " & CStr(PointsInt) & "!", _
                                            "Congrats!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                            ScoreLbl.Text = CStr(PointsInt)
                            ContinuePlay = False
                            'If the user did not guess the computer's number, decrement their number of guess
                            'Tell the user they have not yet won and tell them how many guesses they have left
                        Else
                            NumGuessInt -= 1
                            MessageBox.Show("That was incorrect.  Try again. You have " & CStr(NumGuessInt) & " guesses remaining")
                        End If

                        'If user does not enter a value in the correct range, then display warning message
                   
                Catch ex As Exception
                    MessageBox.Show("Please enter a number between 0 and " & MaxPossible)

                End Try

            Loop




I was able to get to this point with help. Thanks
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Number Guessing Game

Posted 13 July 2011 - 08:04 PM

Ok, what is this event?(when this happens)

NOTE: First your exception will not catch out of range numbers, instead it will catch other incorrect inputs like letters and other characters. Else it is good up to now ;)
Also what remains now?

This post has been edited by smohd: 13 July 2011 - 08:05 PM

Was This Post Helpful? 1
  • +
  • -

#14 collegemant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-July 11

Re: Number Guessing Game

Posted 13 July 2011 - 09:01 PM

there is alot of diffrent ways you could do this... i would use a rand. num generator.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1