迷宫生成和unicode字符

时间:2017-08-18 23:26:45

标签: c++ unicode maze

我是c ++的新手,需要帮助让我的迷宫看起来更好。我想使用unicode字符但我的程序崩溃了。这是我随机迷宫的照片。 image of my maze

您可以在此处查看我的代码

#include <iostream>
#include <ctime>
#include <cstring>
using std::cout;
using std::fill;
using std::endl;
using std::cin;
using std::cout;

    int main()
    {
        srand(time(NULL));
        int arr_x[1000] = { 0 }; fill(arr_x + 0, arr_x + 1000, -1);
        int arr_y[1000] = { 0 }; fill(arr_y + 0, arr_y + 1000, -1);
//coordinates used to go back when the generation runs into blocked path 


        char arr[100][100]; memset(arr, '#', sizeof(char) * 100 * 100);
    //creating 100x100 char array of #
        int rows, cols = 0;
        cout << "Rows "; cin >> rows;//choose 
        cout << "Columns"; cin >> cols;

        int random_number = 0; int x = 0; int y = 0; int index = 0;
        arr[0][0] = ' ';

        while (1)
        {

            random_number = rand() % (4) + 1;
            int available_paths = 0;
            if (x + 1 >= 0 && x + 1 < rows && arr[x + 1][y] != ' ' && arr[x + 2][y] != ' ' && arr[x + 1][y + 1] != ' ' && arr[x + 1][y - 1] != ' ') { available_paths++; if (random_number == 1) { x = x + 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
            if (x - 1 >= 0 && x - 1 < rows && arr[x - 1][y] != ' ' && arr[x - 2][y] != ' ' && arr[x - 1][y + 1] != ' ' && arr[x - 1][y - 1] != ' ') { available_paths++; if (random_number == 2) { x = x - 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
            if (y + 1 >= 0 && y + 1 < cols && arr[x][y + 1] != ' ' && arr[x][y + 2] != ' ' && arr[x + 1][y + 1] != ' ' && arr[x - 1][y + 1] != ' ') { available_paths++; if (random_number == 3) { y = y + 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
            if (y - 1 >= 0 && y - 1 < cols && arr[x][y - 1] != ' ' && arr[x][y - 2] != ' ' && arr[x + 1][y - 1] != ' ' && arr[x - 1][y - 1] != ' ') { available_paths++; if (random_number == 4) { y = y - 1; arr_x[index] = x; arr_y[index] = y; arr[x][y] = ' '; index++; } }
            if (available_paths == 0) //if available_paths are 0 I need to go 
            {                         //back to the last visited square
                index = index - 1;
                if (index < 0) { break; }
                x = arr_x[index]; y = arr_y[index];
            }
        }


        for (int x = 0; x < rows; x++)
        {
            for (int y = 0; y < cols; y++)
            {
                cout << "[" << arr[x][y] << "]";
                if (y == (cols - 1)) { cout << endl; }
            }
        }

        int end;
        cin >> end;
    }

我正在考虑使用这个角色 - ▮。有没有简单的方法来改进我的算法?

0 个答案:

没有答案