Advertisement
Guest User

langtons ant test program

a guest
Mar 7th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. //code is cc0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. //size of torus (odd by odd for weird bridges)
  6. #define MAP_X 9999
  7. #define MAP_Y 9999
  8. //NOTE: change this to fit your terminal size (or smaller)
  9. #define SCR_WIDTH 1906
  10. #define SCR_HEIGHT 501
  11. //rules are defines for testing other rules if needed, should clean this
  12. #define TURN_R switch(ant.dir) {\
  13.     case UP: ant.dir = RIGHT; break;\
  14.     case DOWN: ant.dir = LEFT; break;\
  15.     case LEFT: ant.dir = UP; break;\
  16.     case RIGHT: ant.dir = DOWN; break;\
  17. }
  18. #define TURN_L switch(ant.dir) {\
  19.     case UP: ant.dir = LEFT; break;\
  20.     case DOWN: ant.dir = RIGHT; break;\
  21.     case LEFT: ant.dir = DOWN; break;\
  22.     case RIGHT: ant.dir = UP; break;\
  23. }
  24. //directions and the ant type
  25. enum t_dir {UP, DOWN, LEFT, RIGHT};
  26. struct t_ant {
  27.     int x;
  28.     int y;
  29.     enum t_dir dir;
  30. };
  31. main() {
  32.     //clear screen
  33.     printf("\x1b[48;2;0;0;0m\x1b[2J");
  34.     struct t_ant ant;
  35.     //center in the visible part of screen and initialize
  36.     ant.x = SCR_WIDTH/2;
  37.     ant.y = SCR_HEIGHT/2;
  38.     ant.dir = UP;
  39.     //allocate memory
  40.     char *map = calloc(MAP_X * MAP_Y, sizeof(char));
  41.     while(1) {
  42.         if(ant.x < SCR_WIDTH && ant.y < SCR_HEIGHT) printf("\x1b[%d;%dH", ant.y, ant.x);
  43.         //check current cell then accordingly; flip cell, print new cell, and change direction
  44.         //this also checks if the ant is offscreen in order to save time in order to decide to print or not, it will just process it way faster until the ant reappears
  45.         switch(map[ant.x + (ant.y * MAP_X)]) {
  46.             case 0:
  47.                 map[ant.x + (ant.y * MAP_X)] = 1;
  48.                 TURN_R
  49.                 if(ant.x < SCR_WIDTH && ant.y < SCR_HEIGHT) printf("\x1b[48;2;255;255;255m "); break;
  50.             case 1:
  51.                 map[ant.x + (ant.y * MAP_X)] = 0;
  52.                 TURN_L
  53.                 if(ant.x < SCR_WIDTH && ant.y < SCR_HEIGHT) printf("\x1b[48;2;0;0;0m "); break;
  54.         }
  55.         //move forward in direction
  56.         switch(ant.dir) {
  57.             case UP: ant.y = ant.y + 1; if(ant.y == MAP_Y) ant.y = 0; break;
  58.             case DOWN: ant.y = ant.y - 1; if(ant.y == -1) ant.y = MAP_Y - 1; break;
  59.             case LEFT: ant.x = ant.x - 1; if(ant.x == -1) ant.x = MAP_X - 1; break;
  60.             case RIGHT: ant.x = ant.x + 1; if(ant.x == MAP_X) ant.x = 0; break;
  61.         }
  62.         fflush(stdout); //flush output to screen
  63.         //usleep(50); //dont need this, especially for larger maps
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement