#include <SFML/Graphics.hpp>
#include <cstdint>
sf::Vector2i rotateRight(sf::Vector2i v) {
return sf::Vector2i(v.y, -v.x);
}
sf::Vector2i rotateLeft(sf::Vector2i v) {
return sf::Vector2i(-v.y, v.x);
}
class antField {
public:
uint32_t W, H;
sf::Vector2i antPos, antDir;
uint8_t *cells;
antField(uint32_t W_, uint32_t H_) {
W = W_;
H = H_;
antPos = sf::Vector2i(W / 2, H / 2);
antDir = sf::Vector2i(1, 0);
cells = new uint8_t[W * H];
}
~antField() {
delete[] cells;
}
void step(sf::Image *img) {
uint32_t *map = (uint32_t *) (img->getPixelsPtr());
uint32_t i = antPos.x + W * antPos.y;
uint8_t c = cells[i];
bool angle = c != 2;
map[i] = c == 0 ? 0xff0013FF :
c == 1 ? 0xff9C8903 :
0xff00EC95;
antDir = angle ? rotateLeft(antDir) : rotateRight(antDir);
cells[i] = (c + 1) % 3;
antPos += antDir;
(antPos.x += W) %= W;
(antPos.y += H) %= H;
}
};
int main() {
sf::ContextSettings setting;
setting.antialiasingLevel = 8;
const uint16_t winW = 800, winH = 800;
antField mainField(winW, winH);
sf::Image img;
img.create(winW, winH);
sf::Texture tex;
tex.create(winW, winH);
sf::Sprite spr(tex);
sf::RenderWindow window(sf::VideoMode(winW, winH), "ant", sf::Style::Default, setting);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
for (uint32_t i = 0; i < 1000; i++) {
mainField.step(&img);
}
tex.loadFromImage(img);
window.clear();
window.draw(spr);
window.display();
}
return 0;
}