C中的迷宫生成代码生成奇怪的字符 [英] Maze generation code in C generating weird characters

查看:111
本文介绍了C中的迷宫生成代码生成奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用C进行uni项目并且它正在生成迷宫,但中间字符很奇怪(随机的ascii字符),我不知道它们来自何处或它们是如何生成的。我想用k中描述的ascii字符填充所有内容,但内墙不是由此定义的。另外我怎么能确保我的迷宫有一个入口和一个出口?



我尝试了什么:



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:

#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("");
}

推荐答案

您只需将迷宫的边框设置为特定值 - 您未经初始化的大部分迷宫。从这样开始:

You only set the borders of your maze to a specific value - the bulk of the maze you leave uninitialised. Start like this:
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!


你的代码没有表现出来你期望的方式,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道您的代码应该做什么,它没有发现错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。
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.


如果您需要填写一些特定功能(如进入或退出迷宫),而不是实现它。写一个检查功能,证明功能,什么时候不需要一些纠正代码。元代码:
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:
if( !IsDataCorrect() ( {
  FixIt();
} else {
  LogMessage("all fine");
}

总是假设某些内容可能符合 Murphys law 假设它。



提示:使用一些结构来获得更好的数据处理和写输出功能

Always assume the possibility that something goes as Murphys law postulates it.

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


这篇关于C中的迷宫生成代码生成奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆