Mostrando las entradas con la etiqueta Randomize. Mostrar todas las entradas
Mostrando las entradas con la etiqueta Randomize. Mostrar todas las entradas

martes, 15 de septiembre de 2015

Número aleatorio (ejemplo de como usar report)



El componente report no es fácil de usar, o mejor dicho de entender como funciona al menos para mi, estoy en campaña de aprender a usarlo y como siempre busco hacer cosas supersuper simples pues así se entiende.
Este ejemplo hace un reporte de un numero aleatorio.

Primero que nada creamos un proyecto nuevo y en el formulario le agregamos un botón, después vamos a componentes y agregamos gb.report2.

Creamos un nueva clase tipo report como lo muestra la imagen de abajo.


Este código lo pegamos en el formulario principal por defecto de llama FMain


' gambas class file


Public Sub Form_Open()
Me.Center
End

Public Sub Button1_Click()
Dim reportTitulo As New Report1 'aquí declaramos la clase de tipo Report1,
'que es la clase que creamos anteriormente que es un formulario especial con algunos elementos
'como label, pictures, algunos contenedores y algo mas.

Report1.Preview() ' aquí ordenamos ejecutar el formulario especial (from de report) para poder visualizar los resultados.
'Todo lo que queramos hacer o que se muestre en el reporte tenemos que hacerlo dentro de la clase Report1.
End



Este código lo pegamos en la clase report que tendrá como nombre predeterminado Report1.


' gambas class file

Public Sub Report_Open()
Dim Xnumero As Integer

Randomize
Xnumero = Rand(0, 1000)

ReportLabel2.Text = Xnumero

End


Resultado del reporte.



Bueno espero que les ayude a iniciarse a usar este complemento llamado gb.report2, saludos.

Reportes-0.0.1.tar.gz


Ejemplo 2 (variación)

' gambas class file


Public Sub Form_Open()
Me.Center
End

Public Sub Button1_Click()
Dim reportTitulo As Report1
Dim Xnumero As Integer

Randomize
Xnumero = Rand(0, 1000)

Print Xnumero

reportTitulo = New Report1(Xnumero)

reportTitulo.Preview()

End
--------------------------------------------------------------------
' gambas class file

Public Sub _new(EsteNumero As Integer)
ReportLabel2.Text = EsteNumero
End

Public Sub Report_Open()

End

Reportes-0.0.2.tar.gz


Ejemplo 3(complicando un poco ja)




FMain:


' gambas class file


Public Sub Form_Open()
Me.Center
End

Public Sub Button1_Click()
Dim ReportNumeros As Report1

ReportNumeros = New Report1(TextBox1.Text)
ReportNumeros.Preview()

End





Clase Report1:

' gambas class file

Private Xcantidad As Integer


Public Sub _new(cantidad As Integer)
Xcantidad = cantidad
End

Public Sub Report_Open()
Dim XNumeros As Integer[]
Dim a, Xnum As Integer
Dim XReportLabel As ReportLabel

Randomize

For a = 1 To Xcantidad

Xnum = Rand(1, 1000)

Print Xnum

XReportLabel = New ReportLabel(RVBox)
XReportLabel.Text = Xnum
XReportLabel.Visible = True
XReportLabel.Font.Size = 20
XReportLabel.Font.Bold = True
XReportLabel.Alignment = Align.Center

Next

End







MontonNumeros-0.0.1.tar.gz


Ejemplo 4 (report de texto de un TextArea) 





Fmain:



' gambas class file

Public Sub Form_Open()
Me.Center
End

Public Sub Button1_Click()
Dim ReportNotas As Report1
Dim TextoFormateado As String
Dim Partes As String[]

Partes = Split(TextArea1.Text, gb.NewLine)

ReportNotas = New Report1(Partes)
ReportNotas.Preview()

End





Report1

' gambas class file

Private Xtexto As String[]


Public Sub _new(parrafos As String[])
Xtexto = parrafos
End


Public Sub Report_Open()
Dim EsteParrafo As String
Dim LabelParrafo As ReportLabel

For Each EsteParrafo In Xtexto
LabelParrafo = New ReportLabel(RVBox)
LabelParrafo.Visible = True
LabelParrafo.Text = EsteParrafo
Next

End


ReportTextArea-0.0.1.tar.gz

lunes, 27 de octubre de 2014

Sgn



' gambas class file

' by postapase

Public Sub Button1_Click()
Dim num As Integer

Randomize

num = Rnd(-1000, 1000) 'Rnd elige un numero aleatorio entre -1000 y 1000
'y lo guarda en la variable de tipo integer num

If Sgn(num) = 1 Then 'Sgn analiza el dato integer o numerico dentro de num
'y devuele 1 si el número es positivo o si es negativo devuelve -1
TextArea1.Text &= "El número " & num & " es un número positivo ( " & Sgn(num) & " )" & gb.NewLine
Else
TextArea1.Text &= "El número " & num & " es un número negativo ( " & Sgn(num) & " )" & gb.NewLine
Endif

End
 

domingo, 7 de septiembre de 2014

Contar números pares e impares



Usando MOD


' Gambas class file

'by postapase

Public Sub Form_Open()
Dim x, Num, dato, dato1, par, impar As Integer
Dim Numeros As New Integer[]
Dim todos As String

For x = 0 To 10
    Randomize
    Wait 0.1
    Num = Rnd(0, 100)
    Numeros.Add(Num)
Next

For Each dato In Numeros
   If dato Mod 2 = 0 Then
     Inc par
   Else
     Inc impar
   Endif
Next

For Each dato1 In Numeros
  todos &= dato1 & ","
Next

Print Left$(todos, Len(todos) - 1)
Print "Impares= " & impar
Print "Pares= " & par

End

domingo, 9 de junio de 2013

AdivinarNúmero v0.0.1


Mas vale tarde que nunca.... este es un clásico programa donde el programa elije un número y el usuario intenta descubrirlo con 10 posibilidades, el programa nos da la única pista que consiste en decir si el número que tenemos que descubrir es menor o mayor. Le agregue mas dificultad por medio de niveles,en el nivel 1 el programa elige entre 1 y 50,en el nivel dos de 1 a 200, nivel 3 de 1 a 500, nivel 4 de 1 a 700 y el nivel mas difícil de 1 a mil.
Tiene una lista de usuarios que acertaron y los ordena de mayor a menor(bueno ese detalle todavía no lo resuelvo, espero que a ustedes se les ocurra una brillante idea y la compartan) donde los puntos se dan asi: 1000 porque juega en el nivel 5, 700 porque juega en el nivel 4 y asi sucesivamente, agregando a esas cifras el número de intentos que no se uso. Ejemplo: postapase  juega en el nivel 5 y acierta en el primer intento son 10 puntos mas (1010), otro usuario juega en el nivel 3 y acierta en el 5 intento, los punto son 505 y así con lo demás. Bueno algo es algo :)


Fmain:

' Gambas class file

Public espandir As Boolean = False
Public offniveles As Boolean = True
Public Nmagico As Float
Public Ningresado As Integer
Public intentos As Integer

Public puntaje As Integer
Public nomJugador As String
Public Const N1 As Integer = 50
Public Const N2 As Integer = 200
Public Const N3 As Integer = 500
Public Const N4 As Integer = 700
Public Const N5 As Integer = 1000
Public nivelelegido As Integer


Public Sub Form_Open()
  ' exxpandir(False)
  Me.Center
  Me.Width = 210
  ValueBox1.Enabled = False
  nivelelegido = N1
  nivel1.Checked = True
  intentos = 10
  Labintentos.Visible = False
  offniveles = False
  End

Public Function nivelesNO(offniveles As Boolean) As Boolean
  If offniveles = True Then
   nivel1.Enabled = False
   nivel2.Enabled = False
   nivel3.Enabled = False
   nivel4.Enabled = False
   nivel5.Enabled = False
  Else
   nivel1.Enabled = True
   nivel2.Enabled = True
   nivel3.Enabled = True
   nivel4.Enabled = True
   nivel5.Enabled = True
  Endif
    Return
End

Public Sub btnborrar_Click()
 ValueBox1.Value = 0
  ValueBox1.SetFocus
End


Public Sub btnVer_Click()

  Dim agrandar, achicar As Integer
  If espandir = True Then
    For achicar = 595 To 210 Step -1
      FMain.Width = achicar
      Wait 0.00049
    Next
  espandir = False
  btnVer.Text = "V\nE\nR\n" "\nR\nE\nC\nO\nR\nE\nS"
  Else
    For agrandar = 210 To 595 Step 1
      FMain.Width = agrandar
      Wait 0.00049
    Next
    espandir = True
    btnVer.Text = "O\nC\nU\nL\nT\nA\nR"
  Endif
End

Public Sub btnSalir_Click()
  Me.Close
End

Public Sub btnComenzar_Click()
Dim achicar As Integer
Labinfo.Text = ""
Labintentos.Text = ""


If espandir = True Then
    For achicar = 595 To 210 Step -1
      FMain.Width = achicar
      Wait 0.00049
    Next
  espandir = False
  btnVer.Text = "V\nE\nR\n" "\nR\nE\nC\nO\nR\nE\nS"
  usuario.Show
  Else
   btnVer.Enabled = False
 Labintentos.Caption = ""
Labinfo.Caption = ""
usuario.Show
intentos = 10

  Randomize
  Nmagico = Int(Rnd(1, nivelelegido))
 ValueBox1.SetFocus
   Print Nmagico
   Print nivelelegido
    Endif


End

Public Sub btnIngresar_Click()
Dim level As Integer
If ValueBox1.Text = 0 Then
  Message.Info("El numero mínimo es 1")
  ValueBox1.Value = 1
  Else
  

If ValueBox1.Text = Null Then
  Message.Info("ingrese un número para continuar")
  ValueBox1.Text = ""
  ValueBox1.SetFocus
  Return
  Else
Labintentos.Caption = nomJugador & "\nTe quedan " & intentos & "\n restantes para adivinar\n el número Magico"
Ningresado = ValueBox1.Text

If Ningresado = " " Then
  Message.Info("Ingrese un numero")
  Else
    If intentos = 1 Then
       Labinfo.Caption = "Has perdido"
       Labintentos.Caption = nomJugador & "\n suerte para\n el próximo juego"
      ValueBox1.Text = 0
     
       btnborrar.Enabled = False
       btnIngresar.Enabled = False
      ValueBox1.Enabled = False
      btnComenzar.Enabled = True
      nivelesNO(False)
    
   Else
     If Ningresado > nivelelegido Then
       Select Case nivelelegido
         Case 50
         level = 1
         Case 200
         level = 2
         Case 500
         level = 3
         Case 700
         level = 4
         Case 1000
         level = 5
        End Select
       Message.Info("Esta jugando en nivel: " & level & "\n" & "eliga un número menor o igual a " & nivelelegido)
       ValueBox1.Text = ""
       ValueBox1.SetFocus
      Else

        If Ningresado >= 1001 Then
           Message.Info("Elija un número entre 1 y 1000")
        Else
        
          Select Case Ningresado
             Case Nmagico
               Labintentos.Caption = "FELICITACIONES \n HAS ACERTADO!!!"
             Labinfo.Caption = "GANASTE!!!"
             puntaje = nivelelegido + intentos
            ListBox1.Add(puntaje & " <= " & nomJugador & " => " & Date)
            ListBox1.Sorted = True
            btnVer.Enabled = True
             offniveles = False
             nivelesNO(offniveles)
             ValueBox1.Value = 0
            ValueBox1.Enabled = False
             btnIngresar.Enabled = False
             btnComenzar.Enabled = True
             btnborrar.Enabled = False
            
             Case Else
           If Ningresado > Nmagico Then
             Labinfo.Caption = "numero menor"
            Dec intentos
            Labintentos.Caption = nomJugador & "\nTe quedan " & intentos & "\n restantes para adivinar\n el número Magico"
          Else
            Labinfo.Caption = "numero mayor"
             Dec intentos
            Labintentos.Caption = nomJugador & "\nTe quedan " & intentos & "\n restantes para adivinar\n el número Magico"
            Labintentos.Refresh
           
         Endif
        End Select
        Endif
     Endif
  Endif
Endif
Endif
Endif
Print intentos
End


Public Sub Form_KeyRelease()
  If ValueBox1.Text <> Null Then
    btnIngresar.Enabled = True
    Else
      Return
  Endif
End

Public Sub nivel3_Click()
  nivelelegido = N3
  btnNivel.Text = "N3"
  nivel3.Checked = True
  nivel2.Checked = False
  nivel1.Checked = False
  nivel4.Checked = False
  nivel5.Checked = False
End

Public Sub nivel1_Click()
  nivelelegido = N1
  btnNivel.Text = "N1"
  nivel1.Checked = True
  nivel2.Checked = False
  nivel3.Checked = False
  nivel4.Checked = False
  nivel5.Checked = False
End

Public Sub nivel2_Click()
  nivelelegido = N2
  btnNivel.Text = "N2"
  nivel2.Checked = True
  nivel1.Checked = False
  nivel3.Checked = False
  nivel4.Checked = False
  nivel5.Checked = False
End

Public Sub nivel4_Click()
  nivelelegido = N4
  btnNivel.Text = "N4"
  nivel4.Checked = True
  nivel2.Checked = False
  nivel3.Checked = False
  nivel1.Checked = False
  nivel5.Checked = False
End

Public Sub nivel5_Click()
   nivelelegido = N5
   btnNivel.Text = "N5"
   nivel5.Checked = True
   nivel2.Checked = False
   nivel3.Checked = False
   nivel4.Checked = False
   nivel1.Checked = False
End

Public Sub salir_Click()

  Me.Close

End

Public Sub acercade_Click()

  info.Show

End



usuario:
' Gambas class file


Public Sub btnAceptar_Click()
If Txtnombre.Text = Null Then
  Message.Info("Ingrese su nombre para continuar")
  Return
  Else
   FMain.intentos = 10    ' agregue esta línea pues mostraba los intentos que restaban al jugador anterior  :oops:
    FMain.btnborrar.Enabled = True
    FMain.Enabled = True
    FMain.nomJugador = UCase$(Txtnombre.Text)
   
    FMain.Labintentos.Visible = True
    FMain.Labintentos.Caption = FMain.nomJugador & "\nTe quedan " & FMain.intentos & "\n restantes para adivinar\n el número Magico"
    FMain.btnComenzar.Enabled = False
    FMain.offniveles = True
    FMain.nivelesNO(FMain.offniveles)
Endif
 FMain.ValueBox1.Enabled = True
 FMain.ValueBox1.SetFocus
 FMain.btnIngresar.Enabled = True
 Me.Close
End

Public Sub Form_Open()
  FMain.Enabled = False
  Me.Center
  Me.Title = "Ingrese su nombre"
End

Public Sub btnCancelar_Click()
  Me.Close
  FMain.Enabled = True
  FMain.btnVer.Enabled = False
End



info:
' Gambas class file

Public Sub Form_Open()
 Me.Center
End

Public Sub Form_Leave()
  Me.Close
End



Código fuente:    AdivinarNumero-0.0.1.tar.gz
                  
Le agregue una lista de los numeros ingresados,
guarda recores en un archivo *.lst
sigue con errores pero.... se lo dejo para que practiquen modificaciones o agregados
 AdivinarNumero-0.0.2.tar.gz

AdivinarNumero_v0.0.3.tar.gz