Página 1 de 1

AYUDA URGENTE COMO QUITAR LOS DOS 10 QUE ME SALEN ???AYUDA

Publicado: Lun Mar 12, 2007 5:09 pm
por INTER
Pues tengo una duda mañana tengo que entregar sin falta el sudoku y el problema es que cuando pulso el boton crear siempre me sale dos 10 aleatorio pero solo 2 y no se que hacer como quitarlos? alguien tiene un poquito de tiempo para ayudarme?

aqui teneis el codigo:
--------------------------------------------------------------------------
Private Sub cmdGenerar_Click()

CreateSudokuGrid

Dim i As Integer

For i = 1 To 81

Text1(i).Enabled = True
Text1(i).ForeColor = vbBlack
Text1(i).Enabled = False

Next i

End Sub
---------------------------------------------------------------------
Private Sub CreateSudokuGrid()

Dim b, i, j, k As Integer
Dim numRand As Integer
Dim ValIsOK(0 To 9) As Boolean
Dim valsOK As Integer

Do

For i = 1 To 81
Text1(i).Text = ""
Next i

For i = 1 To 81
If GetValidVals(i, ValIsOK()) = True Then
Randomize

Do
numRand = Int(10 * Rnd)
If ValIsOK(numRand) = True Then Exit Do
Loop

Text1(i) = numRand

Else

Text1(i) = "10"
End If
Next i
If isValid() Then Exit Do
Loop

For i = 1 To 81
Text1(i).Enabled = True
numRand = Rnd(CInt(3) + 1) + 1
If numRand > 1 Then

Text1(i).BackColor = Gris
Text1(i).ForeColor = vbBlack
Text1(i).Enabled = False
Else

Text1(i).BackColor = Blanco
Text1(i).ForeColor = Blanco

End If

Me.Refresh

Next i

End Sub
Private Function isValid()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim s(0 To 10) As Boolean

For i = 1 To 9
For j = 0 To 10: s(j) = False: Next j
For j = 1 To 9

k = (i - 1) * 9 + j
If s(Val(Me.Text1(k).Text)) = True Then
isValid = False: Exit Function
Else

s(Val(Me.Text1(k).Text)) = True

End If

Next j

Next i

For i = 1 To 9
For j = 0 To 10: s(j) = False: Next j
For j = 1 To 9

k = (j - 1) * 9 + i
If s(Val(Me.Text1(k).Text)) = True Then
isValid = False: Exit Function
Else

s(Val(Me.Text1(k).Text)) = True

End If

Next j

Next i

isValid = True

End Function
---------------------------------------------------------------------------------------
Private Function GetValidVals(ByVal Location As Integer, ByRef ValidVals() As Boolean) As Boolean

Dim i, j, k As Integer
Dim row, col As Integer
Dim numStr As String
Dim numOK As Boolean
Dim retVal As Boolean

For i = 1 To 9: ValidVals(i) = True: Next i

For i = 1 To 9

numStr = CStr(i)

For j = 1 To 9
k = (Int((Location - 1) / 9) * 9) + j
If Text1(k).Text = numStr Then ValidVals(i) = False
Next j

For j = 1 To 9
k = (((Location - 1) Mod 9) + 1) + ((j - 1) * 9)
If Text1(k).Text = numStr Then ValidVals(i) = False

Next j
Next i

retVal = False
For i = 1 To 9
retVal = IIf(ValidVals(i) = True, True, retVal)

Next i

GetValidVals = retVal

End Function
---------------------------------------------------------------------------------------

Re: AYUDA URGENTE COMO QUITAR LOS DOS 10 QUE ME SALEN ???AYU

Publicado: Lun Mar 12, 2007 6:02 pm
por lo_jorge
No estoy siguiendo fielmente todas las instrucciones, pues es algo que tu debes conocer mejor que yo. Pero aqui te tengo unas observaciones para que busques el problema. Si el código lo hiciste tu solo, entonces te será fácil hallar la respuesta, pero si es copiado de algún lado, entoences haz UN SOLO CAMBIO A LA VEZ:
________________________________________________________________
INTER escribió:Dim ValIsOK(0 To 9) As Boolean
Por qué dimensionas de 0 a 10 cuando los números van de 1 a 9?
________________________________________________________________
INTER escribió:numRand = Int(10 * Rnd)
Prueba esta sentencia cual es el valor máximo y el mínimo
________________________________________________________________
INTER escribió:If ValIsOK(numRand) = True Then Exit Do Loop
Text1(i) = numRand
Else
Text1(i) = "10"
End If
Ottro grupo de ordenes sospechoso de ser incoherente. Si es 0 reemplazas por 10? Mejor tu random lo haces de 0 a 8, y le sumas 1, asi va ir de 1 a 9, y mejor si le agregas el integro para que de una vez el random te salga los numero sque quieres
________________________________________________________________
INTER escribió:numRand = Rnd(CInt(3) + 1) + 1
Esta sentencia tambien la debes analizar, cual es el valor máximo y mínimo
________________________________________________________________
INTER escribió:Dim s(0 To 10) As Boolean
Otra vez el valor 10 cuando yo creo que debe ir de 1 a 9
________________________________________________________________
INTER escribió:For j = 0 To 10: s(j) = False: Next j
Piensa si esto está bien
________________________________________________________________
INTER escribió:k = (i - 1) * 9 + j
Explicame esto
________________________________________________________________
INTER escribió:For j = 0 To 10: s(j) = False: Next j
Estas seguro que es de 0 a 10?
________________________________________________________________
INTER escribió:k = (Int((Location - 1) / 9) * 9) + j
INTER escribió:k = (((Location - 1) Mod 9) + 1) + ((j - 1) * 9)
Lo mismo de siempre valores máximos y mínimos
________________________________________________________________
Espero esté en lo correcto en mis observaciones, como te digo, en este momento no tengo instalado ningún intérprete de lenguaje para decirte que hacer.

Suerte...

Publicado: Lun Mar 12, 2007 7:36 pm
por Invitado
gracias por contestar y ayudarme

1)a la primera pregunta ya la he cambiado por un;

Dim ValIsOK(0 To 9) As Boolean

2)

el valor de numrand es igual a 0

3) como seria para que saliera del 1 al 9 sin repetirse es porque cuando cambio de numero (Text1(i) = "10") en vez de un 10 le cambio por un 9 y se me queda boqueado y si lo quito no sale nada en la casilla ya que este sirve para agregar el numero que falta.

4)numRand = Rnd(CInt(3) + 1) + 1 = numRand valdria 6 y CInt(3) valdria 3

5)en ese trozo k = (Int((Location - 1) / 9) * 9) + j
k=0
location = 1
j=1

y el ultimo k = (((Location - 1) Mod 9) + 1) + ((j - 1) * 9)

k=9

location = 1

j = 1

Publicado: Mar Mar 13, 2007 6:24 am
por lo_jorge
1 <= [ 1 + Int (RANDOM * 8.99) ] <= 9