在使用Opengl在C ++中构建迷宫时需要帮助 [英] Need help in building maze in c++ using opengl

查看:108
本文介绍了在使用Opengl在C ++中构建迷宫时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的解决方案路径代码,我使用了随机fn,但是每次产生相同的结果.和2,3条路径已显示,但正确的迷宫包含一条用于解决方案的路径和许多错误的路径,我该怎么办?有人告诉我

this is my code for solution path, i used random fn but every time it produces the same result. and 2,3 paths are shown but correct maze contains one path for solution and so many false paths, how can i do this? kindly someone tell me

void SolutionPath()
{
    int x=0,y=0,tempX,tempY,currentX,currentY,legSize=3,direction=RIGHT;
    while(x<mazeheight)>
    {
        while(legSize-- && x <mazeheight)>
        {
            currentX = x;
            currentY = y;
            switch(direction)
            {
                case LEFT:
                    mazeArray[x][y--]= SOLUTION_PATH;
                    break;
                case RIGHT:
                    mazeArray[x][y++]= SOLUTION_PATH;
                    break;
                case TOP:
                    mazeArray[x--][y]= SOLUTION_PATH;
                    break;
                 case BOTTOM:
                    mazeArray[x++][y]= SOLUTION_PATH;
                    break;
            }
            if(x<0||y<0||y>mazeWidth)
            {
                  x= currentX;
                  y= currentY;
                  break;
            }

        }
        tempX = x;
        tempY = y;
        int flag=0;
        do
        {
            flag++;
            legSize = (rand()%5 )+1;
            direction = (rand()%3)+1;
            switch(direction)
            {
                case LEFT:
                    tempY-=legSize;
                    break;
                case RIGHT:
                    tempY+=legSize;
                    break;
                case TOP:
                    tempX-=legSize;
                    break;
                 case BOTTOM:
                    tempX+=legSize;
                    break;
            }

        }
        while((tempX<0||tempY<0||tempY>=mazeWidth||tempX>=mazeHeight)&& flag<3);
        if(flag>=3)
            direction = (rand()%3)+1;
      }
}

推荐答案

在代码中的最后一个do循环之前,请查看变量的值.您正在不断添加tempX,但是while()表达式中的其他变量之一很可能阻止循环终止.使用调试器逐步解决此类问题,以尝试查看其中一个值或测试可能不一致的地方,总是很有用的.

出于第二种想法,您可能还需要在第一个循环中执行此操作.
Take a look at the values of your variables before the final do loop in your code. You are adding to tempX continually but it''s quite possible that one of the other variables in your while() expression is preventing the loop from terminating. It is always useful to step through problems like this with your debugger to try and see where one of your values or tests may be inconsistent.

On second thoughts you probably need to do this in your first loop also.


甜心,

我想您在随机数生成方面存在问题.可能是因为您的程序无法生成随机数.

Hi Sweety,

I guess You have a problem with random number generation.might be your program is not able to generate the random numbers.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
int i, r;

srand(time(0));

for(i = 0; i <= 2000; i++) 
{
	r = rand();
	cout << "Number is " << r << ". It was generated on try number " << i << ".\n";
}

return 0;
}



请检查上面的示例代码以生成随机数...希望它能为您提供帮助.



please check the sample code above for random number generation... hope it can help you..


这篇关于在使用Opengl在C ++中构建迷宫时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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