Advertisement
linuxgnuru

arduino

Dec 10th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. typedef struct
  2. {
  3.   int x, y;
  4.   void *p;
  5.   int c;
  6.   char dirs;
  7. } Node;
  8.  
  9. static Node *nodes;
  10. const int width  = 9;
  11. const int height = 9;
  12.  
  13. void setup()
  14. {
  15.   Serial.begin(9600);
  16.   makeMap();
  17. }
  18.  
  19. void loop()
  20. {
  21. }
  22.  
  23. void map_init()
  24. {
  25.   static Node *n;
  26.  
  27.   nodes = new Node[width * height];
  28.   for (int i = 0; i < width; i++)
  29.   {
  30.     for (int j = 0; j < height; j++)
  31.     {
  32.       n = nodes + i + j * width;
  33.       if (i * j % 2)
  34.       {
  35.         n->x = i;
  36.         n->y = j;
  37.         n->dirs = 15;
  38.         n->c = 0;
  39.       }
  40.       else n->c = 1;
  41.     }
  42.   }
  43. }
  44.  
  45. Node *link(Node *n)
  46. {
  47.   int x = 0, y = 0;
  48.   char dir;
  49.   Node *dest;
  50.   if (n == NULL) return NULL;
  51.   while (n->dirs)
  52.   {
  53.     dir = (1 << random(4));
  54.     if (~n->dirs & dir) continue;
  55.     n->dirs &= ~dir;
  56.     switch (dir)
  57.     {
  58.       case 1:
  59.         if (n->x + 2 < _width)
  60.         {
  61.           x = n->x + 2;
  62.           y = n->y;
  63.         }
  64.         else continue;
  65.         break;
  66.       case 2:
  67.         if (n->y + 2 < _height)
  68.         {
  69.           x = n->x;
  70.           y = n->y + 2;
  71.         }
  72.         else continue;
  73.         break;
  74.       case 4:
  75.         if (n->x - 2 >= 0)
  76.         {
  77.           x = n->x - 2;
  78.           y = n->y;
  79.         }
  80.         else continue;
  81.         break;
  82.       case 8:
  83.         if (n->y - 2 >= 0)
  84.         {
  85.           x = n->x;
  86.           y = n->y - 2;
  87.         }
  88.         else continue;
  89.         break;
  90.     }
  91.     dest = nodes + x + y * width;
  92.     if (dest->c == 0)
  93.     {
  94.       if (dest->parent != NULL) continue;
  95.       dest->p = n;
  96.       nodes[n->x + (x - n->x) / 2 + (n->y + (y - n->y) / 2) * width].c = 0;
  97.       return dest;
  98.     }
  99.   }
  100.   return (Node *)n->p;
  101. }
  102.  
  103. void makeMap()
  104. {
  105.   static Node *start, *last;
  106.  
  107.   randomSeed(analogRead(A1));
  108.   map_init();
  109.   start = nodes + 1 + width;
  110.   start->p = start;
  111.   last = start;
  112.   while ((last = link(last)) != start);
  113.   Serial.println();
  114.   draw();
  115. }
  116.  
  117. void draw()
  118. {
  119.   for (int i = 0; i < height; i++)
  120.   {
  121.     for (int j = 0; j < width; j++)
  122.     {
  123.       if (nodes[j + i * width].c == 1) Serial.print(F("#"));
  124.       else Serial.print(F(" "));
  125.     }
  126.     Serial.println();
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement