Aug 19, 2017 at 3:48pm UTC
I need help making my maze look better.I want to use unicode characters but my program crashes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#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;
}
Please help me display my maze using ▮.
Last edited on Aug 19, 2017 at 3:50pm UTC
Aug 19, 2017 at 3:58pm UTC
Do you want to display ▮ where you currently display #?
On non-WIndows OS, just replace line 49 with
cout << "[" << (arr[x][y] == '#' ? "▮" : " " ) << "]" ;
(which goes a small way towards separating data model from presentation)
$ ./test
Rows 5
Columns10
[ ][ ][ ][ ][ ][▮][ ][ ][ ][▮]
[▮][▮][▮][▮][ ][▮][ ][▮][ ][ ]
[ ][ ][ ][ ][ ][▮][ ][ ][▮][ ]
[▮][ ][▮][▮][▮][ ][ ][▮][▮][ ]
[ ][ ][ ][ ][ ][ ][▮][ ][ ][ ]
on Windows, there are the usual complications of twisting its arm to support any sort of Unicode, but msvc2017/win10 should be enough to only do two things: use u8"▮" instead of "▮", and call SetConsoleOutputCP(CP_UTF8); as I was just showing in
http://www.cplusplus.com/forum/general/220252/#msg1013553
Last edited on Aug 19, 2017 at 3:59pm UTC
Aug 19, 2017 at 4:14pm UTC
I use msvc2017/win10. For some reason the console outputs rectangle with question marks instead of ▮.
Aug 19, 2017 at 4:42pm UTC
For unicode characters, you need to use wchar_t
and std::wcout
. Also note that console and unicode characters don't mix together very well.
Last edited on Aug 19, 2017 at 4:42pm UTC