/
lantonsant.cpp
101 lines (99 loc) · 2.06 KB
/
lantonsant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <ctime>
#include <cmath>
#include <gl/glut.h>
#define windowsize 400
#define mapsize 100
bool map[mapsize][mapsize];
int width, height;
int antx, anty;
int heading;
void init();
void calc();
void draw();
void reshape(GLsizei, GLsizei);
void init()
{
heading = 0;
antx = mapsize / 2;
anty = mapsize / 2;
width = windowsize;
height = windowsize;
std::srand(unsigned(time(NULL)));
for (int i = 0; i < mapsize; ++i)
for (int j = 0; j < mapsize; ++j)
map[i][j] = (rand() % 100 > 85);
return;
}
void calc()
{
if (map[antx][anty])
{
map[antx][anty] = false;
heading = (heading + 1) % 4;
}
else
{
map[antx][anty] = true;
heading = (heading + 3) % 4;
}
switch (heading)
{
case 0:--antx; break;
case 1:--anty; break;
case 2:++antx; break;
case 3:++anty; break;
}
if (antx < 0)
antx = mapsize - 1;
else if (antx >= mapsize)
antx = 0;
if (anty < 0)
anty = mapsize - 1;
else if (anty >= mapsize)
anty = 0;
return;
}
void draw()
{
calc();
reshape(width, height);
glutPostRedisplay();
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2);
glBegin(GL_POINTS);
glColor3f(0.5, 0.8, 0.9);
for (int i = 0; i < mapsize; ++i)
for (int j = 0; j < mapsize; ++j)
if (map[i][j])
glVertex2f(i - mapsize / 2, j - mapsize / 2);
glEnd();
glFlush();
}
void reshape(GLsizei w, GLsizei h)
{
width = w;
height = h;
GLfloat ratio = (GLfloat)w / (GLfloat)h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double left = (double)-mapsize * 0.5 - 0.1;
double right = (double)mapsize*0.5 + 0.1;
if (ratio <= 1) gluOrtho2D(left, right, left / ratio, right / ratio);
else gluOrtho2D(left*ratio, right*ratio, left, right);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return;
}
int main()
{
init();
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(windowsize, windowsize);
glutCreateWindow("lanton\'s ant");
glutReshapeFunc(reshape);
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}