Const ancho = 9
Const alto = 9
Const maxmin = 10
Dim tablero(alto, ancho) As Integer
Dim jugador As Integer
Dim inicio As Long
Dim fin As Long
Dim iniciado As Boolean
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub aviso(texto As String)
Range(“K1″).Value = texto
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub comprobar_zona_central()
Dim i, fil, col As Integer
Dim minas As Integer
For fil = 2 To alto – 1
For col = 2 To ancho – 1
If tablero(fil, col) <> -2 Then
minas = 0
For i = col – 1 To col + 1
If tablero(fil – 1, i) = -2 Then minas = minas + 1
If tablero(fil + 1, i) = -2 Then minas = minas + 1
Next i
If tablero(fil, col – 1) = -2 Then minas = minas + 1
If tablero(fil, col + 1) = -2 Then minas = minas + 1
If minas > 0 Then tablero(fil, col) = minas
End If
Next col
Next fil
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub comprobar_primera_ultima_fila()
Dim i, col As Integer
Dim minas As Integer
For col = 2 To ancho – 1
If tablero(1, col) <> -2 Then
minas = 0
For i = col – 1 To col + 1
If tablero(2, i) = -2 Then minas = minas + 1
Next i
If tablero(1, col – 1) = -2 Then minas = minas + 1
If tablero(1, col + 1) = -2 Then minas = minas + 1
If minas > 0 Then tablero(1, col) = minas
End If
If tablero(alto, col) <> -2 Then
minas = 0
For i = col – 1 To col + 1
If tablero(alto – 1, i) = -2 Then minas = minas + 1
Next i
If tablero(alto, col – 1) = -2 Then minas = minas + 1
If tablero(alto, col + 1) = -2 Then minas = minas + 1
If minas > 0 Then tablero(alto, col) = minas
End If
Next col
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub comprobar_primera_ultima_columna()
Dim i, fil As Integer
Dim minas As Integer
For fil = 2 To alto – 1
If tablero(fil, 1) <> -2 Then
minas = 0
For i = fil – 1 To fil + 1
If tablero(i, 2) = -2 Then minas = minas + 1
Next i
If tablero(fil – 1, 1) = -2 Then minas = minas + 1
If tablero(fil + 1, 1) = -2 Then minas = minas + 1
If minas > 0 Then tablero(fil, 1) = minas
End If
If tablero(fil, ancho) <> -2 Then
minas = 0
For i = fil – 1 To fil + 1
If tablero(i, ancho – 1) = -2 Then minas = minas + 1
Next i
If tablero(fil – 1, ancho) = -2 Then minas = minas + 1
If tablero(fil + 1, ancho) = -2 Then minas = minas + 1
If minas > 0 Then tablero(fil, ancho) = minas
End If
Next fil
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub comprobar_cuatro_esquinas()
Dim i, col As Integer
Dim minas As Integer
If tablero(1, 1) <> -2 Then
minas = 0
If tablero(1, 2) = -2 Then minas = minas + 1
If tablero(2, 1) = -2 Then minas = minas + 1
If tablero(2, 2) = -2 Then minas = minas + 1
If minas > 0 Then tablero(1, 1) = minas
End If
If tablero(1, ancho) <> -2 Then
minas = 0
If tablero(1, ancho – 1) = -2 Then minas = minas + 1
If tablero(2, ancho – 1) = -2 Then minas = minas + 1
If tablero(2, ancho) = -2 Then minas = minas + 1
If minas > 0 Then tablero(1, ancho) = minas
End If
If tablero(alto, 1) <> -2 Then
minas = 0
If tablero(alto – 1, 1) = -2 Then minas = minas + 1
If tablero(alto – 1, 2) = -2 Then minas = minas + 1
If tablero(alto, 2) = -2 Then minas = minas + 1
If minas > 0 Then tablero(alto, 1) = minas
End If
If tablero(alto, ancho) <> -2 Then
minas = 0
If tablero(alto – 1, ancho – 1) = -2 Then minas = minas + 1
If tablero(alto – 1, ancho) = -2 Then minas = minas + 1
If tablero(alto, ancho – 1) = -2 Then minas = minas + 1
If minas > 0 Then tablero(alto, ancho) = minas
End If
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub marcar_celda()
Dim fil, col, marcadas As Integer
If ActiveCell.Interior.ColorIndex > 0 Then
ActiveCell.Interior.ColorIndex = 38
marcadas = marcadas + 1
fil = ActiveCell.Row
col = ActiveCell.Column
tablero(fil, col) = -3
End If
End Sub
Sub desmarcar_celda()
ActiveCell.Interior.ColorIndex = 19
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub vaciar_celdas(ByVal fil As Integer, ByVal col As Integer)
Cells(fil, col).Interior.ColorIndex = 0 ‘Pone la celda en blanco
If tablero(fil, col) > 0 Then ‘Muestra el número en la celda
Cells(fil, col).Value = tablero(fil, col)
ElseIf tablero(fil, col) = 0 Then ‘Busca celdas vacías adyacentes
tablero(fil, col) = -1 ‘El valor -1 indica una celda vacía destapada
If fil > 1 And col > 1 Then vaciar_celdas fil – 1, col – 1 ‘Arriba izquierda
If fil > 1 And col < ancho Then vaciar_celdas fil – 1, col + 1 ‘Arriba derecha
If fil < alto And col > 1 Then vaciar_celdas fil + 1, col – 1 ‘Abajo izquierda
If fil < alto And col < ancho Then vaciar_celdas fil + 1, col + 1 ‘Abajo derecha
If fil > 1 Then vaciar_celdas fil – 1, col ‘Arriba
If fil < alto Then vaciar_celdas fil + 1, col ‘Abajo
If col > 1 Then vaciar_celdas fil, col – 1 ‘Izquierda
If col < ancho Then vaciar_celdas fil, col + 1 ‘Derecha
End If
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub destapar_celda()
Dim fil, col, fi, co As Integer
If iniciado = False Then
inicio = Timer
iniciado = True
End If
”Range(“P2″).Value = inicio
fil = ActiveCell.Row
col = ActiveCell.Column
If tablero(fil, col) = -2 Then ‘Fin del juego si pisamos una mina
For fi = 1 To alto
For co = 1 To ancho
If tablero(fi, co) <> -2 Then
Cells(fi, co).Interior.ColorIndex = 0
If tablero(fi, co) > 0 Then Cells(fi, co).Value = tablero(fi, co)
Else
Cells(fi, co).Interior.ColorIndex = 38
Cells(fi, co).Value = “X”
End If
Next co
Next fi
”Range(“P2″).Value = inicio
fin = CLng(Timer) – inicio
Range(“O2″).Value = 10
Range(“P2″).Value = fin & ” Seg.”
MsgBox (“GAME OVER!”)
ElseIf tablero(fil, col) > 0 Then ‘Muestra el número en la celda
Cells(fil, col).Interior.ColorIndex = 0
Cells(fil, col).Value = tablero(fil, col)
tablero(fil, col) = -1 ‘La celda ha sido destapada
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub final_juego()
Dim fi, col As Integer
For fi = 1 To alto
For co = 1 To ancho
If tablero(fi, co) = -1 Then
destapas = destapas + 1
ElseIf tablero(fi, co) = -2 And Cells(fi, co).Interior.ColorIndex = 38 Then
minas = minas + 1
End If
Next co
Next fi
If destapadas = alto * ancho – maxmin And minas = maxmin Then
fin = CLng(Timer) – inicio
Range(“O2″).Value = minas
Range(“P2″).Value = fin & ” Seg.”
MsgBox (“WINNER!”)
End If
Else
vaciar_celdas fil, col ‘Muestra celda vacía y las adyacentes
End If
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub colocar_numeros()
Dim fil, col As Integer
Dim minas As Integer
For fil = 1 To alto
For col = 1 To ancho
If tablero(fil, col) <> -2 Then
minas = 0
If fil > 1 And col > 1 Then If tablero(fil – 1, col – 1) = -2 Then minas = minas + 1 ‘Arriba izquierda
If fil > 1 And col < ancho Then If tablero(fil – 1, col + 1) = -2 Then minas = minas + 1 ‘Arriba derecha
If fil < alto And col > 1 Then If tablero(fil + 1, col – 1) = -2 Then minas = minas + 1 ‘Abajo izquierda
If fil < alto And col < ancho Then If tablero(fil + 1, col + 1) = -2 Then minas = minas + 1 ‘Abajo derecha
If fil > 1 Then If tablero(fil – 1, col) = -2 Then minas = minas + 1 ‘Arriba
If fil < alto Then If tablero(fil + 1, col) = -2 Then minas = minas + 1 ‘Abajo
If col > 1 Then If tablero(fil, col – 1) = -2 Then minas = minas + 1 ‘Izquierda
If col < ancho Then If tablero(fil, col + 1) Then minas = minas + 1 ‘Derecha
tablero(fil, col) = minas
End If
Next col
Next fil
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Sub comenzar_juego()
Dim i, fil, col As Integer
Erase tablero
iniciado = False
Range(“P2″).Value = “”
Range(“O2″).Value = “”
Range(“A1:I9″).Clear
Range(“A1:I9″).HorizontalAlignment = xlCenter
Range(“A1:I9″).VerticalAlignment = xlCenter
Range(“A1:I9″).Borders.ColorIndex = 9
Range(“A1:I9″).Interior.ColorIndex = 19
For i = 1 To maxmin ‘Colocar 10 minas al azar
Randomize
fil = Int(alto * Rnd + 1)
Randomize
col = Int(ancho * Rnd + 1)
If tablero(fil, col) = 0 Then
tablero(fil, col) = -2 ‘El valor -2 indica una mina
Cells(fil, col).Interior.ColorIndex = 38
Else
i = i – 1
End If
Next
colocar_numeros
‘comprobar_zona_central
‘comprobar_primera_ultima_fila
‘comprobar_primera_ultima_columna
‘comprobar_cuatro_esquinas
End Sub
//——————————————————————————————————————————————–//——————————————————————————————————————————————–
Estas lineas separan los subs, teneís que borrarlos, visual basic no lo tolera como comentario (el Comentario es (–> ‘ <– ))
View full article »