Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Here's my problem 2

Status
Not open for further replies.

kr0n1k

Technical User
Joined
Jan 15, 2001
Messages
2
Location
US
This is a guessing game and on the fifth attempt you are supposed to get a Game Over message box. My code gives a Game Over box but also gives a message box saying you have 0 attempts left. I need to eliminate the 0 attempts msgbox. Also if i guess correctly on the fifth try i get the msgbox you guessed correctly but i also get the game over box after. One last thing on my vbquestion box asking whether or not you want to play again i have to click the yes or no buttons twice. Thanks for your input.

Private Sub cmdGuess_Click()

GuessNum = txtGuessNum.Text
Output = lblOutput.Caption


xChances = xChances + 1
xAttempts = xAttempts + 1


If GuessNum = EnterNum Then
MsgBox "You guessed correctly and you did it on your " & xAttempts & _
" attempt!"
MsgBox "Play again?", vbQuestion + vbYesNo

If MsgBox("Play again?", vbQuestion + vbYesNo) = vbYes Then
Unload Me
frmEnterNum.Show
Else
Unload Me
End If

Else

MsgBox "That is incorrect you only have " & CInt(6 - xChances) & _
" more chances!"

End If

If xChances = 6 Then

MsgBox "Game Over"
MsgBox "Play again?", vbQuestion + vbYesNo

If MsgBox("Play again?", vbQuestion + vbYesNo) = vbYes Then
Unload Me
frmEnterNum.Show
Else
Unload Me
End If

End If

End Sub

Private Sub Form_Load()

xChances = 1
xAttempts = 0

End Sub
 
For the "0 attemps left" msgbox try this...

Else
if xChances < 5 then
MsgBox &quot;That is incorrect you only have &quot; &amp; CInt(6 - xChances) &amp; _
&quot; more chances!&quot;
End If
End If

This will bypass this box if there are no more chances left.

For the &quot;Game Over&quot;, try this...

If xChances = 6 And GuessNum <> EnterNum Then

This will bypass the game over msgbox if you guessed right.

On the having to click twice problem, try this...


resp = MsgBox (&quot;Play again?&quot;, vbQuestion + vbYesNo)

If resp = vbYes Then
Unload Me
frmEnterNum.Show
Else
Unload Me
End If

I hope this helps!
 
Try this...modify if needbe but theprinciple is correct

Private Sub cmdGuess_Click()
Const kMAX_ATTEMPTS = 5
Dim strGuessNum As String
Dim Game_Over As Boolean

Game_Over = False
strGuessNum = txtGuessNum.Text
If intAttempts = kMAX_ATTEMPTS Then
MsgBox &quot;Game Over. You lose!&quot;
Game_Over = True
Else
intAttempts = intAttempts + 1
'Check if user's guess matches number.
If strGuessNum = strEnterNum Then
'You may want to add an st or nd or rd or th as needbe in your message.
MsgBox &quot;You guessed correctly and you did it on your &quot; &amp; intAttempts &amp; &quot; attempt!&quot;
Game_Over = True
Else
MsgBox &quot;That is incorrect you only have &quot; &amp; (6 - intAttempts) &amp; &quot; more chances!&quot;
End If
End If
If Game_Over Then
If MsgBox(&quot;Play again?&quot;, vbQuestion + vbYesNo) = vbYes Then
Unload Me
frmEnterNum.Show
Else
Unload Me
End If
End If
End Sub

Private Sub Form_Load()
intAttempts = 0
End Sub
 
Thanks for all your help guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top