VBA Excel - Langton's Ant

October 2017


VBA Excel - Langton's Ant



This VBA code is an illustration of a program created by Chris Langton .

Rules of the "game"

  • If the ant is on a white square, it rotates to the left
  • If it is on a black square, it rotates to the right
  • The Ant reverse color of the box on which it is located (black becomes white and vice versa);
  • The ant moves one step in the direction it is currently pointing too.

Getting started

  • Dialog boxes have been included to show changes in the movement of the ant. Three stages exists: the "symmetric" phase, the "chaotic" phase and the "highway" phase.
  • Lines of code from 8-14 , are a formatting for the Excel sheet ( "Sheet1")

Coding

  • Open a new Excel workbook,
  • Press ALT + F11 (=> Visual Basic Editor window opens)
  • Insert/Module
  • Paste the below code:


Option Explicit

Sub Ant
Dim ColActuelle As Integer, LigActuelle As Long, ColFuture As Integer, LigFuture As Long, i As Long
Dim Haut As Boolean, Gauche As Boolean, Bas As Boolean, Droite As Boolean

'Mise en forme
Application.ScreenUpdating = False
With Sheets("Feuil1").Range("A1:IV30000")
    .ColumnWidth = 3
    .RowHeight = 19.5
    .Interior.ColorIndex = -4142
End With
Application.ScreenUpdating = True

MsgBox "Début de la phase « symétrique »"
Haut = True
Gauche = False
Bas = False
Droite = False
LigFuture = 100
ColFuture = 50
Sheets("Feuil1").Cells(LigFuture, ColFuture).Select
For i = 1 To 15000
    If i = 500 Then Sheets("Feuil1").Cells(LigFuture, ColFuture).Select: MsgBox "Début de la phase « chaotique »"
    If i = 10000 Then Sheets("Feuil1").Cells(LigFuture, ColFuture).Select: MsgBox "Début de la phase « autoroute »"
    LigActuelle = LigFuture
    ColActuelle = ColFuture
    If LigActuelle = 1 Or ColActuelle = 1 Or ColActuelle = 252 Or LigActuelle = 60000 Then GoTo Fin
    With Sheets("Feuil1").Cells(LigActuelle, ColActuelle)
        If .Interior.ColorIndex = -4142 Then
            .Interior.ColorIndex = 1
            If Haut = True Then
                Haut = False: Gauche = True: LigFuture = LigActuelle: ColFuture = ColActuelle - 1
            ElseIf Gauche = True Then
                Gauche = False: Bas = True: LigFuture = LigActuelle + 1: ColFuture = ColActuelle
            ElseIf Bas = True Then
                Bas = False: Droite = True: LigFuture = LigActuelle: ColFuture = ColActuelle + 1
            ElseIf Droite = True Then
                Droite = False: Haut = True: LigFuture = LigActuelle - 1: ColFuture = ColActuelle
            End If
        Else
            .Interior.ColorIndex = -4142
            If Haut = True Then
                Haut = False: Droite = True: LigFuture = LigActuelle: ColFuture = ColActuelle + 1
            ElseIf Gauche = True Then
                Gauche = False: Haut = True: LigFuture = LigActuelle - 1: ColFuture = ColActuelle
            ElseIf Bas = True Then
                Bas = False: Gauche = True: LigFuture = LigActuelle: ColFuture = ColActuelle - 1
            ElseIf Droite = True Then
                Droite = False: Bas = True: LigFuture = LigActuelle + 1: ColFuture = ColActuelle
            End If
        End If
    End With
Next i
Exit Sub
Fin:
MsgBox "Stop ! La fourmi a atteints les limites de la feuille Excel."
End Sub
  • Adapt the code by systematically replacing "Sheet1 " with the name of your sheet.
  • Close the Visual Basic Editor
  • Press: Alt + F8 , choose " Ant" and click "Run."

Note that


It is possible to increase (or decrease) the number of iterations and boxes, by changing the line 24:
  • For i = 1 To 15000
It is also possible to change colors by changing the lines 12, 31, 32 and 43 ...
Published by jak58. Latest update on January 3, 2014 at 06:39 AM by jak58.
This document, titled "VBA Excel - Langton's Ant," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).