Difference between revisions of "Langston's Ant"

From Public Wiki
Jump to navigation Jump to search
 
Line 16: Line 16:
 
#define X 11
 
#define X 11
 
#define Y 11
 
#define Y 11
#define S 10
+
#define S 240
  
 
void main(void)
 
void main(void)
Line 23: Line 23:
 
unsigned short x, y, c, d, h, s;
 
unsigned short x, y, c, d, h, s;
 
//init
 
//init
x=X/2; y=Y/2; h=0;
+
x=X/2; y=Y/2; h=3;
 
for(c=0;c<Y;c++)
 
for(c=0;c<Y;c++)
 
{
 
{
 
for(d=0;d<X;d++)
 
for(d=0;d<X;d++)
 
{
 
{
world[d][c] = ' ';
+
world[d][c] = '.';
 
}
 
}
 
}
 
}
Line 62: Line 62:
 
printf("/\n\n");
 
printf("/\n\n");
 
//turn logic
 
//turn logic
if(world[x][y]==' ')
+
if(world[x][y]=='.')
 
{
 
{
 
h++; //CW
 
h++; //CW
Line 72: Line 72:
 
h--; //CCW
 
h--; //CCW
 
h= h % 4;
 
h= h % 4;
world[x][y]=' ';
+
world[x][y]='.';
 
}
 
}
 
//move forward
 
//move forward
 
if(h==0)
 
if(h==0)
y = (y - 1) % Y;
+
{if(y==0) y=Y-1; else y--;}
 
if(h==2)
 
if(h==2)
y = (y + 1) % Y;
+
{if(y==Y-1) y=0; else y++;}
 
if(h==1)
 
if(h==1)
x = (x + 1) % X;
+
{if(x==X-1) x=0; else x++;}
 
if(h==3)
 
if(h==3)
x = (x - 1) % X;
+
{if(x==0) x=X-1; else x--;}
 
 
 
}
 
}
  

Latest revision as of 18:12, 27 May 2021

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. The ant defaults being oriented towards north as an initial condition.

Compilation

gcc -o Ant Ant.c

Code

 1 #include<stdio.h>
 2 #define X 11
 3 #define Y 11
 4 #define S 240
 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=3;
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 			{if(y==0) y=Y-1; else y--;}
65 		if(h==2)
66 			{if(y==Y-1) y=0; else y++;}
67 		if(h==1)
68 			{if(x==X-1) x=0; else x++;}
69 		if(h==3)
70 			{if(x==0) x=X-1; else x--;}
71 	}
72 
73 	//terminate
74 	printf("\nEOS \n");
75 	return;
76 }