C++ windows graphic 制作迷宫游戏~求助求助~!!!!!

jingyao 2013-10-07 05:34:13


请问高手 如何构建图像上的 迷宫 一下是我 初步做到的 代码,不知道该怎么写下去了....

#include <string>
#include "ccc_win.h" // for graphics classes and functions

const int MAZE_SIZE = 16;

using namespace std;

class MazeSquare
{
public:
bool leftWall, rightWall, bottomWall, topWall;
bool visited;
int steps;
MazeSquare() // constructor
{
Initialise();
}
void Initialise(void) // reinitialise a square for a new maze
{
leftWall = true; // create the maze square with all the walls
rightWall = true;
bottomWall = true;
topWall = true;
visited = false; // the robot has not visited the square yet
steps = 256; // greater than maximum possible number of steps
}
};
void CreateMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE])
{
for (int i = 0; i < MAZE_SIZE; i++)
{
cwin <<
}
}



int ccc_win_main() // main function for a graphics program
{
MazeSquare maze[MAZE_SIZE][MAZE_SIZE]; // maze design
int x = 0, y = 0; // robot position
bool exit = false; // flag to control end of program
// initialise the random number generator
srand((unsigned int)(time(NULL)));

cwin.coord(-3,22,22,-4);

PenWidth pen3 = PenWidth(2);
PenColour blackPen = PenColour(0, 0, 0);

Point origin(0,0);
Point p1(0,16);
Point p2(16,0);
Point p3(16,16);
Circle smallCircle(origin,0.5);
// graphics object.
Line down_horizontal(origin,p2);
Line top_horizontal(p2,p3);
Line vertical_left(origin,p1);
Line vertical_right(p1,p3);
// draw the maze.
cwin << pen3 << blackPen << down_horizontal << top_horizontal << vertical_left << vertical_right;

Point p = cwin.get_mouse("Click a button or move the robot");
CreateMaze(maze);




return 0;
}


...全文
221 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Defonds 2013-10-08
  • 打赏
  • 举报
回复
这个很成熟了,找个自己看看
woshinia 2013-10-08
  • 打赏
  • 举报
回复
生成迷宫还是要靠算法的吧,自己随便想的太丑陋了。 一般1,搜索。先把墙填满,然后遍历按一定的随机规则拆墙,检查拆墙后迷宫是否符合规则,不符合即回溯,符合继续拆。未处理的区块按有入口无出口的规则,同理遍历。 2,图论。把所有的空格当成结点,构造一个最小生成树。 现成代码网上也不少,自己搜吧。
赵4老师 2013-10-08
  • 打赏
  • 举报
回复
jingyao 2013-10-08
  • 打赏
  • 举报
回复
如何使用随机函数 Rand()拆墙
jingyao 2013-10-08
  • 打赏
  • 举报
回复
已经定义了 maze[16][16] 如何用 rand() 函数生产每一个小方块,然后用bool return False(没有墙)ture (有墙) 来生成 迷宫???
cmztestat 2013-10-07
  • 打赏
  • 举报
回复
我说下我的思路: (1)把迷宫分为m * n的数组(maze[m][n]); (2)数组的类型为unsigned char; (3)定义如下宏: #define TOPBLOCK 0x01; #define BOTTOMBLOCK 0x02; #define LEFTBLOCK 0x04; #define RIGHTBLOCK 0x08; (4)设置unsigned char的属性如下: 举个栗子说明当指定的块上和左右不通时为:maze[0][0] = TOPBLOCK | LEFTBLOCK | RIGHTBLOCK; (5)当用户传达方向键信息时(way表示用户按下的方向),再举个栗子说明: if(way == TOP && !TOPBLOCK) //这写错了if(way == top && !(maze & TOPBLOCK)) { //... } (6)当用户进入特定快时完成; 希望能帮到你,我只是提出个思路,没实践过.
cmztestat 2013-10-07
  • 打赏
  • 举报
回复
我说下我的思路: (1)把迷宫分为m * n的数组(maze[m][n]); (2)数组的类型为unsigned char; (3)定义如下宏: #define TOPBLOCK 0x01; #define BOTTOMBLOCK 0x02; #define LEFTBLOCK 0x04; #define RIGHTBLOCK 0x08; (4)设置unsigned char的属性如下: 举个栗子说明当指定的块上和左右不通时为:maze[0][0] = TOPBLOCK | LEFTBLOCK | RIGHTBLOCK; (5)当用户传达方向键信息时(way表示用户按下的方向),再举个栗子说明: if(way == TOP && !TOPBLOCK) { //... } (6)当用户进入特定快时完成; 希望能帮到你,我只是提出个思路,没实践过.
凌典 2013-10-07
  • 打赏
  • 举报
回复
没有人会闲下来帮你做题目,你得把你在哪里卡住了的拿出来讨论
  • 打赏
  • 举报
回复
可以参照一下这个开源库 http://en.wikipedia.org/wiki/Maze_generation_algorithm
jingyao 2013-10-07
  • 打赏
  • 举报
回复
以下 是 题目的一部分要求 To complete this program it is necessary to have a good design for storing the maze layout. Good programs store data about a thing in a way that closely resembles the thing itself. The maze is on a grid of 16 x 16 squares, so information about the array should be in a 16 x 16 array. For each square, the following information needs to be stored:  For each of the 4 walls on the square, a Boolean variable is needed to represent whether the wall is present (true) or absent (false)  A Boolean variable is needed to represent whether the robot has visited this square. This will allow the program to display the robot’s path through the maze.  An integer is needed to store the smallest number of steps to reach the square from the start of the maze. This is used to solve the maze. We will use a simple class for each square: class MazeSquare { public: bool leftWall, rightWall, bottomWall, topWall; bool visited; int steps; MazeSquare() // constructor { Initialise(); } void Initialise(void) // reinitialise a square for a new maze { leftWall = true; // create the maze square with all the walls rightWall = true; bottomWall = true; topWall = true; visited = false; // the robot has not visited the square yet steps = 256; // greater than maximum possible number of steps } }; Creating the Maze Use the random number generator to remove walls from inside the maze. To simplify the program logic, do not remove any of the outer walls. The robot will move from the bottom left corner of the maze to the top right corner. To be likely to get a suitable maze, you need to remove about 60% of the walls. The random number function rand() returns an integer random number between 0 and RAND_MAX. Therefore, for each internal wall, generate a random number, and remove the wall if the random number if less than 0.6 * RAND_MAX, otherwise leave the wall in place. Remember that each internal wall is a side of two adjacent squares, so if the wall is removed from one square it must also be removed from the appropriate adjacent square. To access a wall from the maze array use expressions like maze[px][py].leftWall to access the wall at maze position px, py. For example, to remove the left wall use: maze[px][py].leftWall = false; Then you must also remove the right wall of the square to the left: maze[px - 1][py].rightWall = false; Remember the array indexes both start at zero and the maximum array index value is MAZE_SIZE – 1. 705003 Engineering Computing 2013 Assignment 3 (Version 2) Page 4 Drawing the Window In graphics programs, it is simplest to clear and redraw the window completely whenever anything changes in the display. Otherwise it can be hard to know which parts of the window display need to be redrawn. Write a separate function that clears the graphics window and completely redraws it. Call this function whenever the display needs to be redrawn. In the graphics system used here, you can choose your own coordinate system for the window by calling function cwin.coord. Select a coordinate system to simplify the program coding. For example, choose units so that each maze square is size 1 x 1 and that the squares are located at integer x and y positions. Also, try to make the buttons an integer size and located at integer coordinates. Draw the robot as a coloured circle in the maze, and draw the path it has followed as a series of circles (or some other shape) of a different colour in each square the robot has visited.

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧