Maze believe that everyone has passed, after all, books ah what what the top will have a maze, the main test your logical thinking. Then we learn C/s also need to learn logical way of thinking, then today I would like to share, how to use C + + to create a simple random maze game. (Code words I only intercepted how to create a maze of code, if you want a full set of code can add group: 558502932, the group has a lot of C + + learning materials to provide learning, we communicate progress)
The full version of the Maze game effect is as follows:
The code is as follows:
Voidcreatemaze (Intx,inty)
Intdir[4][2] = {0, 1, 1, 0, 0,-1,-1, 0};//answer error
Intj,temp;//temp Temporary variable
for (inti = 0; i < 4; i++)
J =rand ()% 4;//0-3 the subscript of the array//j the same time it will never change.
Map[x][y] = road;//Assigns this point to the road
for (inti = 0; i < 4; i++)
if (map[x + 2 * dir[i][0]][y + 2 * dir[i][1]] (= = WALL)/Up 0 1//Next Door
Map[x + Dir[i][0]][y + dir[i][1]] = ROAD;
Createmaze (x + 2*dir[i][0], y + 2*dir[i][1]);
Srand ((Unsignedint) time (NULL));
for (inti = 0; I <= height+1; i++)
for (INTJ = 0; J <= WIDTH + 1; j + +)
if (i = = 0 | | | i = = HEIGHT + 1 | | j = = 0 | | j = = WIDTH + 1)
Createmaze (2 * (rand ()% (HEIGHT/2) + 1), 2 * (rand ()% (WIDTH/2) + 1));
for (inti = 0; I <= HEIGHT + 1; i++)
Map[i][width + 1] = WALL;
for (inti = 0; I <=WIDTH+1; i++)
Map[height + 1][i] = WALL;
Draw the entrance and exit
Map[height-1][width] = end;
for (inti = 1; I <= HEIGHT; i++)
for (INTJ = 1; J <= WIDTH; j + +)
|
This is just a part of the code, so you copy and paste is useless, give you the code is only the role of guidance, mainly you want to know programming ideas and programming ideas, I hope you can learn C/s + + C + +.