Hola Comunidad:
Bueno he estado aprendiendo Vb por mi cuenta asi que vi que una de las formas es estudiando los codigos y ver el trabajo de otros. asi que me descargue el source en VB 6 del International Lotto Uk, pero No entiendo la ultima parte me gustaria saber si alguien podria hecharme una mano.
Source
Option Explicit
Dim iBallsToMake As Integer
Private Sub Form_Load()
' Initialize random procedure:
Randomize
End Sub
Private Function Get_RANDOM(lowerbound As Integer, upperbound As Integer) As Integer
' The RND formula!
' Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Get_RANDOM = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Function
Private Sub lblURL_Click(Index As Integer)
Dim sURL As String
sURL = Trim(lblURL(Index).Caption)
Shell "explorer " & sURL, vbMaximizedFocus
End Sub
Private Sub cmdMake_Click()
' Disable button and properties:
cmdMake.Enabled = False
txtBallCount.Enabled = False
txtMinBall.Enabled = False
txtMaxBall.Enabled = False
Dim i As Integer
' Hide all balls and set labels to "77":
For i = imgBall.LBound To imgBall.UBound
imgBall(i).Visible = False
lblNum(i).Visible = False
lblNum(i).Caption = "77"
Next i
' Get the number of balls to make:
iBallsToMake = Val(txtBallCount.Text)
' Check if number is between 1 and 7:
If iBallsToMake > 7 Then
iBallsToMake = 7
txtBallCount.Text = "7"
ElseIf iBallsToMake <= 0 Then
iBallsToMake = 1
txtBallCount.Text = "1"
End If
' Start the process:
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
' Are there balls to make?
If iBallsToMake <= 0 Then
' Stop timer:
Timer1.Enabled = False
' Enable properties:
cmdMake.Enabled = True
txtBallCount.Enabled = True
txtMinBall.Enabled = True
txtMaxBall.Enabled = True
Exit Sub ' Done!
End If
' Make ball visible:
imgBall(7 - iBallsToMake).Visible = True
lblNum(7 - iBallsToMake).Visible = True
Dim iNewNum As Integer
Dim i As Integer
Dim iTryAgainCounter As Integer
iTryAgainCounter = 0
try_again:
' Avoid hand up with max ball 2 and 7 balls :)
If iTryAgainCounter > 100 Then
' Set the ball value:
lblNum(7 - iBallsToMake).Caption = "?"
GoTo one_less
End If
' Get the random value:
iNewNum = Get_RANDOM(Val(txtMinBall.Text), Val(txtMaxBall.Text))
' Avoid falling the same number again:
For i = imgBall.LBound To imgBall.UBound
If Val(lblNum(i).Caption) = iNewNum Then
iTryAgainCounter = iTryAgainCounter + 1
GoTo try_again
End If
Next i
' Set the ball value:
lblNum(7 - iBallsToMake).Caption = iNewNum
one_less:
' Ok, one ball is there!
iBallsToMake = iBallsToMake - 1
End Sub
-----------------------------------------------------------------------------------------------
Duda:
Bueno el problema es el siguiente, supuestamente en el codigo con variable iballstomake es ekivalente al numero de bolas que se va a caer al azar, entonces
' Make ball visible:
imgBall(7 - iBallsToMake).Visible = True
lblNum(7 - iBallsToMake).Visible = True
Esto solo haria aparecer a la primera bola y no las 7 que supuestamente deberia estar alli.
Es mas otra cosa que no entiendo, porque pone esta condicion si itryagaincounter es dificil que sea mayor a 100 x lo que nunka se cumpliria.....
If iTryAgainCounter > 100 Then
' Set the ball value:
lblNum(7 - iBallsToMake).Caption = "?"
GoTo one_less
End If
La verdad estoy con muchas ganas d aprender sin embargo creo... que necesitare de ayuda...
Espero que haya sido claro con mi explicacion.
Salu2
' Make ball visible:
imgBall(7 - iBallsToMake).Visible = True
lblNum(7 - iBallsToMake).Visible = True
La variable iBallsToMake es absesible en todos los procesos y como esta en un timer, este al cumplir las 7 rondas mostrara de 1 en una es decir asi
6
65
654
6543
65432
6543210
PORQUE? por que al activar el proces cmdMake_Click se establece iBallsToMake = Val(txtBallCount.Text) y despues activa el timer, despues en este hace lo que ya te mencione y cuando revisa que iBallsToMake <= 0 se desactiva el timer y termina su siclo.
ahora para esto otro:
If iTryAgainCounter > 100 Then
' Set the ball value:
lblNum(7 - iBallsToMake).Caption = "?"
GoTo one_less
End If
Al entrar en el proceso timer se establece cada vez a iTryAgainCounter =0 pero como es posible que llegue a 100? sencillo
si se cumple
Val(lblNum(i).Caption) = iNewNum
Es decir el valor de lbl(i)Num = iNewNum se le sumara uno YYY retrocese a la marca/etiqueta try_again
Dim iNewNum As Integer
Dim i As Integer
Dim iTryAgainCounter As Integer
iTryAgainCounter = 0
try_again: '<-------Aquì
' Avoid hand up with max ball 2 and 7 balls
P.D.: si me equivoque en alguna explicaciòn disculpa pero tengo sueo Y NO USAS GESHI para simplificar la Lectura del CODIGO.
Usa:
[code=vb ]
Codeigo Visual Basic 6
[/code ]
Obviamente sin espacios en las etiquetas
P.S.: disculpa si no lo expliue bien o me salte unas cosas pero son mas que deducibles si revisas bien XP, ademàs de que tengo sueño y ya no pienso bien ahorita >:(
Dulces Lunas!¡.
[/code]
Hola BlackZero
Gracias x la pronta respuesta, luego d analizar cuidadosamente los codigos pude despejar mis dudas. Sin embargo, aun me keda algo de intriga.... bueno pues yo he hecho un generador de numeros, todo esta "bien" al parecer pero el problema es que cada vez q abro el programa me genera los mismos numeros. Es decir supuestamente la funcion Rnd genera numeros aleatorios pero en realidad no es asi. Ejemplo: Al abrir el programa y Clickeo Generar. ME sale Siempre:
1. 3 0 2 3 1
2. 1 3 0 1 8
3. 9 8 3 4 5
Es un ejemplo. Es por eso que me he detenido en analizar este codigo ya que en este no me da los mismos numeros cada vez q inicio el programa.
si solo pasale Randomize
Aquì te dejo la funcion de Random la funcion correcta:
Private Function RANDOM(RangoInicial As Long, RangoFinal As Long) As Long
Randomize Rnd
' // Intercambiamos los valores por si no se insertaron correctamente
If RangoInicial > RangoFinal Then
Dim tmp As Long
tmp = RangoInicial
RangoInicial = RangoFinal
RangoFinal = tmp
End If
' // Fin
RANDOM = (RangoFinal - RangoInicial + 1) * Rnd + RangoInicial
End Function
Solo pasale
El numero inicial y el numero final con eso te generara los numeros aleatoriso en dicho rango, si te llegas a equivocar la funcion lo arreglara por ti
Si es mucho aquì te dijo l version reducida:
Round(Rnd * #5) ' // Cambia el 5 Por el numero deseado tomara un rango de 0 a 5
' // Si deseas numeros desimales quita Round()
' // Inicia siempre con Randomize () antes de usar Rnd
Dulces Lunas!¡.
Ya vi el problema vroer era solo agregar otra function como dijiste q era la randomize al momento de cargar el programa.... Se t agradece x tu aporte..
Y sigue asi .. GRacias
Salu2