Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cstdio> //atoi
- #include <cstdlib>
- #include <windows.h>
- //Суть
- //1) Ходим во все доступные стороны
- //2) Если нет ходов, то идём туда от куда пришли
- enum eDirection { STOP = 0, UP, LEFT, RIGHT, DOWN };
- void MessageInfo(char* str){
- printf("[INFO]%s\n", str);
- }
- class Block {
- private:
- int xPos, yPos;
- bool up, down, left, right, isEmpty;
- eDirection Back = STOP;
- public:
- Block(int xPos, int yPos){
- this->xPos = xPos;
- this->yPos = yPos;
- up, down, left, right = false;
- Back = STOP;
- MessageInfo("Block Created!");
- }
- //Возвращает позицию блока в пространстве
- int[2] getPos(){
- return {xPos, yPos};
- }
- int setPos(int xPos, int yPos){
- this->xPos = xPos;
- this->yPos = yPos;
- }
- bool setBack(eDirection Back){
- if(Back != STOP){
- this->Back = Back;
- return true;
- }
- return false;
- }
- eDirection getBack(){
- return Back;
- }
- void setSide(eDirection dir, bool state){
- switch(dir){
- case UP:
- this->up = state;
- break;
- case LEFT:
- this->left = state;
- break;
- case RIGHT:
- this->right = state;
- break;
- case DOWN:
- this->down = state;
- break;
- default:
- break;
- }
- }
- bool getSide(eDirection dir){
- switch(dir){
- case UP:
- return this->up;
- break;
- case LEFT:
- return this->left;
- break;
- case RIGHT:
- return this->right;
- break;
- case DOWN:
- return this->down;
- break;
- default:
- return false;
- break;
- }
- }
- char getSymbol(){
- if (up && down && left && right) return '+';
- if (up && down) return '|';
- if (left && right) return '-';
- if (left && up) return '/';
- if (up && right) return '\\';
- if (right && down) return '/';
- if (down && left) return '\\';
- if (up) return '#';
- if (down) return '#';
- if (left) return '#';
- if (right) return '#';
- return ' ';
- }
- };
- class Maze {
- public:
- Block** blocks;
- int xSize, ySize, xPos, yPos;
- Maze(int xSize, int ySize, int xPos, int yPos){
- blocks = new Block*[xSize];
- for (int i = 0; i < xSize; i++)
- blocks[i] = new Block[ySize];
- this->xPos = xPos;
- this->yPos = yPos;
- MessageInfo("Maze created!");
- }
- //Получение блока через координаты
- Block getBlock(int xPos, int yPos){
- if (xPos >= 0 || xPos < xSize || yPos >= 0 || yPos < ySize)
- return blocks[xPos][yPos];
- MessageInfo("Выход за рамки массива!");
- return NULL;
- }
- //Получение блока относительно активной координаты
- Block getBlock(eDirection dir){
- switch(dir){
- case 1: //UP
- if (this->yPos - 1 >= 0)
- return blocks[this->yPos - 1][this->xPos];
- break;
- case 2: //LEFT
- if (this->xPos - 1 >= 0)
- return blocks[this->yPos][this->xPos - 1];
- break;
- case 3: //RIGHT
- if (this->xPos + 1 < this->xSize)
- return blocks[this->yPos][this->xPos + 1];
- break;
- case 4: //DOWN
- if (this->yPos + 1 < this->ySize)
- return blocks[this->yPos + 1][this->xPos];
- break;
- default:
- break;
- }
- return NULL;
- }
- //Доступные стороны
- bool[4] getAllowSide(int xPos, int yPos){
- bool Side[4] = false;
- //for (int i = 0; i < 4; i++) Side[i] = false;
- if (getBlock(xPos, yPos - 1).getBack() == STOP) Side[0] = true;
- if (getBlock(xPos - 1, yPos).getBack() == STOP) Side[1] = true;
- if (getBlock(xPos + 1, yPos).getBack() == STOP) Side[2] = true;
- if (getBlock(xPos, yPos + 1).getBack() == STOP) Side[3] = true;
- return Side;
- }
- //Генератор рандомных сторон из доступных
- eDirection RandomSide(bool* allow){
- int Size = 0;
- for (int i = 0; i < 4; i++)
- if (allow[i])
- Size++;
- int* arr = new int[Size];
- int j = 0;
- for(int i = 0; i < 4; i++){
- if (allow[i]){
- arr[j] = i;
- j++;
- }
- }
- switch(arr[rand() % Size]){ //Костыль!!11
- case 1:
- return UP;
- break;
- case 2:
- return LEFT;
- break;
- case 3:
- return RIGHT;
- break;
- case 4:
- return DOWN;
- break;
- default:
- return STOP;
- break;
- }
- }
- //Сделать шаг в каком либо направлении.
- bool MoveBlock(eDirection dir){
- switch(dir){
- case UP:
- if (yPos - 1 >= 0) {
- getBlock(xPos, yPos).setSide(UP, true);
- yPos--;
- } else
- return false;
- break;
- case LEFT:
- if (xPos - 1 >= 0) {
- getBlock(xPos, yPos).setSide(LEFT, true);
- xPos--;
- } else
- return false;
- break;
- case RIGHT:
- if (xPos + 1 < xSize) {
- getBlock(xPos, yPos).setSide(RIGHT, true);
- xPos++;
- } else
- return false;
- break;
- case DOWN:
- if (yPos + 1 < xSize) {
- getBlock(xPos, yPos).setSide(DOWN, true);
- yPos++;
- } else
- return false;
- break;
- default:
- return false;
- break;
- }
- return true;
- }
- void MazeGen(){
- system("cls");
- printf("Maze GEN!");
- bool GEN = true;
- int Step = 0;
- int xStart = xPos;
- int yStart = yPos;
- while(GEN) {
- eDirection dir = RandomSide(getAllowSide(xPos, yPos));
- MessageInfo("")
- MoveBlock(dir);
- switch(dir){
- case UP: //UP
- getBlock(xPos, yPos).setDir(DOWN);
- getBlock(xPos, yPos).setSide(DOWN, true);
- break;
- case LEFT: //LEFT
- getBlock(xPos, yPos).setDir(RIGHT);
- getBlock(xPos, yPos).setSide(RIGHT, true);
- break;
- case RIGHT: //RIGHT
- getBlock(xPos, yPos).setDir(LEFT);
- getBlock(xPos, yPos).setSide(LEFT, true);
- break;
- case DOWN: //DOWN
- getBlock(xPos, yPos).setDir(UP);
- getBlock(xPos, yPos).setSide(UP, true);
- break;
- default:
- break;
- }
- printf("Block(%d, %d): Move to: %d", xPos, yPos, dir);
- if (dir == STOP){
- MoveBlock(getBlock(xPos, yPos).getDir());
- printf("Block(%d, %d): MoveBack: %d", xPos, yPos, dir);
- }
- if(dir == STOP && xPos == xStart && yPos == yStart)
- break;
- }
- }
- void Draw(){
- system("cls");
- for (int i = 0; i < xSize; i++){
- for (int j = 0; j < ySize; j++){
- printf("%s", blocks[i][j].getSymbol());
- }
- printf("\n");
- }
- }
- };
- int main()
- {
- Maze maze1(10, 10, 0, 0);
- maze1.MazeGen();
- maze1.Draw();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement