Langston's Ant

From Public Wiki
Revision as of 16:54, 27 May 2021 by Legg (talk | contribs) (Created page with "==Synopsis== Performs a simulation of Langston's Ant ==Notes== There are no borders, the map wraps edge to edge. Define statements have a 11x11 grid and 10 steps. The ANT...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Synopsis

Performs a simulation of Langston's Ant

Notes

There are no borders, the map wraps edge to edge. Define statements have a 11x11 grid and 10 steps. The ANT always starts at, or near, the center.

Compilation

gcc -o Ant Ant.c

Code

 1 #include<stdio.h>
 2 #define X 11
 3 #define Y 11
 4 #define S 10
 5 
 6 void main(void)
 7 {
 8 	char world[X][Y];
 9 	unsigned short x, y, c, d, h, s;
10 	//init
11 	x=X/2; y=Y/2; h=0;
12 	for(c=0;c<Y;c++)
13 	{
14 		for(d=0;d<X;d++)
15 		{
16 			world[d][c] = ' ';
17 		}
18 	}
19 
20 	//loop
21 	for(s=0;s<S;s++)
22 	{
23 		//print current state
24 		printf("S=%d\n\n",s+1);
25 		printf("%c", '/');
26 		for(c=0;c<X;c++)
27 		{
28 			printf("%c", '-');
29 		}
30 		printf("%c\n", '\\');
31 		for(c=0;c<Y;c++)
32 		{
33 			printf("%c", '|');
34 			for(d=0;d<X;d++)
35 			{
36 				if(d==x && c==y)
37 					printf("A");
38 				else
39 					printf("%c",world[d][c]);
40 			}
41 			printf("%c\n", '|');
42 		}
43 		printf("%c", '\\');
44 		for(c=0;c<X;c++)
45 		{
46 			printf("%c", '-');
47 		}
48 		printf("/\n\n");
49 		//turn logic
50 		if(world[x][y]==' ')
51 		{
52 			h++; //CW
53 			h = h % 4;
54 			world[x][y]='*';
55 		}
56 		else
57 		{	
58 			h--; //CCW
59 			h= h % 4;
60 			world[x][y]=' ';
61 		}
62 		//move forward
63 		if(h==0)
64 			y = (y - 1) % Y;
65 		if(h==2)
66 			y = (y + 1) % Y;
67 		if(h==1)
68 			x = (x + 1) % X;
69 		if(h==3)
70 			x = (x - 1) % X;
71 
72 	}
73 
74 	//terminate
75 	printf("\nEOS \n");
76 	return;
77 }