Task: Langton's ant








VB: Option Explicit

Sub Ant()
  Dim TablDatas(1 To 200, 1 To 256) As String, sDir As String, sFile As String, Str As String
  Dim ColA As Integer, LigA As Long, ColF As Integer, LigF As Long, i As Long, j As Integer, Num As Long
  Dim Top As Boolean, Left As Boolean, Bottom As Boolean, Right As Boolean
    
    
    Select Case Int(Rnd(4) * 4)
      Case 0: Top = True
      Case 1: Right = True
      Case 2: Bottom = True
      Case 3: Left = True
    End Select
      
    LigF = 80
    ColF = 50
    For i = 1 To 200
        For j = 1 To 256
            TablDatas(i, j) = " "
        Next
    Next
    
    sDir = "/Users/mjreed/Desktop/"
    
    sFile = "Langton_Ant.txt"
    
    
    Dim Dir As String
    For i = 1 To 15000
        LigA = LigF
        ColA = ColF
       
        If LigA = 1 Or ColA = 1 Or ColA = 256 Or LigA = 200 Then GoTo Fin
        If TablDatas(LigA, ColA) = " " Then
            TablDatas(LigA, ColA) = "#"
            Select Case True
                Case Top: Top = False: Right = True: LigF = LigA: ColF = ColA + 1
                Case Left: Left = False: Top = True: LigF = LigA - 1: ColF = ColA
                Case Bottom: Bottom = False: Left = True: LigF = LigA: ColF = ColA - 1
                Case Right: Right = False: Bottom = True: LigF = LigA + 1: ColF = ColA
            End Select
        Else
            TablDatas(LigA, ColA) = " "
            Select Case True
                Case Top: Top = False: Left = True: LigF = LigA: ColF = ColA - 1
                Case Left: Left = False: Bottom = True: LigF = LigA + 1: ColF = ColA
                Case Bottom: Bottom = False: Right = True: LigF = LigA: ColF = ColA + 1
                Case Right: Right = False: Top = True: LigF = LigA - 1: ColF = ColA
            End Select
        End If
    Next i
Fin:
    
    Num = FreeFile
    Open sDir & sFile For Output As #Num
    
    For i = 1 To UBound(TablDatas, 1)
        Str = vbNullString
        For j = 1 To UBound(TablDatas, 2)
            Str = Str & TablDatas(i, j)
        Next j
        Print #Num, Str
    Next i
    Close #Num
    Exit Sub
End Sub




Ada: with Ada.Text_IO;

procedure Langtons_Ant is

   Size: constant Positive := 100; 

   subtype Step is Integer range -1 .. +1;

   procedure Right(N, W: in out Step) is
      Tmp: Step := W;
   begin
      W := - N;
      N := Tmp;
   end Right;

   procedure Left(N, W: in out Step) is
   begin
      for I in 1 .. 3 loop
         Right(N, W);
      end loop;
   end Left;

   Color_Character: array(Boolean) of Character :=
     (False => ' ', True => '#');

   Is_Black: array (1 .. Size, 1 .. Size) of Boolean :=
     (others => (others => False)); 

   Ant_X, Ant_Y: Natural := Size/2; 
   Ant_North: Step := 1; Ant_West: Step := 0; 

   Iteration: Positive := 1;

begin
   loop 
      if Is_Black(Ant_X, Ant_Y) then
         Left(Ant_North, Ant_West);
      else
         Right(Ant_North, Ant_West);
      end if;
      Is_Black(Ant_X, Ant_Y) := not Is_Black(Ant_X, Ant_Y);
      Ant_X := Ant_X - Ant_North; 
      Ant_Y := Ant_Y - Ant_West;  
      Iteration := Iteration + 1;
    end loop;

exception
   when Constraint_Error => 
      for X in 1 .. Size loop
         for Y in 1 .. Size loop
            Ada.Text_IO.Put(Color_Character(Is_Black(X, Y)));
         end loop;
         Ada.Text_IO.New_Line;
      end loop;
      Ada.Text_IO.Put_Line("# Iteration:" & Integer'Image(Iteration));
end Langtons_Ant;