4 Replies - 971 Views - Last Post: 28 November 2009 - 12:54 AM Rate Topic: -----

#1 abhishek99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-November 09

Guess numbers & Win

Posted 26 November 2009 - 09:39 PM

Public Class Form1
	Dim Randomizer As New Random
	Dim Numbers As List(Of Integer)
	Dim Number1 As Integer
	Dim Number2 As Integer
	Dim Number3 As Integer
	Dim Number4 As Integer
	Dim Number5 As Integer
	Dim Number6 As Integer
	Dim txtNumbers As List(Of Integer)
	Dim txtNumber1 As Integer
	Dim txtNumber2 As Integer
	Dim txtNumber3 As Integer
	Dim txtNumber4 As Integer
	Dim txtNumber5 As Integer
	Dim txtNumber6 As Integer
	Dim Guess11 As Integer = 0
	Dim Guess1 As Integer
	Dim Guess2 As Integer
	Dim Guess3 As Integer
	Dim Guess4 As Integer
	Dim Guess5 As Integer
	Dim Guess As Integer

	Public Sub Naming()

		Number1 = Randomizer.Next(20)
		Number2 = Randomizer.Next(20)
		Number3 = Randomizer.Next(20)
		Number4 = Randomizer.Next(20)
		Number5 = Randomizer.Next(20)
		Number6 = Randomizer.Next(20)

		txtNumber1 = Val(txt1.Text)
		txtNumber2 = Val(txt2.Text)
		txtNumber3 = Val(txt3.Text)
		txtNumber4 = Val(txt4.Text)
		txtNumber5 = Val(txt5.Text)
		txtNumber6 = Val(txt6.Text)

	End Sub

	Public Sub Final()
		Naming()

		If Number1 = txtNumber1 Then
			Guess1 = Guess11 + 1
		Else
			Guess1 = Guess11
		End If

		If Number2 = txtNumber2 Then
			Guess2 = Guess1 + 1
		Else
			Guess2 = Guess1
		End If

		If Number3 = txtNumber3 Then
			Guess3 = Guess2 + 1
		Else
			Guess3 = Guess2
		End If

		If Number4 = txtNumber4 Then
			Guess4 = Guess3 + 1
		Else
			Guess4 = Guess3
		End If

		If Number5 = txtNumber5 Then
			Guess5 = Guess4 + 1
		Else
			Guess5 = Guess4
		End If

		If Number6 = txtNumber6 Then
			Guess = Guess5 + 1
		Else
			Guess = Guess5
		End If

		Label1.Text = Number1
		Label2.Text = Number2
		Label3.Text = Number3
		Label4.Text = Number4
		Label5.Text = Number5
		Label6.Text = Number6
	End Sub

	Private Sub cmdGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGO.Click
		Final()
		lblMessage.Text = "You had " & Guess & " guesses.."
	End Sub
End Class



I have to make a game in Visual Basic as a part of a school project. The application makes the user input 6 numbers. It then randomly picks numbers and then compares them with the 6 numbers. It arranges both of them in ascending order. The randomly selected numbers are checked for any repetition. If the user gets 1 right then he makes $2, if he gets 2 right then $20, 3 then $60, 4 then $100, 5 then $200 and if he gets 6 right then he makes $500. To start a game user should pay $ 50. He gets $100 when the game starts. When the user runs out of money then his game ends and he enters his name in high score.
I have coded a part of that. I am stuck. I just want ideas what I can do now!! I will then code it on my own..Please its urgent!! I am stuck here.

Is This A Good Question/Topic? 0
  • +

Replies To: Guess numbers & Win

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Guess numbers & Win

Posted 26 November 2009 - 10:29 PM

Ok, any time you see your code doing something like this...

Dim Number1 As Integer
Dim Number2 As Integer
Dim Number3 As Integer
Dim Number4 As Integer
Dim Number5 As Integer
Dim Number6 As Integer
...



Then you should immediately think "I need an array to hold these numbers." Two arrays will make things a lot easier for you in this project and also cut your code down drastically. You will then use a loop to load the first array with input the user enters. Then the second array will be loaded with a loop that generates the random numbers. Then you can use the Array object to sort each of them. Then of course since they are now the same length (the term is called parallel arrays) you can simply loop through them at the same time and compare each value.

Just imagine if they asked you to enter 500 numbers. Your code would grow crazy! So I strongly suggest you switch over to arrays for this. I will get you started with the array that generates random numbers...


' Declare an array of 6 numbers (5 is highest subscript)
Dim rndNumbers(5) As Integer

' Our random number
Dim rnd As New Random

' We loop 6 times, loading each array spot with a random number 1 to 10.
For i As Integer = 0 To 5
	 rndNumbers(i) = rnd.Next(1, 11)
Next

' Here we just print out the list to show that the numbers are loaded and that there are 6 random numbers
For Each num As Integer In rndNumbers
	 MessageBox.Show(num.ToString())
Next



So now you have an array of six random numbers called "rndNumbers" and now do another for the user input values and use rndNumbers and the other array for your comparisons. Hope you get the idea. Good luck!


"At DIC we be parallel array loving code ninjas... ok, our love is not that strong otherwise we would say our love is 'unparalleled'" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 abhishek99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-November 09

Re: Guess numbers & Win

Posted 27 November 2009 - 12:22 AM

How do you compare values of an array to an individual value in another array.
Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Guess numbers & Win

Posted 27 November 2009 - 07:23 AM

The secret ninja magic For loop.
For each value As Integer in myArray
  If value = whatIWant Then
	'do secret ninja magic
  End If
End For


Was This Post Helpful? 1
  • +
  • -

#5 abhishek99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-November 09

Re: Guess numbers & Win

Posted 28 November 2009 - 12:54 AM

Its really difficult. Please help me??I have tried it for the past two days but I am not getting any success. Please help!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1