New style of programming / Langton’s Ant

2 days ago, I imagined the programming styles that would be possible with a ‘can’ statement, which allows methods of a class to be declared outside of a class at any time during execution. My goal was to allow programs to be written in the order that you have new ideas, or in the order that they are explained in a summarized writing.

I outlined two python projects of identical function: a normally structured project and a can-statement project. The function of each project was to simulate and show Langton’s Ant.

original version:

#there is a Board class
# a Board can prepare(ruleset) its Ants' AI
# a Board can advance() its Ants
# a board can inform & control its Ants with step()
# a Board can display() itself
#there is an Ant class
# an Ant can move()
# an Ant can turn()
# an Ant can bound()
#there is an AI class
# an AI can decide(condition) how to turn
# an AI can influence(condition) the current square
#create a Board called "world"
#tell world to prepare(ruleset)
#loop:
# tell world to advance()
# tell world to display()

‘can’ statement version:

#there is a Board class.
#there is an Ant class.
#an Ant can move()
#an Ant can turn()
#there is an AI class.
#an AI can decide(condition) ho to turn
#an AI can influence(condition) the current square
#Boards can prepare(ruleset) their Ants' AI
#an Ant which is the child of a Board can step()
#an Ant which is the child of a Board can bound()
#a Board can advance() its Ants
#a Board can display() itself
#create a Board called "world"
#tell world to prepare(ruleset) its Ants.
#loop:
# tell world to advance()
# tell world to display()

It might be good for drafts, but it isn’t helping if it causes you to delay memorization of the structure of the project. It also becomes messier and harder to jump into. Perhaps it can be used to write first drafts of classes, and then converted to regular python before classes get very large.

In combination with ‘can’ statements, the example uses ‘of’ statements, which allow methods to be created in the context of both an object and its parent.

Ant of Board can Bound(self,parent):
  self.x %= parent.width
  self.y %= parent.height

 

I took both outlines and created real python projects out of them. The ‘can’ statement version isn’t runnable, but for a while, I updated it to fix errors I found in the working version. It would work if I could compile it. I’ll clean up both projects and publish them soon.

langtons-ant-LR-2-highway-nearWrap-clean
My program’s output after running the first 11208 iterations of the basic LR Langton’s ant.
The board wraps around, so the Ant isn’t able to build an infinite highway.

Leave a comment