1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
Declare Function GetTickCount Lib "Kernel32" () As Long
Dim Tbl() As Long
Dim Plage As Range
Dim Cel As Range
Dim Lgn As Long
Dim Col As Long
Dim PosCol As Long
Dim PosLgn As Long
Dim J As Long
Dim X As Long
Dim Y As Long
Dim Prox As Boolean
Dim Ini As Boolean
Sub Minuterie(Milliseconde As Long)
Dim Arret As Long
Arret = GetTickCount() + Milliseconde
Do While GetTickCount() < Arret
DoEvents
Loop
End Sub
Sub Virus()
Dim PlgProx As Range
Dim CelProx As Range
Dim C As Long
Dim L As Long
On Error GoTo Fin
'initialise les variables
If Ini = False Then
Initialise
Ini = True
End If
'effectue une pose d'une demi seconde
Minuterie 500
'si déjà initialisée, colore en bleu (contaminée) car la cellule est en vert (couleur du virus)
If PosLgn <> 0 Then Plage(PosLgn, PosCol).Interior.ColorIndex = 5
'supprime la couleur verte
If Plage(Lgn, Col).Interior.ColorIndex = 10 Then Plage(Lgn, Col).Interior.ColorIndex = -4142
'
If X <> 0 Then If Plage(X, Y).Interior.ColorIndex = 10 Then Plage(X, Y).Interior.ColorIndex = -4142
'évite d'incrémenter ou de décrémenter si la cible est une cellule à proximité
If Prox = False Then
'effectue un décalage de ligne et colonne en positif ou négatif selon position par rapport à la cible
If Plage(Lgn, Col).Row < Tbl(1, J) Then Lgn = Lgn + 1
If Plage(Lgn, Col).Row > Tbl(1, J) Then Lgn = Lgn - 1
If Plage(Lgn, Col).Column < Tbl(2, J) Then Col = Col + 1
If Plage(Lgn, Col).Column > Tbl(2, J) Then Col = Col - 1
End If
'pour l'effet visuel, la cellule en cours est colorée en vert
If Plage(Lgn, Col).Interior.ColorIndex = -4142 Then Plage(Lgn, Col).Interior.ColorIndex = 10
'si la couleur est rouge ou déjà en bleu
If Plage(Lgn, Col).Interior.ColorIndex = 3 Or Plage(Lgn, Col).Interior.ColorIndex = 5 Then
'si c'est la cellule cible (en rouge) incrémente pour la cible suivante
If Plage(Lgn, Col).Address = Cells(Tbl(1, J), Tbl(2, J)).Address Then
J = J + 1
'sinon, si la cellule n'est pas la cellule cible mais qu'elle fait partie des cibles et qu'elle
'n'a pas encore été contaminée (couleur bleu)
ElseIf Plage(Lgn, Col).Interior.ColorIndex = 3 Then
'redimensionne le tableau
RedimTablo Tbl(), Lgn, Col
End If
'mémorise la position
PosCol = Col
PosLgn = Lgn
'colore en vert (couleur du virus)
Plage(Lgn, Col).Interior.ColorIndex = 10
End If
'si la cellule en cours n'est pas en ligne 1 ou colonne 1
If Lgn > 1 Then L = -1
If Col > 1 Then C = -1
'mémorise pour supprimer la couleur verte de la cellule précédente
X = Lgn
Y = Col
'défini la plage de proximité
Set PlgProx = Plage.Range(Plage(Lgn, Col).Offset(L, C), Plage(Lgn, Col).Offset(1, 1))
'parcour la plage à la recherche d'une cellule rouge
For Each CelProx In PlgProx
'si trouvée, redéfini la colonne et la ligne pour se diriger vers elle au prochain clic
'mets la variable "Prox" à vrai et fin de boucle
If CelProx.Interior.ColorIndex = 3 Then
Col = CelProx.Column
Lgn = CelProx.Row
Prox = True
Exit For
End If
'si rien trouver, pas de cellule à infecter à proximité
Prox = False
Next CelProx
'appelle à nouveau pour le pas suivant
Virus
Exit Sub
Fin:
Reinitialise
End Sub
Sub Initialise()
Dim I As Long
'défini la plage en dur
Set Plage = Range("A1:O36")
'la sélection
'Set Plage = Selection
'mémorise l'emplacement des cellules colorées
For Each Cel In Plage
If Cel.Interior.ColorIndex = 3 Then
I = I + 1
ReDim Preserve Tbl(1 To 2, 1 To I)
Tbl(1, I) = Cel.Row
Tbl(2, I) = Cel.Column
End If
Next Cel
Randomize
Lgn = Int(Rnd * Plage.Rows.Count) + 1
Col = Int(Rnd * Plage.Columns.Count) + 1
J = 1
'le tableau est redistribué de façon aléatoire
Tri Tbl
End Sub
Sub Reinitialise()
Set Plage = Nothing
Erase Tbl()
Set Cel = Nothing
Ini = False
Lgn = 0
Col = 0
J = 0
PosCol = 0
PosLgn = 0
J = 0
Prox = False
X = 0
Y = 0
End Sub
Sub Tri(Tablo() As Long)
Dim Dico As Object
Dim Cle As Variant
Dim Tempo1 As Long
Dim Tempo2 As Long
Dim K As Long
'initialise le générateur de nombres aléatoire
Randomize
'crée le dico
Set Dico = CreateObject("Scripting.Dictionary")
'boucle tant que toutes les positions non pas été trouvées
Do
K = Int(Rnd * UBound(Tablo, 2)) + 1
If K <> 0 Then
If Dico.exists(K) = False Then
Dico.Add K, K
End If
End If
Loop While Dico.Count <> UBound(Tablo, 2)
K = 0
'réorganise le tableau pour une avancée aléatoire
For Each Cle In Dico.Keys
K = K + 1
Tempo1 = Tablo(1, K)
Tempo2 = Tablo(2, K)
Tablo(1, K) = Tablo(1, Cle)
Tablo(2, K) = Tablo(2, Cle)
Tablo(1, Cle) = Tempo1
Tablo(2, Cle) = Tempo2
Next Cle
End Sub
Sub RedimTablo(Tablo() As Long, Ligne As Long, Colonne As Long)
Dim M As Long
Dim N As Long
For M = 1 To UBound(Tablo, 2)
'quand trouvée, la supprime en déplacant les valeurs dans les dimensions et fin de boucle M
If Tablo(1, M) = Ligne And Tablo(2, M) = Colonne Then
For N = M To UBound(Tbl, 2) - 1
Tbl(1, N) = Tbl(1, N + 1)
Tbl(2, N) = Tbl(2, N + 1)
Next N
Exit For
End If
Next M
'redimensionne en supprimant la dernière dimension qui ne sert plus à rien
ReDim Preserve Tablo(1 To 2, 1 To UBound(Tablo, 2) - 1)
End Sub
Sub Colorer()
Dim LaPlage As Range
Dim LaCel As Range
Set LaPlage = Range("A1:O36")
For Each LaCel In LaPlage
If LaCel.Interior.ColorIndex = 5 Then LaCel.Interior.ColorIndex = 3
Next LaCel
End Sub |
Partager