Advertisement
Usow_Maxim

MazeGen_0.1

Oct 27th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cstdio> //atoi
  3. #include <cstdlib>
  4. #include <windows.h>
  5.  
  6. //Суть
  7. //1) Ходим во все доступные стороны
  8. //2) Если нет ходов, то идём туда от куда пришли
  9.  
  10. enum eDirection { STOP = 0, UP, LEFT, RIGHT, DOWN };
  11.  
  12. void MessageInfo(char* str){
  13.     printf("[INFO]%s\n", str);
  14. }
  15.  
  16. class Block {
  17. private:
  18.     int xPos, yPos;
  19.     bool up, down, left, right, isEmpty;
  20.     eDirection Back = STOP;
  21. public:
  22.     Block(int xPos, int yPos){
  23.         this->xPos = xPos;
  24.         this->yPos = yPos;
  25.         up, down, left, right = false;
  26.         Back = STOP;
  27.         MessageInfo("Block Created!");
  28.     }
  29.  
  30.     //Возвращает позицию блока в пространстве
  31.     int[2] getPos(){
  32.         return {xPos, yPos};
  33.     }
  34.  
  35.     int setPos(int xPos, int yPos){
  36.         this->xPos = xPos;
  37.         this->yPos = yPos;
  38.     }
  39.  
  40.     bool setBack(eDirection Back){
  41.         if(Back != STOP){
  42.             this->Back = Back;
  43.             return true;
  44.         }
  45.         return false;
  46.     }
  47.  
  48.     eDirection getBack(){
  49.         return Back;
  50.     }
  51.  
  52.     void setSide(eDirection dir, bool state){
  53.         switch(dir){
  54.             case UP:
  55.                 this->up = state;
  56.                 break;
  57.             case LEFT:
  58.                 this->left = state;
  59.                 break;
  60.             case RIGHT:
  61.                 this->right = state;
  62.                 break;
  63.             case DOWN:
  64.                 this->down = state;
  65.                 break;
  66.             default:
  67.                 break;
  68.         }
  69.     }
  70.  
  71.     bool getSide(eDirection dir){
  72.         switch(dir){
  73.             case UP:
  74.                 return this->up;
  75.                 break;
  76.             case LEFT:
  77.                 return this->left;
  78.                 break;
  79.             case RIGHT:
  80.                 return this->right;
  81.                 break;
  82.             case DOWN:
  83.                 return this->down;
  84.                 break;
  85.             default:
  86.                 return false;
  87.                 break;
  88.         }
  89.     }
  90.  
  91.     char getSymbol(){
  92.         if (up && down && left && right) return '+';
  93.         if (up && down) return '|';
  94.         if (left && right) return '-';
  95.         if (left && up) return '/';
  96.         if (up && right) return '\\';
  97.         if (right && down) return '/';
  98.         if (down && left) return '\\';
  99.         if (up) return '#';
  100.         if (down) return '#';
  101.         if (left) return '#';
  102.         if (right) return '#';
  103.         return ' ';
  104.     }
  105. };
  106.  
  107. class Maze {
  108. public:
  109.     Block** blocks;
  110.     int xSize, ySize, xPos, yPos;
  111.  
  112.     Maze(int xSize, int ySize, int xPos, int yPos){
  113.         blocks = new Block*[xSize];
  114.         for (int i = 0; i < xSize; i++)
  115.             blocks[i] = new Block[ySize];
  116.         this->xPos = xPos;
  117.         this->yPos = yPos;
  118.         MessageInfo("Maze created!");
  119.     }
  120.  
  121.     //Получение блока через координаты
  122.     Block getBlock(int xPos, int yPos){
  123.         if (xPos >= 0 || xPos < xSize || yPos >= 0 || yPos < ySize)
  124.             return blocks[xPos][yPos];
  125.         MessageInfo("Выход за рамки массива!");
  126.         return NULL;
  127.     }
  128.  
  129.     //Получение блока относительно активной координаты
  130.     Block getBlock(eDirection dir){
  131.         switch(dir){
  132.             case 1: //UP
  133.                 if (this->yPos - 1 >= 0)
  134.                     return blocks[this->yPos - 1][this->xPos];
  135.                 break;
  136.             case 2: //LEFT
  137.                 if (this->xPos - 1 >= 0)
  138.                     return blocks[this->yPos][this->xPos - 1];
  139.                 break;
  140.             case 3: //RIGHT
  141.                 if (this->xPos + 1 < this->xSize)
  142.                     return blocks[this->yPos][this->xPos + 1];
  143.                 break;
  144.             case 4: //DOWN
  145.                 if (this->yPos + 1 < this->ySize)
  146.                     return blocks[this->yPos + 1][this->xPos];
  147.                 break;
  148.             default:
  149.                 break;
  150.         }
  151.         return NULL;
  152.     }
  153.  
  154.     //Доступные стороны
  155.     bool[4] getAllowSide(int xPos, int yPos){
  156.         bool Side[4] = false;
  157.         //for (int i = 0; i < 4; i++) Side[i] = false;
  158.         if (getBlock(xPos, yPos - 1).getBack() == STOP) Side[0] = true;
  159.         if (getBlock(xPos - 1, yPos).getBack() == STOP) Side[1] = true;
  160.         if (getBlock(xPos + 1, yPos).getBack() == STOP) Side[2] = true;
  161.         if (getBlock(xPos, yPos + 1).getBack() == STOP) Side[3] = true;
  162.         return Side;
  163.     }
  164.     //Генератор рандомных сторон из доступных
  165.     eDirection RandomSide(bool* allow){
  166.         int Size = 0;
  167.         for (int i = 0; i < 4; i++)
  168.             if (allow[i])
  169.                 Size++;
  170.         int* arr = new int[Size];
  171.  
  172.         int j = 0;
  173.         for(int i = 0; i < 4; i++){
  174.             if (allow[i]){
  175.                 arr[j] = i;
  176.                 j++;
  177.             }
  178.         }
  179.         switch(arr[rand() % Size]){ //Костыль!!11
  180.             case 1:
  181.                 return UP;
  182.                 break;
  183.             case 2:
  184.                 return LEFT;
  185.                 break;
  186.             case 3:
  187.                 return RIGHT;
  188.                 break;
  189.             case 4:
  190.                 return DOWN;
  191.                 break;
  192.             default:
  193.                 return STOP;
  194.                 break;
  195.         }
  196.     }
  197.  
  198.     //Сделать шаг в каком либо направлении.
  199.     bool MoveBlock(eDirection dir){
  200.         switch(dir){
  201.             case UP:
  202.                 if (yPos - 1 >= 0) {
  203.                     getBlock(xPos, yPos).setSide(UP, true);
  204.                     yPos--;
  205.                 } else
  206.                     return false;
  207.                 break;
  208.             case LEFT:
  209.                 if (xPos - 1 >= 0) {
  210.                     getBlock(xPos, yPos).setSide(LEFT, true);
  211.                     xPos--;
  212.                 } else
  213.                     return false;
  214.                 break;
  215.             case RIGHT:
  216.                 if (xPos + 1 < xSize) {
  217.                     getBlock(xPos, yPos).setSide(RIGHT, true);
  218.                     xPos++;
  219.                 } else
  220.                     return false;
  221.                 break;
  222.             case DOWN:
  223.                 if (yPos + 1 < xSize) {
  224.                     getBlock(xPos, yPos).setSide(DOWN, true);
  225.                     yPos++;
  226.                 } else
  227.                     return false;
  228.                 break;
  229.             default:
  230.                 return false;
  231.                 break;
  232.         }
  233.         return true;
  234.     }
  235.  
  236.     void MazeGen(){
  237.         system("cls");
  238.         printf("Maze GEN!");
  239.         bool GEN = true;
  240.         int Step = 0;
  241.         int xStart = xPos;
  242.         int yStart = yPos;
  243.         while(GEN) {
  244.             eDirection dir = RandomSide(getAllowSide(xPos, yPos));
  245.             MessageInfo("")
  246.             MoveBlock(dir);
  247.             switch(dir){
  248.                 case UP: //UP
  249.                     getBlock(xPos, yPos).setDir(DOWN);
  250.                     getBlock(xPos, yPos).setSide(DOWN, true);
  251.                     break;
  252.                 case LEFT: //LEFT
  253.                     getBlock(xPos, yPos).setDir(RIGHT);
  254.                     getBlock(xPos, yPos).setSide(RIGHT, true);
  255.                     break;
  256.                 case RIGHT: //RIGHT
  257.                     getBlock(xPos, yPos).setDir(LEFT);
  258.                     getBlock(xPos, yPos).setSide(LEFT, true);
  259.                     break;
  260.                 case DOWN: //DOWN
  261.                     getBlock(xPos, yPos).setDir(UP);
  262.                     getBlock(xPos, yPos).setSide(UP, true);
  263.                     break;
  264.                 default:
  265.                     break;
  266.             }
  267.             printf("Block(%d, %d): Move to: %d", xPos, yPos, dir);
  268.             if (dir == STOP){
  269.                 MoveBlock(getBlock(xPos, yPos).getDir());
  270.                 printf("Block(%d, %d): MoveBack: %d", xPos, yPos, dir);
  271.             }
  272.             if(dir == STOP && xPos == xStart && yPos == yStart)
  273.                 break;
  274.         }
  275.     }
  276.  
  277.     void Draw(){
  278.         system("cls");
  279.         for (int i = 0; i < xSize; i++){
  280.             for (int j = 0; j < ySize; j++){
  281.                 printf("%s", blocks[i][j].getSymbol());
  282.             }
  283.             printf("\n");
  284.         }
  285.     }
  286. };
  287.  
  288. int main()
  289. {
  290.     Maze maze1(10, 10, 0, 0);
  291.     maze1.MazeGen();
  292.     maze1.Draw();
  293.     return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement