The example code of the Random Generation maze game in C + + language implementation

Source: Internet
Author: User
Tags rand

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:

Create a Maze
Voidcreatemaze (Intx,inty)
{
Define 4 Directions
Intdir[4][2] = {0, 1, 1, 0, 0,-1,-1, 0};//answer error
Upset the direction
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.
temp = dir[i][0];
Dir[i][0] = dir[j][0];
DIR[J][0] = temp;
temp = dir[i][1];
DIR[I][1] = dir[j][1];
DIR[J][1] = temp;
}
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]);
}
}
}
Intmain ()
{
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)
{
MAP[I][J] = ROAD;
}
Else
{
MAP[I][J] = WALL;
}
}
}
Createmaze (2 * (rand ()% (HEIGHT/2) + 1), 2 * (rand ()% (WIDTH/2) + 1));
Boundary
for (inti = 0; I <= HEIGHT + 1; i++)
{
Map[i][0] = WALL;
Map[i][width + 1] = WALL;
}
for (inti = 0; I <=WIDTH+1; i++)
{
Map[0][i] = WALL;
Map[height + 1][i] = WALL;
}
Draw the entrance and exit
MAP[2][1] = START;
Map[height-1][width] = end;
for (inti = 1; I <= HEIGHT; i++)
{
for (INTJ = 1; J <= WIDTH; j + +)
{
Paint (i, j);
}
}
Paint (0, 0);
Return0;
}

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 + +.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.