Hi,

I am very new to visual basic 6.0 and I am trying to create a guessing game in visual basic where the computer generates three numbers and the player tries to guess what they are. I am now starting the basics and I have the GUI sorted out but the code is giving me a bit of problem. When the GUESS button is pressed the computer is supposed to generate a random 3 digit number and they must appear as ***. This is what i have so far and it does not work.

Private Sub cmdGuess_Click()
Me.Text2.Text = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

End Sub

The player must then try to guess the three numbers by using a number pad press a button to see if they have won.

Recommended Answers

All 17 Replies

Did you set upperbound and lowerbound to anything?

Private Sub cmdGuess_Click()
Me.Text2.Text = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

End Sub

Please be patient with me as i am a novice at this. No I did not set the upper or lower bounds to anything. Can you explain how to do this?

Here is some code that will hopefully point you in the direction you are trying to go:

Private Sub cmdGuess_Click()

dim upperbound as integer
dim lowerbound as integer

upperbound = 100
lowerbound = 10

Me.Text2.Text = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

End Sub

Thank you it worked somewhat but i have to solve some other problems. Can i get bact to you if more help is needed

Of course; I'd be happy to help whenever I can!

My numbers are being randomly generated by the computer but when i try to get my player to select 3 numbers one is being replaced by the other (in other words only one number is showing up) i know i need to do an if statement but cannot figure out how to code it. I know it needs to be something like this; if the player choses (for instance) 1 3 5 then store it and compare it to the randomly stored number. so should it be something like this?

if (num = 1) then
lbl.player.caption = 1

elseif (num = 2) then
lbl.player.caption = 2

I am choosing the numbers by a numbered key pad.

How do I code my keypad?

Hello,

Nobody seems to be answering my question is that because I really have no clue about what I am doing? I am feeling really pressured here because I really have no clue and i would really like to learn this!!!

Okay... as far as the first issue... you mention that the program picks three random numbers okay but that one is being replaced? It sounds like you are storing a different value in the variable. Do you have any code?

As far as the keypad, what are you trying to do with it? Are you trying to use the keypad to enter numbers in a textbox or are you trying to detect when a user presses one of the buttons on the keypad?

Okay... as far as the first issue... you mention that the program picks three random numbers okay but that one is being replaced? It sounds like you are storing a different value in the variable. Do you have any code?

As far as the keypad, what are you trying to do with it? Are you trying to use the keypad to enter numbers in a textbox or are you trying to detect when a user presses one of the buttons on the keypad?

I am trying to use the keypad to enter and store threee number on a lable. There is one lable for player 1 and one lable for player 2 when both players have picked their numbers they check to see if they have won by clicking a check the number button.

This is the code i have so far and am still working on it.

Private Sub cmdExit_Click()

End

End Sub

Private Sub cmdGuess_Click()
Dim upperbound As Integer
Dim lowerbound As Integer

upperbound = 999
lowerbound = 100

Me.Text1.Text = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Me.lblSelect.Visible = True
cmdPlayer1.Enabled = True
cmdPlayer2.Enabled = True
Me.lblGuesstheNumber.Visible = True



End Sub


Private Sub cmdOne_Click()

End Sub

Private Sub cmdNum_Click(Index As Integer)
'Please help with this section
'Only Three numbers are to be stored
'When the keys on the keypad is clicked
'I know this is wrong but i cannot correct it

Me.Label4.Caption = Me.Label4.Caption & "1"
Me.Label4.Caption = Me.Label4.Caption & "2"
Me.Label4.Caption = Me.Label4.Caption & "3"

End Sub

Private Sub cmdNumber_Click()

End Sub

Private Sub cmdPlayer1_Click()
Me.lblGuesstheNumber.Visible = True
frm1.Enabled = True
Me.lblPlayer1.Visible = True
End Sub

Private Sub cmdPlayer2_Click()
Me.lblGuesstheNumber.Visible = True
frm1.Enabled = True
Me.lblPlayer2.Visible = True
End Sub


Private Sub cmdStartover_Click()

cmdGuess.Caption = "GUESS ! "
lblSelect.Visible = False
cmdPlayer1.Enabled = False
cmdPlayer2.Enabled = False
lblGuesstheNumber.Visible = False
frm1.Enabled = False
lblPlayer1.Visible = False
lblPlayer2.Visible = False
lblCheckWinner.Visible = False
cmdWinner.Enabled = False
lblChecknumber.Visible = False
cmdNumber.Enabled = False
cmdStartover.Enabled = False
cmdExit.Enabled = False


End Sub

Private Sub cmdWinner_Click()


End Sub

Private Sub Form_Load()
cmdGuess.Caption = "GUESS ! "
lblSelect.Visible = False
cmdPlayer1.Enabled = False
cmdPlayer2.Enabled = False
lblGuesstheNumber.Visible = False
frm1.Enabled = False
lblPlayer1.Visible = False
lblPlayer2.Visible = False
lblCheckWinner.Visible = False
cmdWinner.Enabled = False
lblChecknumber.Visible = False
lblNumber.Enabled = False
cmdNumber.Enabled = False
cmdStartover.Enabled = False
cmdExit.Enabled = False


End Sub

Private Sub Label4_Click()
'The three numbers are to be stored here

End Sub

Private Sub Label5_Click()
'And here

End Sub

I've looked through the code. Is there a reason you don't add 3 text boxes for each user and have them type the numbers they want to guess in the text box?

You mentioned that you are using a keypad... is this the keypad on the keyboard or is it a different device?

I have attached a sample project of how I would program a 2 player guessing games where each player picks three numbers. To open the program, unzip the file and open "Number Guess.sln"

Please note that I used beginning programming techniques to make the code more understandable for beginning programmers.

You can enter numbers into the textboxes with the keypad on the keyboard (just make sure Num Lock is on).

Please let me know if this is helpful. I reviewed your code, but without the form and objects, it was difficult to understand what was going on. Feel free to send me your project files if you want me to review those.

we were given the gui and we have to work with that. I will try to attach it.

I replied to your e-mail with updated code.

If you mean with keypad those keys on the right side of the (standard) keyboard, just press Num Lock. If you want to do that programmatically, see: Turn Num Lock, Caps Lock keys on and off with VB6 and you will probably forget that keypad thing :D

This project was written in VB6. The keypad is a control array of command buttons. Here is the code to determine which command button out of the array was clicked and changing a label based on the button:

If player = 1 Then 'See which player is selected
  If Len(Label4.Caption) < 3 Then 'Make sure there are not 3 digits already choosen
    Me.Label4.Caption = Me.Label4.Caption & (Index + 1) Mod 10 'Add the digit to the label.
  End If
ElseIf player = 2 Then
  If Len(Label5.Caption) < 3 Then
    Me.Label5.Caption = Me.Label5.Caption & (Index + 1) Mod 10
  End If
End If

Hi,
You can Use Array of Three Number for Computer Guess and User Guess, instead of having a single number.

Ex

Dim UserGuess(2) As Integer
  Dim ComGuess(2) As Integer

  Sub GenerateComGuess()
     'Generate Guess Here
  End Sub

  Function CompareGuess( ) As Boolean
     ' Check the Guess
  End Function

  ' When Form Load or New Game - Generate Computer Guess

  ' When User type the Key Generate and Update User Guess
  ' Finally Compare the Guesses

I think this is the logic you looking for and I hope this will be helpful for you.

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.