How to resolve the algorithm Langton's ant step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Langton's ant step by step in the Prolog programming language

Table of Contents

Problem Statement

Langton's ant is a cellular automaton that models an ant sitting on a plane of cells, all of which are white initially, the ant facing in one of four directions. Each cell can either be black or white. The ant moves according to the color of the cell it is currently sitting in, with the following rules: This rather simple ruleset leads to an initially chaotic movement pattern, and after about 10000 steps, a cycle appears where the ant moves steadily away from the starting location in a diagonal corridor about 10 cells wide.
Conceptually the ant can then walk infinitely far away.

Start the ant near the center of a 100x100 field of cells, which is about big enough to contain the initial chaotic part of the movement. Follow the movement rules for the ant, terminate when it moves out of the region, and show the cell colors it leaves behind.

The problem has received some analysis; for more details, please take a look at the Wikipedia article   (a link is below)..

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Langton's ant step by step in the Prolog programming language

Source code in the prolog programming language

%_______________________________________________________________
% Langtons ant.
:-dynamic
	black/1.

plot_point(Row, Col) :-   % Output a 5x5 black box at R,C
	new(C, box(5,5)), X is Col * 5 - 2, Y is Row * 5 - 2,
	send(C, colour, colour(black)), send(C, fill_pattern, colour(blue)),
	send(C, center(point(X,Y))), send(@win, display, C).
update_win :-  % Make a 500x500 window, find all the black points and plot them
	new(@win, window('Langtons Ant')),
	send(@win, size, size(500,500)), send(@win, open),
	black(Row/Col),plot_point(Row,Col),fail.
update_win.

direction(Row, Col, left) :- black(Row/Col), !, retract(black(Row/Col)).
direction(Row, Col, right):- not(black(Row/Col)), !, assert(black(Row/Col)).

move(_, Row,Col) :- (Row < 0; Col < 0; Row > 99; Col > 99), !.
move(north,Row,Col) :-
	(direction(Row,Col,left), C is Col - 1, !, move(west, Row, C));
	(direction(Row,Col,right), C is Col + 1, !, move(east, Row, C)).
move(south,Row,Col) :-
	(direction(Row,Col,right), C is Col - 1, !, move(west, Row, C));
	(direction(Row,Col,left), C is Col + 1, !, move(east, Row, C)).
move(east,Row,Col) :-
	(direction(Row,Col,right), R is Row + 1, !, move(south, R, Col));
	(direction(Row,Col,left), R is Row - 1, !, move(north, R, Col)).
move(west,Row,Col) :-
	(direction(Row,Col,left), R is Row + 1, !, move(south, R, Col));
	(direction(Row,Col,right), R is Row - 1, !, move(north, R, Col)).

go :-   retractall(black(_)), move(north,49,49), update_win.


  

You may also check:How to resolve the algorithm Empty program step by step in the Fish programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Zoomscript programming language
You may also check:How to resolve the algorithm File input/output step by step in the PowerShell programming language
You may also check:How to resolve the algorithm LU decomposition step by step in the PARI/GP programming language