Random Number Game

Guess a random number

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3060 Views - Last Post: 28 June 2009 - 05:20 PM Rate Topic: -----

#1 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Random Number Game

Posted 27 June 2009 - 10:29 AM

 Public Class Form1

	Dim randomGenerator As New Random
	Dim number As Integer
	Dim correctcounter As Integer = 0
	Dim totalcounter As Integer = 0

	Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

		Me.Close()

	End Sub

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

		
	End Sub

	Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click

		'declare variables
		Dim guess As Integer
		Dim excheck As Exception
		Dim strErrorMsg As String
		
		'local variable initialization
		excheck = Nothing
		strErrorMsg = ""
		guess = 0

		Try

			'check if its a integer in guessTextBox
			If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
				'trap the error
				strErrorMsg = "Enter a number."
				Me.guessTextBox.Text = ""
				Me.guessTextBox.Focus()
			End If

			'assign random number
			Integer.TryParse(Me.guessTextBox.Text, guess)

			If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then 
					MessageBox.Show("Guess another number.")
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				End If
			End If

			'reset the text box
			guessTextBox.Text = ""
			guessTextBox.Focus()

		Catch excheck
			If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _  MessageBoxIcon.Exclamation)
			End If
		End Try

		'clean up variables
		excheck = Nothing
		strErrorMsg = Nothing
		guess = Nothing

	End Sub

	Sub GetRandomNum()
		number = randomGenerator.Next(1, 6) 
		Debug.Print("Number is {0}", number) 'test
		totalcounter = 0
	 End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		GetRandomNum()
		'initialize the random number. 
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6) 
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub

	Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
		'selects the existing text
		Me.guessTextBox.SelectAll()
	End Sub

	Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
		'allows only numbers and the backspace key
		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
			AndAlso e.KeyChar <> ControlChars.Back Then
			e.Handled = True
		End If
	End Sub

   
  


I'm trying to get the program to assign a random number for someone to guess. They get five chances, numbers 1-5. However, it always says Incorrect number, but when I click on Check without putting a number in, its says "Congratulations." Obviously I'm doing something wrong.

Also, I need to add an IF/THEN statement for someone to guess either a higher or lower number, but not sure how to do that. Any suggestions?


View Postsoutherngenes, on 27 Jun, 2009 - 09:26 AM, said:

 Public Class Form1

	Dim randomGenerator As New Random
	Dim number As Integer
	Dim correctcounter As Integer = 0
	Dim totalcounter As Integer = 0

	Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

		Me.Close()

	End Sub

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

		
	End Sub

	Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click

		'declare variables
		Dim guess As Integer
		Dim excheck As Exception
		Dim strErrorMsg As String
		
		'local variable initialization
		excheck = Nothing
		strErrorMsg = ""
		guess = 0

		Try

			'check if its a integer in guessTextBox
			If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
				'trap the error
				strErrorMsg = "Enter a number."
				Me.guessTextBox.Text = ""
				Me.guessTextBox.Focus()
			End If

			'assign random number
			Integer.TryParse(Me.guessTextBox.Text, guess)

			If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then 
					MessageBox.Show("Guess another number.")
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				End If
			End If

			'reset the text box
			guessTextBox.Text = ""
			guessTextBox.Focus()

		Catch excheck
			If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _  MessageBoxIcon.Exclamation)
			End If
		End Try

		'clean up variables
		excheck = Nothing
		strErrorMsg = Nothing
		guess = Nothing

	End Sub

	Sub GetRandomNum()
		number = randomGenerator.Next(1, 6) 
		Debug.Print("Number is {0}", number) 'test
		totalcounter = 0
	 End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		GetRandomNum()
		'initialize the random number. 
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6) 
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub

	Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
		'selects the existing text
		Me.guessTextBox.SelectAll()
	End Sub

	Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
		'allows only numbers and the backspace key
		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
			AndAlso e.KeyChar <> ControlChars.Back Then
			e.Handled = True
		End If
	End Sub

   
  


Is This A Good Question/Topic? 0
  • +

Replies To: Random Number Game

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Random Number Game

Posted 27 June 2009 - 11:29 AM

You appear to have copied code in your post twice, might edit and clean that up.

Have you step debugged?
Was This Post Helpful? 0
  • +
  • -

#3 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 27 June 2009 - 11:47 AM

View PostJune7, on 27 Jun, 2009 - 10:29 AM, said:

You appear to have copied code in your post twice, might edit and clean that up.

Have you step debugged?


I accidentally posted it twice...I will to edit it so it will show up only once. Not sure if I can, but will try. I'll also try step debugging. I'm very new to this so very confusing since I'm not sure how to get it to work correctly.
Was This Post Helpful? 0
  • +
  • -

#4 janmichaelintia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 21-June 09

Re: Random Number Game

Posted 27 June 2009 - 01:28 PM

Comment out the randomGenerator.Next method from your 'GetRandomNum' procedure. This is conflicting with your randomGenerator.Next method inside the newGame_Click Sub procedure. This is why you were not getting the right guess. Try this code:


   Sub GetRandomNum()
		' number = randomGenerator.Next(1, 6) commented out
		Debug.Print("Number is {0}", number) 'test
		totalcounter = 0
	End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		'initialize the random number.
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6)
		GetRandomNum()
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub



Was This Post Helpful? 1
  • +
  • -

#5 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 27 June 2009 - 01:51 PM

View Postjanmichaelintia, on 27 Jun, 2009 - 12:28 PM, said:

Comment out the randomGenerator.Next method from your 'GetRandomNum' procedure. This is conflicting with your randomGenerator.Next method inside the newGame_Click Sub procedure. This is why you were not getting the right guess. Try this code:


   Sub GetRandomNum()
		' number = randomGenerator.Next(1, 6) commented out
		Debug.Print("Number is {0}", number) 'test
		totalcounter = 0
	End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		'initialize the random number.
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6)
		GetRandomNum()
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub




I did that. Seems to have worked, but now my range is 0-5. Do I need to do something with one of the ranges? Also, is there a way to have it say Guess a Higher/Lower number? I know its a IF...THEN statement, but not sure where to put it.
Public Class Form1

	Dim randomGenerator As New Random
	Dim number As Integer
	Dim correctcounter As Integer = 0
	Dim totalcounter As Integer = 0

	Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

		Me.Close()

	End Sub

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

		
	End Sub

	Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click

		'declare variables
		Dim guess As Integer
		Dim excheck As Exception
		Dim strErrorMsg As String
		
		'local variable initialization
		excheck = Nothing
		strErrorMsg = ""
		guess = 0

		Try

			'check if its a integer in guessTextBox
			If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
				'trap the error
				strErrorMsg = "Enter a number."
				Me.guessTextBox.Text = ""
				Me.guessTextBox.Focus()
			End If

			'assign random number
			Integer.TryParse(Me.guessTextBox.Text, guess)

			If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then 
					MessageBox.Show("Guess another number.")
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				End If
			End If

			'reset the text box
			guessTextBox.Text = ""
			guessTextBox.Focus()

		Catch excheck
			If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _  MessageBoxIcon.Exclamation)
			End If
		End Try

		'clean up variables
		excheck = Nothing
		strErrorMsg = Nothing
		guess = Nothing

	End Sub

	Sub GetRandomNum()
		  Debug.Print("Number is {0}", number) 'test
		totalcounter = 0
	 End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		GetRandomNum()
		'initialize the random number. 
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6) 
		totalcounter = 0
		guessTextBox.Focus()
	End Sub

	Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
		'selects the existing text
		Me.guessTextBox.SelectAll()
	End Sub

	Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
		'allows only numbers and the backspace key
		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
			AndAlso e.KeyChar <> ControlChars.Back Then
			e.Handled = True
		End If
	End Sub

   


Was This Post Helpful? 0
  • +
  • -

#6 janmichaelintia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 21-June 09

Re: Random Number Game

Posted 27 June 2009 - 02:02 PM

I forgot to mention, you should also move the line 'GetRandomNum()' just below line 'number = randomgenerator.Next(1, 6)' from inside your 'newGame_Click' Sub procedure. You not need to edit the ranges it works fine and it only ranges from 1 to 6. Here's the code:


Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		'initialize the random number.
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6)
				 GetRandomNum()
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub






I can't seem to get the 'Higher/Lower' number guess. Can you give an example?

This post has been edited by janmichaelintia: 27 June 2009 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 27 June 2009 - 02:26 PM

For example: someone guesses 5 when the number should be 2. I want it to say "Guess a lower number." Or they guess the number 3 when it should be 4 so I want to say "Guess a higher number." I think its a nested loop in the If then statement, just trying to figure out the best way to do it...

By the way, for some reason its putting in 0 as a number to guess for some reason. Have to figure out why its doing that.

View Postjanmichaelintia, on 27 Jun, 2009 - 01:02 PM, said:

I forgot to mention, you should also move the line 'GetRandomNum()' just below line 'number = randomgenerator.Next(1, 6)' from inside your 'newGame_Click' Sub procedure. You not need to edit the ranges it works fine and it only ranges from 1 to 6. Here's the code:


Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		'initialize the random number.
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6)
				 GetRandomNum()
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub






I can't seem to get the 'Higher/Lower' number guess. Can you give an example?

Was This Post Helpful? 0
  • +
  • -

#8 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 27 June 2009 - 06:33 PM

View Postsoutherngenes, on 27 Jun, 2009 - 01:26 PM, said:

For example: someone guesses 5 when the number should be 2. I want it to say "Guess a lower number." Or they guess the number 3 when it should be 4 so I want to say "Guess a higher number." I think its a nested loop in the If then statement, just trying to figure out the best way to do it...

By the way, for some reason its putting in 0 as a number to guess for some reason. Have to figure out why its doing that.

View Postjanmichaelintia, on 27 Jun, 2009 - 01:02 PM, said:

I forgot to mention, you should also move the line 'GetRandomNum()' just below line 'number = randomgenerator.Next(1, 6)' from inside your 'newGame_Click' Sub procedure. You not need to edit the ranges it works fine and it only ranges from 1 to 6. Here's the code:


Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		'initialize the random number.
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 6)
				 GetRandomNum()
		totalcounter = 0
		'gamecounter += 1
		guessTextBox.Focus()
	End Sub






I can't seem to get the 'Higher/Lower' number guess. Can you give an example?

For example: someone guesses 5 when the number should be 2. I want it to say "Guess a lower number." Or they guess the number 3 when it should be 4 so I want to say "Guess a higher number." I think its a nested loop in the If then statement, just trying to figure out the best way to do it...

By the way, for some reason its putting in 0 as a number to guess for some reason. Have to figure out why its doing that.
Was This Post Helpful? 0
  • +
  • -

#9 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Random Number Game

Posted 28 June 2009 - 01:05 AM

Hi,

I think you can do something like this:



 If guess < number Then
			   Label1.Text = "Too Low"
				totalcounter += 1
				 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
			   Label1.Text = ""
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				   Label1.Text = ""
			Elseif guess > number then
				Label1.Text = " Too High"
				totalcounter += 1
 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
				Label1.Text = ""
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				   Label1.Text = ""
				End If
else

If guess = number then
Label1.Text = "Excellent ! "
correctcounter += 1
MsgBox("Congratulations! You guessed correctly.")
			End If


Was This Post Helpful? 0
  • +
  • -

#10 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 28 June 2009 - 08:47 AM

View PostLuc001, on 28 Jun, 2009 - 12:05 AM, said:

Hi,

I think you can do something like this:



 If guess < number Then
			   Label1.Text = "Too Low"
				totalcounter += 1
				 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
			   Label1.Text = ""
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				   Label1.Text = ""
			Elseif guess > number then
				Label1.Text = " Too High"
				totalcounter += 1
 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
				Label1.Text = ""
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				   Label1.Text = ""
				End If
else

If guess = number then
Label1.Text = "Excellent ! "
correctcounter += 1
MsgBox("Congratulations! You guessed correctly.")
			End If



I tried that, and it appears to work, but not always correctly. I'll see if I can figure what is going wrong.
Was This Post Helpful? 0
  • +
  • -

#11 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Random Number Game

Posted 28 June 2009 - 08:56 AM

Hi,

What's going wrong?
Was This Post Helpful? 0
  • +
  • -

#12 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 28 June 2009 - 09:30 AM

Ok. I have it somewhat working; however, think there's something wrong with my IF...THEN statements because it will tell me to enter a lower/higher number, which may be correct the first time through the loop, but then it doesn't tell me the correct thing. For example: the number is 8, but I guess 5, the first time it will say to enter a higher number so I enter 7, but then it will say to enter a lower number. Here's the code:
Public Class Form1

	Dim randomGenerator As New Random
	Dim number As Integer
	Dim correctcounter As Integer = 0
	Dim totalcounter As Integer = 0

	Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

		Me.Close()

	End Sub

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

		
	End Sub

	Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click

		'declare variables
		Dim guess As Integer
		Dim excheck As Exception
		Dim strErrorMsg As String
		
		'local variable initialization
		excheck = Nothing
		strErrorMsg = ""
		guess = 0

		Try

			'assign random number
			Integer.TryParse(Me.guessTextBox.Text, guess)
		number = randomGenerator.Nex(1,11) ‘ numbers 1-10

'check if its a integer in guessTextBox
			If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
				'trap the error
				strErrorMsg = "Enter a number."
				Me.guessTextBox.Text = ""
				Me.guessTextBox.Focus()
			End If

			
			If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then ‘5 chances 
				  If guess < number then
				MsgBox(“Guess a higher number.”)
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				Else
				MsgBox(“Guess a lower number.”) 
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				  End if
		Else
 MsgBox("You didn't guess correctly. The number is:  " & number)
					GetRandomNum()
				End If
			End If

			'reset the text box
			guessTextBox.Text = ""
			guessTextBox.Focus()

		Catch excheck
			If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _  MessageBoxIcon.Exclamation)
			End If
		End Try

		'clean up variables
		excheck = Nothing
		strErrorMsg = Nothing
		guess = Nothing

	End Sub

	Sub GetRandomNum()
		  Debug.Print("Number is {0}", number) 'test to see #
		totalcounter = 0
	 End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		GetRandomNum()
		'initialize the random number. 
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 11) ‘numbers 1 through 10 
				 GetRandomNum()
  totalcounter = 0
		guessTextBox.Focus()
	End Sub

	Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
		'selects the existing text
		Me.guessTextBox.SelectAll()
	End Sub

	Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
		'allows only numbers and the backspace key
		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
			AndAlso e.KeyChar <> ControlChars.Back Then
			e.Handled = True
		End If
	End Sub



Was This Post Helpful? 0
  • +
  • -

#13 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Random Number Game

Posted 28 June 2009 - 10:03 AM

Change this part of your code:

 If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then ‘5 chances
				  If guess < number then
				MsgBox(“Guess a higher number.”)
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				Else
				MsgBox(“Guess a lower number.”)
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				  End if
		Else
 MsgBox("You didn't guess correctly. The number is:  " & number)
					GetRandomNum()
				End If
			End If



Into this one:


If guess < number Then
			   Messagebox.show("Too Low")
				totalcounter += 1
				 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
			   
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				   
			Elseif guess > number then
				Messagebox.show(" Too High")
				totalcounter += 1
 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
			   
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				  
				End If
else

If guess = number then

correctcounter += 1
MsgBox("Congratulations! You guessed correctly.")
			End If


Was This Post Helpful? 0
  • +
  • -

#14 southerngenes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 09

Re: Random Number Game

Posted 28 June 2009 - 10:38 AM

View PostLuc001, on 28 Jun, 2009 - 09:03 AM, said:

Change this part of your code:

 If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then ‘5 chances
				  If guess < number then
				MsgBox(“Guess a higher number.”)
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				Else
				MsgBox(“Guess a lower number.”)
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				  End if
		Else
 MsgBox("You didn't guess correctly. The number is:  " & number)
					GetRandomNum()
				End If
			End If



Into this one:


If guess < number Then
			   Messagebox.show("Too Low")
				totalcounter += 1
				 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
			   
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				   
			Elseif guess > number then
				Messagebox.show(" Too High")
				totalcounter += 1
 If totalcounter < 5 Then
					MessageBox.Show("Guess another number.")
			   
				Else
					MessageBox.Show("You didn't guess correctly. Want to try again?")
					GetRandomNum()
				  
				End If
else

If guess = number then

correctcounter += 1
MsgBox("Congratulations! You guessed correctly.")
			End If


I'm getting errors doing it that way. Its only going through the loop once.
Was This Post Helpful? 0
  • +
  • -

#15 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: Random Number Game

Posted 28 June 2009 - 12:44 PM

View Postsoutherngenes, on 28 Jun, 2009 - 08:30 AM, said:

Ok. I have it somewhat working; however, think there's something wrong with my IF...THEN statements because it will tell me to enter a lower/higher number, which may be correct the first time through the loop, but then it doesn't tell me the correct thing. For example: the number is 8, but I guess 5, the first time it will say to enter a higher number so I enter 7, but then it will say to enter a lower number. Here's the code:
Public Class Form1

	Dim randomGenerator As New Random
	Dim number As Integer
	Dim correctcounter As Integer = 0
	Dim totalcounter As Integer = 0

	Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

		Me.Close()

	End Sub

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

		
	End Sub

	Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click

		'declare variables
		Dim guess As Integer
		Dim excheck As Exception
		Dim strErrorMsg As String
		
		'local variable initialization
		excheck = Nothing
		strErrorMsg = ""
		guess = 0

		Try

			'assign random number
			Integer.TryParse(Me.guessTextBox.Text, guess)
		number = randomGenerator.Nex(1,11) ‘ numbers 1-10

'check if its a integer in guessTextBox
			If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
				'trap the error
				strErrorMsg = "Enter a number."
				Me.guessTextBox.Text = ""
				Me.guessTextBox.Focus()
			End If

			
			If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
			Else
				totalcounter += 1
				If totalcounter < 5 Then ‘5 chances 
				  If guess < number then
				MsgBox(“Guess a higher number.”)
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				Else
				MsgBox(“Guess a lower number.”) 
				Me.guessTextBox.Text = “”
				Me.guessTextBox.Focus()  
				  End if
		Else
 MsgBox("You didn't guess correctly. The number is:  " & number)
					GetRandomNum()
				End If
			End If

			'reset the text box
			guessTextBox.Text = ""
			guessTextBox.Focus()

		Catch excheck
			If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _  MessageBoxIcon.Exclamation)
			End If
		End Try

		'clean up variables
		excheck = Nothing
		strErrorMsg = Nothing
		guess = Nothing

	End Sub

	Sub GetRandomNum()
		  Debug.Print("Number is {0}", number) 'test to see #
		totalcounter = 0
	 End Sub

	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		GetRandomNum()
		'initialize the random number. 
		Dim randomgenerator As New Random
		number = randomgenerator.Next(1, 11) ‘numbers 1 through 10 
				 GetRandomNum()
  totalcounter = 0
		guessTextBox.Focus()
	End Sub

	Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
		'selects the existing text
		Me.guessTextBox.SelectAll()
	End Sub

	Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
		'allows only numbers and the backspace key
		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
			AndAlso e.KeyChar <> ControlChars.Back Then
			e.Handled = True
		End If
	End Sub




That could should actually work, your main problem is this here in your CheckGuessBtn_Click handler:
			Integer.TryParse(Me.guessTextBox.Text, guess)
			number = randomGenerator.Next(1, 11) ' numbers 1-10

You are generating a new random number every time this button is pressed. The information that you are getting about your number being higher or lower is irrelevant, because every time you go to click that button to check the guess an entirely different number is being generated. Take that line of code out of there and just make sure you click your New Game button to generate the number. You could then raise your newGame_Click event automatically after a correct number is guessed or the number of chances runs out.
So your Try...Catch block remains the same, except the line where you assign a new number is removed and you can optionally start a new game automatically.

 Try

			'You don't want to assign a new random number here
			Integer.TryParse(Me.guessTextBox.Text, guess)

			'check if its a integer in guessTextBox
			If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
				'trap the error
				strErrorMsg = "Enter a number."
				Me.guessTextBox.Text = ""
				Me.guessTextBox.Focus()
			End If


			If guess = number Then
				correctcounter += 1
				MsgBox("Congratulations! You guessed correctly.")
				newGame.PerformClick() 'Start a new game automatically
			Else
				totalcounter += 1
				If totalcounter < 5 Then '5 chances
					If guess < number Then
						MsgBox("Guess a higher number.")
						Me.guessTextBox.Text = ""
						Me.guessTextBox.Focus()
					Else
						MsgBox("Guess a lower number.")
						Me.guessTextBox.Text = ""
						Me.guessTextBox.Focus()
					End If
				Else
					MsgBox("You didn't guess correctly. The number is:  " & number)
					GetRandomNum()
					newGame.PerformClick() 'Start a new game automatically
				End If
			End If

			'reset the text box
			guessTextBox.Text = ""
			guessTextBox.Focus()

		Catch excheck
			If strErrorMsg.Length = 0 Then
				MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			End If
		End Try


Also, you have already declared "randomGenerator" at the top of the form and do not need to do so again in your newGame_Click handler, you can just reinitialize it. If you don't want 0 to be one of your numbers you can make sure it is not within a Do...Loop Until statement, you could also use this to make sure you don't get the same number again.
This would look like:
	Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
		MsgBox("A new number has been selected for you to guess.")
		GetRandomNum()
		'initialize the random number.
		'Dim randomgenerator As New Random
		randomGenerator = New Random
		Do
			number = randomGenerator.Next(1, 11) 'numbers 1 through 10
		Loop Until (number <> 0 And number <> numberHistory)
		numberHistory = number
		GetRandomNum()
		totalcounter = 0
		guessTextBox.Focus()
	End Sub

Just make sure you declare another integer variable named "numberHistory" at the top of your class.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2