EASY!
Run this little 10 minute project I did during a bit of slow time. Takes about 30 seconds to run and is fun to watch.
Some fun little practice you can do with this as a base code.
PS
Sorry if you find this post inappropriate for this section, I just thought it might bring a bit of a smile and also serve to show some basic concepts. I found Langton's Ant to be a great introduction into automation.
Run this little 10 minute project I did during a bit of slow time. Takes about 30 seconds to run and is fun to watch.
Code:
Sub LangtonsAnt()
Sheets.Add
'code below sets all cells to be small and square
Application.ScreenUpdating = False
Range(ActiveSheet.Cells.Address).RowHeight = 4
For i = 1 To 3 'excel is needy and has to ahve this run multiple times to work, go ahead and try it only unning this block once, you will see what I mean
With ActiveSheet
.Columns.ColumnWidth = _
.Columns("A").ColumnWidth / .Columns("A").Width * _
.Rows(1).Height
End With
Next
Application.ScreenUpdating = True
'instructions
MsgBox ("This is called Langton's Ant, it is an automototon with 2 rules." & Chr(13) & " 1. If the cell is white, turn it black and move to the left." & Chr(13) & " 2. If the cell is black, turn it white and move to the right." & Chr(13) & "press ctrl+pause/break to end early, but it only does 10,000 steps which should take less than 30 seconds.")
'now the good stuff
Dim x, y As Long 'x=row, y=column
Dim direction As Single 'can only be 0-5, 1-5 correspond to cardinal directions, 0 and 5 signify passing "North" in one direction or the other
Dim color As Boolean 'I'll get to this in a minute
x = 30: y = 40: direction = 1 'sets arbitrary starting point and direction
For i = 1 To 10000 'arbitrary numebr of steps, make it longer or shorter as you like
Cells(x, y).Select 'I use select to give a visual representation of the "ant's" position
If Selection.Interior.ColorIndex = 1 Then 'if it is black
color = True 'note that is it black
Selection.Interior.ColorIndex = -4142 'set it to have no fill
direction = direction + 1 'turn right
Else 'if it is not black
color = False 'note it is not black
Selection.Interior.ColorIndex = 1 'set it to black
direction = direction - 1 'turn left
End If
If direction > 4 Then direction = 1 'if you turned right all the way back to North, set it to North
If direction < 1 Then direction = 4 'if you turned left all the way past North, set it to West (left of north)
Select Case direction
Case 1 'going north
x = x - 1 'minus a row
Case 2 'going east
y = y + 1 'add a column
Case 3 'going south
x = x + 1 'add a row
Case 4 'going west
y = y - 1 'minus a column
End Select
If x > 200 Then x = 200 'arbitrary maximum so it doesn't go off screen
If y > 200 Then y = 200
If x < 1 Then x = 1 'not so arbitrary minimums. If X or Y are <1, it will error out
If y < 1 Then y = 1 'try making them "loop" back tot he outter bounds, it will have a different behavior on hitting a boundry like that.
DoEvents 'this is made to slow it down just ever so slightly and give you a chance to break the code if it breaks
Next
End Sub
Some fun little practice you can do with this as a base code.
- Change the starting position of the ant to see how it reacts with walls
- Change the way walls interact (looping around VS hard block)
- Add a way for the user to control the number of steps
- Add a userform to allow a user to setup their own rules (add another rule of RED meaning don't change direction)
- Add more ants!
PS
Sorry if you find this post inappropriate for this section, I just thought it might bring a bit of a smile and also serve to show some basic concepts. I found Langton's Ant to be a great introduction into automation.