Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i am doing a project in uni with C and it is generating a maze but the middle characters are weird (random ascii characters) and i do not know where they come from or how are they generated. I wanted to fill everything with the ascii character described in k but the inner walls are not being defined by this. Also how can i assure that there is an entrance and an exit of my maze?

What I have tried:

C++
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define N 20

void gerador_labirinto(char a[N][N]);

int main(void)
{
char maze[N][N];
srand((unsigned int)time(NULL));
gerador_labirinto(maze);
return 0;
}

void gerador_labirinto(char a[N][N])
{
size_t linha,coluna = 0, r, k=(char)254;

for ( linha = 0; linha < N; ++linha )
{
    a[linha][coluna] = k;
}   

for ( linha = 0; linha < N; ++linha )
{
    a[linha][N - 1] = k;
}

linha = rand() % 20 + 1;
a[linha][0] = k;

linha = rand() % 20 + 1;
a[linha][N - 1] = k;

for (coluna = 1; coluna < N - 1; ++coluna)
{
    a[0][coluna] = k;
}

for (coluna = 1; coluna < N - 1; ++coluna)
{
    a[N - 1][coluna] = k;
}

puts("");
for (linha = 0; linha < N; ++linha)
{
    for (coluna = 0; coluna < N; ++coluna)
    {
        printf_s("%2c",a[linha][coluna]);
    }
    puts("");
}
puts("");
}
Posted
Updated 18-Dec-18 4:50am
v2

You only set the borders of your maze to a specific value - the bulk of the maze you leave uninitialised. Start like this:
C++
void gerador_labirinto(char a[N][N])
    {
    size_t linha,coluna = 0, r, k=(char)254;
    // Clear area
    for ( linha = 1; linha < N - 1; ++linha )
       {
       for (coluna = 1; coluna < N - 1; coluna++)       
           {
           a[linha][coluna] = ' ';
           }
       }
    // Add wall round all edges
    for ( linha = 0; linha < N; ++linha )
       {
       a[linha][0] = k;
       a[linha][N - 1] = k;
       }   
    for ( coluna = 0; coluna < N; ++coluna )
       {
       a[0][coluna] = k;
       a[N - 1][coluna] = k;
       }

And it'll be ready for you to add walls inside!
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
If you need to fullfill some specific features (like entry or exit of the maze) than you need to implement it. Write a check functions which proofes the features and when not you need some correcting code. Meta code:
C++
if( !IsDataCorrect() ( {
  FixIt();
} else {
  LogMessage("all fine");
}
Always assume the possibility that something goes as Murphys law postulates it.

tip: use some structs for better data handling and write output functions
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900