I want to make a maze in C , however I keep running into a problem with the right outer wall. I was wondering if you guys know a way so I can make the outer wall. Ive been trying to work from up to down using \n, but when \n is used the next symbol just goes to the left wall. Thanks in advance!
#include <iostream>
#include <vector>
#include <stack>
/* class Maze {
public:
void makeMaze();
void Print() const;
private:
std::vector <std::vector <char>> Maze;
}; */
int main(int argc, char* argv[]) {
const int WIDTH = 4;
const int HEIGHT = 4;
/* std::string seedValue = " ";
HEIGHT = atoi(argv[1]);
WIDTH = atoi(argv[2]);
if (argc > 3) {
seedValue = argv[3];
} */
// row, column
std::vector <std::vector <std::string>> Maze (WIDTH 1, std::vector<std::string> (HEIGHT 1));
// Roof
for (int column = 0; column < WIDTH; column ) {
Maze[0][column] = " ---";
}
// Left Wall
for (int row = 1; row < HEIGHT 1; row ) {
Maze[row][0] = "|\n ";
}
// Floor
for (int i = 1; i < WIDTH 1; i ) {
Maze[HEIGHT][i] = "--- ";
}
// Right Wall
// FIXME
// Print Maze
for (int i = 0; i < Maze.size(); i ) {
for (int j = 0; j < Maze.at(0).size(); j ) {
std::cout << Maze[i][j];
}
std::cout << std::endl;
}
}
CodePudding user response:
You can keep your current implementation for the most part. The only issue is \n
. Do not have any of those, they are not needed and will only give you problems. Your std::cout << std::endl;
makes a new line after each row, that's all you need. So, do not include \n
in neither the left, nor the right wall
To also "create" the right wall, you can copy what you did for the left wall, but with some minor adjustments:
// Right wall
for (int row = 1; row < HEIGHT 1; row ) {
Maze[row][WIDTH] = "|";
}
Also, what are the pluses for? (
)
You don't have to merge the strings together in order to make it work. Your std::cout << Maze[i][j];
simply adds each string to end of the previous, until you send the endl
to make a new line.
So, I would say get rid of the pluses, (unless it is for styling).
The left wall should look like this:
// Left Wall
for (int row = 1; row < HEIGHT 1; row ) {
Maze[row][0] = "|";
}
CodePudding user response:
You may want to treat your printable maze as a matrix of chars instead of strings:
- You can consider each cell of the maze border having a horizontal fill
---
and a vertical fill|
, and acell_width
andcell_height
. - Your maze would be then defined as a matrix of chars sized
maze_height * cell_height 1
andmaze_width * cell_width 1
. That extra one is needed for the right and bottom borders. - In order to fill the border, you can define two helper functions,
fill_horizontal_border_cell
andfill_vertical_border_cell
. These functions just copy the contents of the stringshorizontal_border_fill
andvertical_border_fill
respectively to the maze matrix. - Finally, you'd need to separately fill the bottom left border corner.
- All this code should be properly encapsulated into classes (e.g.
MazeView
for the printable maze,MazeBorderView
for the printable maze border, and so on).
#include <iostream> // cout
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
// Border
const std::string horizontal_border_fill{" ---"};
const std::string vertical_border_fill{" |"};
auto cell_width{horizontal_border_fill.size()};
auto cell_height{vertical_border_fill.size()};
// Maze
const size_t maze_width = 6;
const size_t maze_height = 5;
std::vector<std::vector<char>> maze(maze_height * cell_height 1,
std::vector<char>(maze_width * cell_width 1, ' ')); // 1 for the right and bottom borders
// Fill border
auto fill_horizontal_border_cell = [&maze, &horizontal_border_fill, &cell_width, &cell_height](size_t row, size_t col) {
row *= cell_height;
col *= cell_width;
for (auto& c : horizontal_border_fill) { maze[row][col ] = c; }
};
auto fill_border_vertical_cell = [&maze, &vertical_border_fill, &cell_width, &cell_height](size_t row, size_t col) {
row *= cell_height;
col *= cell_width;
for (auto& c : vertical_border_fill) { maze[row ][col] = c; }
};
for (size_t col{0}; col < maze_width; col) { // horizontal borders
fill_horizontal_border_cell(0, col); // top
fill_horizontal_border_cell(maze_height, col); // bottom
}
for (size_t row{0}; row < maze_height; row) { // vertical borders
fill_border_vertical_cell(row, 0); // top
fill_border_vertical_cell(row, maze_width); // bottom
}
maze[maze_height * cell_height][maze_width * cell_width] = horizontal_border_fill[0]; // bottom left border corner
// Print maze
for (size_t row{0}; row < maze.size(); row) {
for (size_t col{0}; col < maze[0].size(); col) {
std::cout << maze[row][col];
}
std::cout << "\n";
}
}
// Outputs:
//
// --- --- --- --- --- ---
// | |
//
// | |
//
// | |
//
// | |
//
// | |
// --- --- --- --- --- ---